本文记下常用的bash shell语法和命令,方便日后参考。
参考文档
Bash Guide for Beginners
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/
Contents
注释 Comment
1. bash脚本要以
#!/bin/bash
开头。
2. ‘#’号后面的都是注释,如
# An example for bash comment. echo "hehe" # Print hehe to screen.
运行脚本 Run Script
1. 以Bash子进程运行,环境变量依然有效;(需要执行权限)
$ ./test.sh
或者
$ bash test.sh
开启调试模式
$ bash -x test.sh
2. 直接在当前Bash运行,环境变量和当前Bash的变量都有效;
$ source test.sh
或者
$ . test.sh
变量 Variables
定义变量可以很简单
var1 = hehe var2 = 20 var3 = 6.1
如果想bash的子进程也能使用该变量,要加一个export.
export globle_var1 = 20
字符引用:单引号
单引号引用的所有字符都会escaped。
echo '$date' $date
字符引用:双引号
双引号会escaped除了$符,反引号,\的字符
franky ~> echo "$date" 20021226 franky ~> echo "`date`" Sun Apr 20 11:22:06 CEST 2003 franky ~> echo "I'd say: \"Go for it!\"" I'd say: "Go for it!" franky ~> echo "\" More input>" franky ~> echo "\\" \
常用变量
1. $# 输入的参数个数;
2. $? 上条命令的退出值;
3. $0 shell文件名;
输入输出 IO
1. 输出 echo
$ echo test test
2. 输出 printf
$ printf "%d\n" 100000
3. 输入 read
read var1 var2...
条件控制 Condition Control
if
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
if语句的语法
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi
if TEST-COMMANDS; then CONSEQUENT-COMMANDS; elif MORE-TEST-COMMANDS; then MORE-CONSEQUENT-COMMANDS; else ALTERNATE-CONSEQUENT-COMMANDS; fi
判断大小
if [ "$?" -ne "0" ]
case
case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN) COMMAND-LIST;; esac
示例
case "$1" in start) start ;; stop) stop ;; status) status anacron ;; restart) stop start ;; condrestart) if test "x`pidof anacron`" != x; then stop start fi ;; *) echo $"Usage: $0 {start|stop|restart|condrestart|status}" exit 1 esac
循环 Loop
for
for NAME [in LIST ]; do COMMANDS; done
Example:
for filename in `ls $1` do cmp $1$filename $2$filename done
for ((i=0; i<1024; i++)) do commands done
while
while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done
until
until TEST-COMMAND; do CONSEQUENT-COMMANDS; done
break, continue
break和continue用法跟C语言一样。
正则表达式 Regular expressions
. Matches any single character. ? The preceding item is optional and will be matched, at most, once. * The preceding item will be matched zero or more times. + The preceding item will be matched one or more times. {N} The preceding item is matched exactly N times. {N,} The preceding item is matched N or more times. {N,M} The preceding item is matched at least N times, but not more than M times. - represents the range if it's not first or last in a list or the ending point of a range in a list. ^ Matches the empty string at the beginning of a line; also represents the characters not in the range of a list. $ Matches the empty string at the end of a line. \b Matches the empty string at the edge of a word. \B Matches the empty string provided it's not at the edge of a word. \< Match the empty string at the beginning of word. \> Match the empty string at the end of word.
grep要用re的话,最好加上 -E 这个参数;
sed
查看sed的用法: info sed
Command Result a\ Append text below current line. c\ Change text in the current line with new text. d Delete text. i\ Insert text above current line. p Print text. r Read a file. s Search and replace text. w Write to a file.
Option Effect -e SCRIPT Add the commands in SCRIPT to the set of commands to be run while processing the input. -f Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input. -n Silent mode. -V Print version information and exit.
awk
The variables $1, $2, $3, ..., $N hold the values of the first, second, third until the last field of an input line. The variable $0 (zero) holds the value of the entire line.
kelly@octarine ~/test> ls -l | awk '{ print $5 $9 }' 160orig 121script.sed 120temp_file 126test 120twolines 441txt2html.sh
kelly@octarine ~/test> ls -ldh * | grep -v total | \ awk '{ print "Size is " $5 " bytes for " $9 }' Size is 160 bytes for orig Size is 121 bytes for script.sed Size is 120 bytes for temp_file Size is 126 bytes for test Size is 120 bytes for twolines Size is 441 bytes for txt2html.sh
kelly@octarine ~> df -h | sort -rnk 5 | head -3 | \ awk '{ print "Partition " $6 "\t: " $5 " full!" }' Partition /var : 86% full! Partition /usr : 85% full! Partition /home : 70% full!
函数 Function
定义
function FUNCTION { COMMANDS; } or FUNCTION () { COMMANDS; }
参数
function的参数与脚本的参数差不多。
[lydia@cointreau ~/test] cat showparams.sh #!/bin/bash echo "This script demonstrates function arguments." echo echo "Positional parameter 1 for the script is $1." echo test () { echo "Positional parameter 1 in the function is $1." RETURN_VALUE=$? echo "The exit code of this function is $RETURN_VALUE." } test other_param [lydia@cointreau ~/test] ./showparams.sh parameter1 This script demonstrates function arguments. Positional parameter 1 for the script is parameter1. Positional parameter 1 in the function is other_param. The exit code of this function is 0.