readonly
关键字unset
关键字变量 | 含义 |
---|---|
$0 | 当前脚本的文件名 |
$n | 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。 |
$# | 传递给脚本或函数的参数个数。 |
$* | 传递给脚本或函数的所有参数。 |
$@ | 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。 |
$? | 上个命令的退出状态,或函数的返回值。 |
$$ | 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。 |
#!/bin/bash
echo "File Name: $0"
echo "First Parameter : $1"
echo "First Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"
#!/bin/bash
echo -e "This Bash Test \nfrom koffuxu.com"
echo -n "Please Input Your Nmae"
read name
echo $name
echo #blank line
read -p "How are you" answer
echo $answer
Here Document是已“<<空格变量”为标识的一段Shell程序
当我们跟有些程序交互时,shell脚本却帮不了我们太多。比如编辑一个文件和操作数据库。编辑一个文件一般用vi,进去后hjkl的aio的操作什么的,脚本怎么执行?mysql登陆输入mysql -u 用户 -p 密码后,就跟mysql进行一串交互,shell怎么执行?Here Document就可以派上用场了。
实例ftp操作
lftp ftp-china.amlogic.com <
数学运算符
a=10
b=20
val=`expr $a + $b`
echo “a + b : $val”
表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
完整的表达式要被 ` ` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。
关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字。
运算符 | 说明 | 举例 |
---|---|---|
-eq | 检测两个数是否相等,相等返回 true。 | [ $a -eq $b ] 返回 true。 |
-ne | 检测两个数是否相等,不相等返回 true。 | [ $a -ne $b ] 返回 true。 |
-gt | 检测左边的数是否大于右边的,如果是,则返回 true。 | [ $a -gt $b ] 返回 false。 |
-lt | 检测左边的数是否小于右边的,如果是,则返回 true。 | [ $a -lt $b ] 返回 true。 |
-ge | 检测左边的数是否大等于右边的,如果是,则返回 true。 | [ $a -ge $b ] 返回 false。 |
-le | 检测左边的数是否小于等于右边的,如果是,则返回 true。 | [ $a -le $b ] 返回 true。 |
操作符 | 说明 | 举例 |
---|---|---|
-b file | 检测文件是否是块设备文件,如果是,则返回 true。 | [ -b $file ] 返回 false。 |
-c file | 检测文件是否是字符设备文件,如果是,则返回 true。 | [ -b $file ] 返回 false。 |
-d file | 检测文件是否是目录,如果是,则返回 true。 | [ -d $file ] 返回 false。 |
-f file | 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 | [ -f $file ] 返回 true。 |
-g file | 检测文件是否设置了 SGID 位,如果是,则返回 true。 | [ -g $file ] 返回 false。 |
-k file | 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 | [ -k $file ] 返回 false。 |
-p file | 检测文件是否是具名管道,如果是,则返回 true。 | [ -p $file ] 返回 false。 |
-u file | 检测文件是否设置了 SUID 位,如果是,则返回 true。 | [ -u $file ] 返回 false。 |
-r file | 检测文件是否可读,如果是,则返回 true。 | [ -r $file ] 返回 true。 |
-w file | 检测文件是否可写,如果是,则返回 true。 | [ -w $file ] 返回 true。 |
-x file | 检测文件是否可执行,如果是,则返回 true。 | [ -x $file ] 返回 true。 |
-s file | 检测文件是否为空(文件大小是否大于0),不为空返回 true。 | [ -s $file ] 返回 true。 |
-e file | 检测文件(包括目录)是否存在,如果是,则返回 true。 | [ -e $file ] 返回 true。 |
单引号与双引号的区别
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
case 值 in
模式1)
command1
command2
command3
;;
模式2)
command1
command2
command3
;;
*)
command1
command2
command3
;;
esac
for 变量 in 列表
do
command1
command2
...
commandN
done
while command
do
Statement(s) to be executed if command is true
done
Shell 函数的定义格式如下:
function_name () {
list of commands
[ return value ]
}
如果你愿意,也可以在函数名前加上关键字 function:
函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值。
$ command > file
$ command >> file
$ command < file
基本格式
gawk option program file
gawk '{print "Hello Koffu"}'
因为没有file选项,命令将等待STDIN输入。program放在’{}'内。
-F 就是用来接分隔字段的分隔符
数据字段
$0 表示整个文本行,因为gawk是逐行处理的
$1 表示第一个数据字段
awk '{print $1}' vim.vimrc
打印每行的第一个单词,也就是说默认分隔符是-F 空格;如果要以其它字段分隔符的话,使用-F指定
awk -F : '{print $1}' /etc/passwd
awk -F : 'BEGIN {print "USER Shell" } {print $1" "$7}' /etc/passwd
gangfeng.xu@droid11-sz:0810$ ls p200-0807-autosuspend#120.txt |xargs awk '{s+=gsub(/process command/,"&")} END{printf("filename:%10s, count=%s\n",FILENAME, s)}'
filename:p200-0807-autosuspend#120.txt, count=376
find -name "*.c"|xargs wc -l |awk '{if ($1 > 3600) {print $1 $2}}'
sed -e 's/dog/cat/; /big/small/' file
#!/bin/bash
sed '/^$/d' $0 >> output.file
echo "this is great book" | sed -n '/book$/p'
echo "Yes" | sed -n '/[Yy]es/p'
Yes
echo "yes" | sed -n '/[Yy]es/p'
yes
同样,可以使用[^]来设置过滤
sed -n '/[^ch]at/p' #排除cat或者hat,但保留 at的行
同样,还可以使用[-]来设置区间
sed -n '/[c-h]at/p' #过滤c到h之间的单词的行。
gangfeng.xu@droid11-sz:20150817$ echo "this is cat"|sed -n '/ii*s/p'
this is cat
参考:
1.http://c.biancheng.net/cpp/view/6994.html
2.Linux命令行与Shell脚本编程大全第2版》
实现代码:www.github.com/koffuxu/