本系列文章为《Linux Shell Scripting Cookbook》的读书笔记,只记录了我觉得工作中有用,而我还不是很熟练的命令

书是很好的书,有许多命令由于我比较熟悉,可能就没有记录在其中了

1. 获得进程的环境变量

cat /proc/$PID/environ

将原先彼此间的null('\0')分隔符替换成换行

cat /proc/1194/environ | tr '\0' '\n'

2. 获得字符串长度

length=${#var}

3. 数字运算

let, (()), []执行基本的算数操作,高级操作使用expr,bc

let no1++

let no1-=6

result=$[ no1 + no2 ]

result=$[ $no1 + 5 ]

result=$(( no1 + 50 ))

4. 调试脚本

#!/bin/bash
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}
echo this is test file
for i in {1..6}
do
DEBUG echo $i
done

Bash中 : 告诉shell不要进行任何操作

$@ 表示所有脚本参数的内容

5. 字段分隔符IFS (Internal Field Separator)

#!/bin/bash
passwd=$(cat /etc/passwd)
oldIFS=$IFS;
IFS=$'\n'
for line in $passwd;
do
IFS=":"
count=0
for item in $line;
do
[ $count -eq 0 ] && user=$item;
[ $count -eq 6 ] && shell=$item && echo $user\'s shell is $shell;
let count++
done;
done;
IFS=$oldIFS

第一个IFS的赋值为$'\n',为了第一个for循环按行获得passwd里面的值

第二个IFS赋值为":",为了按:为分隔,取得每个段的值

其中for循环的主体部分,[]的使用可以作为简洁版的判断语句

6. 比较与测试

[ $var1 -ne 0 -a $var2 -gt 2 ] #逻辑与 -a,逻辑或-o

使用&& 以及 || 也可以组合条件

if [[ -n $str1 ]] && [[ -z $str2 ]]; then ...

使用test命令也可以执行条件检测,可以避免使用过多的括号,多个条件使用-a/-o连接

test命令也有更广泛的文件系统相关测试选项,具体查看man test

#!/bin/bash
no1=1
no2=0
test $no1 -eq 1 -a $no2 -gt 1 && echo success || echo fail

文件系统相关测试

[ -f $file_var ]  文件路径或文件名

[ -x $var ] 文件可执行

[ -d $var ] 目录

[ -w $var ] 可写

[ -r $var ] 可读

[ -L $var ] 符号链接

[ -e $var ] 如果给定的变量包含的文件存在,则返回真

例如

#!/bin/bash
fpath=/etc/passwd
[ -e $fpath ] && echo file exist || echo file not exist

字符串的比较最好使用双中括号

[[ -z $str ]] 空串返回真

[[ -n $str ]] 非空串返回真

Linux Shell Scripting Cookbook 读书笔记 1的更多相关文章

  1. Linux Shell Scripting Cookbook 读书笔记 2

    cat,script,find, xargs, tr, tmp文件,字符串截取,批量文件重命名,固定大小文件,自动化交互 1. cat的用法 压缩连续的空白行 cat -s file 也可以用tr,将 ...

  2. Linux Shell Scripting Cookbook 读书笔记 7

    ping, du, ps, kill, 收集系统信息 判断网络中哪些主机是活动主机 #!/bin/bash for ip in 10.215.70.{1..255}; do ( ping $ip -c ...

  3. Linux Shell Scripting Cookbook 读书笔记 6

    wget,curl, tar, rsync wget ftp://example.com/somefile.img -t 5 -O download.img -o log -t表示重试的次数 -O指定 ...

  4. Linux Shell Scripting Cookbook 读书笔记 5

    sed,awk 1. sed (string editor) 使用-i可以将结果运用于原文件 sed 's/text1/text2/' file > newfile mv newfile fil ...

  5. Linux Shell Scripting Cookbook 读书笔记 4

    正则, grep 1. 正则表达式  正则表达式  描述  示例 ^ 行起始标记  ^hell匹配以hell开头的行 $ 行尾标记  test$匹配以test结尾的行 . 匹配任意一个字符  hell ...

  6. Linux Shell Scripting Cookbook 读书笔记 3

    patch, tree, head ,tail 1. 创建不可修改文件 chattr +i file chattr -i file 移除不可修改属性 2. 能够启动闪存或硬盘的混合ISO isohyb ...

  7. 《The Linux Command Line》 读书笔记02 关于命令的命令

    <The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...

  8. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  9. 《Linux内核分析》读书笔记(四章)

    <Linux内核分析>读书笔记(四章) 标签(空格分隔): 20135328陈都 第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行 ...

随机推荐

  1. SqlServer 导出指定表数据 生成Insert脚本

    版权声明:本文为博主原创文章,未经博主允许不得转载.

  2. 安装Oracle客户端时,检查系统要求时状态为错误的解决办法

    这是我自己安装oracle11g至win7的错误记录: 正在检查操作系统要求... 要求的结果: 5.0,5.1,5.2,6.0 之一 实际结果: 6.1 我换了 10g,11g从32bit到64bi ...

  3. <转>python 发送邮件实例

    文件形式的邮件 #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText fr ...

  4. eslint推荐编码规范和airbnb推荐编码规范

    Eslint规范 for 循环禁止使用无限循环(这个非默认推荐) // bad for (var i = 0; i < 10; i--) { } for (var i = 10; i >= ...

  5. springMvc学习地址新

    http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar ...

  6. BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char str[maxn],strtot[ ...

  7. Python之CSV模块

    1. CSV简介 CSV(Comma Separated Values)是逗号分隔符文本格式,常用于Excel和数据库的导入和导出,Python标准库的CSV模块提供了读取和写入CSV格式文件的对象. ...

  8. 分别用for循环,while do-while以及递归方法实现n的阶乘!

    分别用for循环,while do-while以及递归方法实现n的阶乘! 源码: package book;import java.util.Scanner;public class Access { ...

  9. php如何判断SQL语句的查询结果是否为空?

    PHP与mysql这对黄金搭档配合的相当默契,但偶尔也会遇到一些小需求不知道该怎么做,例如今天要谈到的:如何判断sql语句查询的结果集是否为空! 我们以查询学生信息为例,来看看究竟如何实现我们的需求. ...

  10. Problem 17

    Problem 17 If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there a ...