Medusar's Blog
敬畏知识,谦逊前行
Toggle navigation
Medusar's Blog
主页
Booklist
Resources
About Me
归档
标签
有用的Shell片段整理
red
blue
yellow
green
2017-05-07 22:44:55
333
0
0
medusar
red
blue
yellow
green
## 截取字符串的最后一位 ``` bash # echo $var 3234j35ja # echo ${var: -1} #注意:-1前要有个空格 a ``` ## 来自网络 1. 很多很全很强大的http://tldp.org/LDP/abs/html/contributed-scripts.html ## 参数校验 ### 校验参数个数 ``` bash #!/bin/sh #------------------------------------- # this function is used to check the number of parameters #------------------------------------- E_WRONG_ARGS=85 # parameters script_parameters="-a -h -m -z" Number_of_expected_args=4 if [ $# -ne $Number_of_expected_args ] then echo "Usage: `basename $0` $script_parameters" # `basename $0` is the script's filename exit $E_WRONG_ARGS fi ``` ### 特殊字符 ``` bash # {} 双括号用来表示多个 echo \"{These,words,are,quoted}\" # " prefix and suffix # "These" "words" "are" "quoted" cat {file1,file2,file3} > combined_file # Concatenates the files file1, file2, and file3 into combined_file. cp file22.{txt,backup} # Copies "file22.txt" to "file22.backup" echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z # Echoes characters between a and z. echo {0..3} # 0 1 2 3 # Echoes characters between 0 and 3. base64_charset=( {A..Z} {a..z} {0..9} + / = ) # Initializing an array, using extended brace expansion. # From vladz's "base64.sh" example script. ``` ### echo ``` bash hello="A B C D" echo $hello # A B C D echo "$hello" # A B C D # As we see, echo $hello and echo "$hello" give different results. echo '$hello' # $hello ``` ### 批量命令 #### 批量创建文件夹 ``` bash mkdir rc{0,1,2,3,4,5,6,S}.d ``` 上面这个比下面这个要简单 ``` bash for runlevel in 0 1 2 3 4 5 6 S do mkdir rc${runlevel}.d done ``` #### ls也可以批量 ``` bash $ cd / $ ls -ld {,usr,usr/local}/{bin,sbin,lib} drwxr-xr-x 2 root root 4096 Oct 26 01:00 /bin drwxr-xr-x 6 root root 4096 Jan 16 17:09 /lib drwxr-xr-x 2 root root 4096 Oct 27 00:02 /sbin drwxr-xr-x 2 root root 40960 Jan 16 19:35 usr/bin drwxr-xr-x 83 root root 49152 Jan 16 17:23 usr/lib drwxr-xr-x 2 root root 4096 Jan 16 22:22 usr/local/bin drwxr-xr-x 3 root root 4096 Jan 16 19:17 usr/local/lib drwxr-xr-x 2 root root 4096 Dec 28 00:44 usr/local/sbin drwxr-xr-x 2 root root 8192 Dec 27 02:10 usr/sbin ``` ### 判断变量是否存在 下面的命令,如果PID不存在,会报错 ``` bash test $PID -gt 0 ``` 结果: ``` bash BJ1431:practice momo$ test $PID -gt 0 -bash: test: -gt: unary operator expected ``` 即使先判断PID是否为空也不行: ``` bash BJ1431:practice momo$ test -n $PID && test $PID -gt 0 -bash: test: -gt: unary operator expected ``` `-n`是判断字符串长度是否非0,但是如果变量不存在,也会返回true,所以正确的办法是用双引号把变量抱起来。 ``` bash test -n "$PID" && test $PID -gt 0 ``` **所以是不是针对字符串的判断,最好都用双引号括起来呢?** ### 为变量定义默认值 #### 复杂的方式 ``` bash #!/bin/sh echo -en "What is your name [ `whoami` ] " read myname if [ -z "$myname" ]; then myname=`whoami` fi echo "Your name is : $myname" ``` #### 更简单的方式 ``` bash echo -en "What is your name [ `whoami` ] " read myname echo "Your name is : ${myname:-John Doe}" ``` 或 ``` bash echo "Your name is : ${myname:-John Doe}" ``` 或 ``` bash echo "Your name is : ${myname:=John Doe}" ``` **通过`:-`或`:=`可以为shell中的变量赋默认值,变量需要用${}包围** ### awk 显示匹配行的上一行或下一行 假设有文件 out.log ``` bash 2017-05-07 19:25:43,982 INFO message received: hello how are you ? 2017-05-07 19:25:45,415 INFO message received: what is your name ? ``` 上面这个文件比较奇怪,时间戳和info内容不在同一行,那么以下两个需求如何实现呢? 1) 查找`hello how are you ?`消息接收的时间 2)查找`2017-05-07 19:25:45,415`时间接收到的消息内容 当然用grep可以解决问题,但是我们要用awk来实现(awk在控制输出方面更强大)。我们需要做的就是输出匹配行的上一行或者下一行。 ### 显示匹配行的上一行 ``` bash awk '/hello how are you/ {print a;print $0}{a=$0}' out.log ``` 说明: 上面的执行过程用ifelse表示相当于 ``` bash while (not end){ if (current_lines contains 'hello how are you'){ print a; print $0; } a = current_line; } ``` 每次都把当前行存入变量a,当前行匹配的时候,输出变量a,此时a实际上是上一行的内容 注意: `a=$0`是单独放在`{}`中的。 ### 显示匹配行的下一行 ``` bash awk '/2017-05-07 19:25:45,415/{print;getline;print}' out.log ``` 说明: > getline 是awk的内置函数,用于读取下一行。 > print; 默认输出当前操作的行,相当于print $0. ## shell quick reference ``` bash Command Description Example & Run the previous command in the background ls & && Logical AND if [ "$foo" -ge "0" ] && [ "$foo" -le "9"] || Logical OR if [ "$foo" -lt "0" ] || [ "$foo" -gt "9" ] (not in Bourne shell) ^ Start of line grep "^foo" $ End of line grep "foo$" = String equality (cf. -eq) if [ "$foo" = "bar" ] ! Logical NOT if [ "$foo" != "bar" ] $$ PID of current shell echo "my PID = $$" $! PID of last background command ls & echo "PID of ls = $!" $? exit status of last command ls ; echo "ls returned code $?" $0 Name of current command (as called) echo "I am $0" $1 Name of current command's first parameter echo "My first argument is $1" $9 Name of current command's ninth parameter echo "My ninth argument is $9" $@ All of current command's parameters (preserving whitespace and quoting) echo "My arguments are $@" $* All of current command's parameters (not preserving whitespace and quoting) echo "My arguments are $*" -eq Numeric Equality if [ "$foo" -eq "9" ] -ne Numeric Inquality if [ "$foo" -ne "9" ] -lt Less Than if [ "$foo" -lt "9" ] -le Less Than or Equal if [ "$foo" -le "9" ] -gt Greater Than if [ "$foo" -gt "9" ] -ge Greater Than or Equal if [ "$foo" -ge "9" ] -z String is zero length if [ -z "$foo" ] -n String is not zero length if [ -n "$foo" ] -nt Newer Than if [ "$file1" -nt "$file2" ] -d Is a Directory if [ -d /bin ] -f Is a File if [ -f /bin/ls ] -r Is a readable file if [ -r /bin/ls ] -w Is a writable file if [ -w /bin/ls ] -x Is an executable file if [ -x /bin/ls ] ```
上一篇: 无
下一篇:
Linux diff命令
0
赞
333 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
文档导航