shell 学习笔记
常用的参数
变量名 说明
- $n 传递给脚本的第n个参数
- $# 命令行参数个数
- $0 当前脚本名称
- $* 以“参数1 参数2 参数3…”的形式返回所有参数值
- $@ 以“参数1” “参数2” ”参数3“…的形式返回所有参数值
- $_ 保存之前执行的命令的最后一个参数
- $? 最后运行的命令的结束代码(返回值)
- $$ Shell本身的PID(ProcessID)
定义变量:
显式赋值:your_name="jans"用语句赋值:for file in `ls /etc` (注意:这里不是单引号,是反引号)
使用变量:
echo $your_name 或者 echo ${your_name} 一般尽量带上大括号
重新定义变量:
your_name="jessica" 注意:重新定义的时候不能使用$,只有使用变量的时候才用$
字符串:
单引号:
str='this is a string' //单引号中不能出现变量,单引号中不能出现单引号(转移的单引号也不行)
双引号:
str="hello,I know you are \"$your_name\"!! \n" //双引号中可以有变量,可以出现转义字符
拼接字符串:
a="hello "b="world!"echo $a $b
获取字符串长度:
echo ${#your_name}
提取子字符串:
echo ${your_name:1:4} //输出第2到第5,一共4个字符
查找子字符串:
string="alibaba is a great company"echo `expr index "$string" is`#输出:8,这个语句的意思是:找出单词is在这名话中的位置
流程控制:
在linux中[是一个命令,在/usr/bin/[,所以[后面一定要加空格
if else语句:
if condition1;thencommand1;elif condition2;command2;commandN;elsecommand;fi
快捷if:
[ ] && 和 [ ] ||&&如果是“前面”,则“后面”[ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid 检查文件是否存在,如果存在就删掉||如果不是“前面”,则后面[ -f /usr/sbin/dhcpd ] || exit 0 检验文件是否存在,如果存在就退出[ -z "\$1" ] && help 如果第一个参数不存在,就显示help[ "\$1" = "-h" ] && help 如果第一个参数是-h,就显示help
for语句:
for i in item1 item2 itemNdocommand1command2done写成一行:for i in item1 item2...itemN;do command1;command2;done;for i in $(seq 0 4);do echo "second for :$i";donefor i in `seq 0 4`;do echo "third for:$i";donefor i in {0..4};do echo "fourth for:$i";donefor (( i=0; i<10; i++))docommand1;command2;done
while语句:
while conditiondocommanddone
无限循环1
while :docommanddone
无限循环2
while truedocommanddone
无限循环3
for (( ; ; ))
until:
until conditiondocommanddone
case:
每个case需要esac作为结束标记,用)表示分支,用两个分号表示breakcase "${var}" in"Aoptions" )commandAexit;;"Boption" )commandBexit;;"Exit" )exit;;* ) echo "Bad option,please choose again"esac
select选择项语句
#!/bin/bashPS3="请选择菜单"select i in "java" "php" "scala" "spark"doecho "你选择了:"$i $i $iexitdone
条件表达式
文件表达式
if [ -f file ] 如果文件存在if [ -d ... ] 如果目录存在if [ -s file ] 如果文件存在且非空if [ -r file ] 如果文件存在且可读if [ -w file ] 如果文件存在且可写if [ -x file ] 如果文件存在且可执行
整数变量表达式(> 和<不能用于比较大小 ,会被当作尖括号)
if [ int1 -eq int2 ] 如果int1等于int2if [ int1 -ne int2 ] 如果不等于if [ int1 -ge int2 ] 如果>=if [ int1 -gt int2 ] 如果>if [ int1 -le int2 ] 如果<=if [ int1 -lt int2 ] 如果<
字符串变量表达式
if [ \$a = \$b ] 如果string1等于string2if [ \$string1 != \$string2 ] 如果string1不等于string2if [ -n \$string ] 如果string 非空(非0),返回0(true)if [ -z \$string ] 如果string 为空if [ \$string ] 如果string 非空,返回0 (和-n类似)
逻辑表达式
逻辑非 ! //if [ ! -d \$num ] 如果不存在目录$num逻辑与 –a //if [ 表达式1 –a 表达式2 ]逻辑或 -o //if [ 表达式1 –o 表达式2 ]例如:if [ -z "\$JHHOME" -a -d \$HOME/\$num ]如果变量\$JHHOME为空,且\$HOME/\$num目录存在
其他
- source myscript.sh 等价于 ./myscript.sh
- If [ $str ] 等价于 if [ -n $str ] //判断字符串变量是否为空
- shell中0为真,非0为假
- = 作为等于时,其两边都必须加空格,否则失效
- =赋值时等号两边不能有空格
- -eq -ne -lt -nt 只能用于整数,不适用于字符串,字符串判断等于用等号 var=”yes” if [ $var -eq “yes” ] //错误!!!,-eq不能用于字符串
- shell中整形变量不能直接if,必须if [ i –ne 0 ],但可以支持字符串变量if // if [ str ]
- if command 等价于 command + if $?
| #!/bin/sh if cat 111-tmp.txt | grep ting1 then echo found else echo “no found” fi |
#!/bin/sh cat 111-tmp.txt | grep ting1 if [ $? -eq 0 ] then echo $? echo found else echo $? echo “no found” fi |

浙公网安备 33010602011771号