linux shell脚本学习笔记

1. if-then语法

if command
then
	commands
fi

if后面的命令,执行后状态码返回0,则执行then部分的命令

#执行pwd
wang@wang:~/test$ pwd
/home/wang/test
#查看退出码
wang@wang:~/test$ echo $?
0

#执行pwda
wang@wang:~/test$ pwda
未找到 'pwda' 命令,您要输入的是否是:
 命令 'pwdx' 来自于包 'procps' (main)
 命令 'pwd' 来自于包 'coreutils' (main)
 命令 'pida' 来自于包 'pida' (universe)
 命令 'pda' 来自于包 'speech-tools' (universe)
pwda:未找到命令
#查看退出码
wang@wang:~/test$ echo $?
127

#pwd执行成功,退出码为0,而pwda命令执行失败,退出码非0

if pwd
then
	echo "then"
else
	echo "else"
fi

#执行结果
wang@wang:~/test$ ./a.sh 
/home/wang/test
then
#pwd命令执行成功,退出码为0,执行then语句块

2. if-then只能测试命令退出码,如果测试其他条件,可用test命令wang@wang:~/test$ cat a.sh

#!/bin/bash
val="test"

if val
then
	echo "then"
else
	echo "else"
fi

wang@wang:~/test$ ./a.sh 
./a.sh: 行 5: val: 未找到命令
else
#执行"if val"时,提示未找到命令,退出码非0,脚本执行then语句块

如果判断val是否有值,可以使用test命令,语法如下:

if test condition
then
	commands
fi
val="hello"
if test $val
then
	echo "return true"
else
	echo "return false"
fi

#执行结果
wang@wang:~/test$ ./a.sh 
return true

bash shell 提供了另一种测试方法,无需声明test命令

if [ condition ]
then
    command
fi
wang@wang:~/test$ cat a.sh 
#!/bin/bash

val="hello"

if [ $val ]
then
	echo "return true"
else
	echo "return false"
fi

wang@wang:~/test$ ./a.sh 
return true

3. 数值比较

n1 -eq n2	检查n1是否等于n2
n1 -ge n2	检查n1是否大于等于n2
n1 -gt n2	检查n1是否大于n2
n1 -le n2	检查n1是否小于等于n2
n1 -lt n2	检查n1是否小于n2
n1 -ne n2	检查n1是否不等于n2
value1=10
value2=11

if [ $value1 -lt $value2 ]
then
	echo "$value1 is less then $value2"
fi
#执行结果
wang@wang:~/test$ ./a.sh 
10 is less then 11

4. 字符串比较

str1 = str2	检查str1是否等于str2
str1 != str2	检查str1是否不等于str2
str1 < str2	检查str1是否小于str2
str1 > str2	检查str1是否大于str2
-n str1		检查str1长度是否非0
-z str1		检查str1长度是否为0
str1=hello
str2=world
if [ $str1 \< $str2 ]
then
	echo "$str1 is less then $str2"
fi
#执行结果
wang@wang:~/test$ ./a.sh 
hello is less then world

注意:
使用大于,小于符号时需要转义

5. 文件比较

-d file		检查file是否存在并是一个目录
-e file		检查file是否存在
-f file		检查file是否存在并是一个文件
-r file		检查file是否存在并可读
-s file		检查file是否存在并非空
-w file		检查file是否存在并可写
-x file		检查file是否存在并可执行
-O file		检查file是否存在并属当前用户
-G file		检查file是否存在且默认组与当前用户相同
f1 -nt f2	检查f1是否比f2新
f1 -ot f2	检查f1是否比f2旧
location=$HOME

if [ -e $location ]
then
	echo "$location exists"
fi

#执行结果
wang@wang:~/test$ ./a.sh 
/home/wang exists

6. 复合条件测试

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
if [ -d $HOME ] && [ -w $HOME/test.sh ]
then
	echo "$HOME/test.sh exists and you can write to it"
fi

7. 数学表达式使用双括号

(( expression ))
val1=10
if (( $val1 ** 2 > 80 ))
then
	(( val2 = $val1 ** 2 ))
	echo "val2 is $val2"
fi

#执行结果
wang@wang:~/test$ ./a.sh 
val2 is 100

8. 字符使用双方括号

[[ expression ]]
echo $USER
if [[ $USER == w* ]]
then
	echo "hello"
fi

#执行结果
wang@wang:~/test$ ./a.sh 
wang
hello

9. case语法

case variable in
pattern1 | pattern2)
	command1;;
pattern3)
	command2;;
*)
	command3;;
esac
case $USER in 
wangm | han)
	echo "Welcome $USER";;
testing)
	echo "testing account";;
*)
	echo "sorry, you are not allowed here";;
esac

10. 获取脚本参数

$0用于获取第1个参数的值,即脚本文件名称;$1用于获取第2个参数的值;$2获取第3个参数的值,以此类推

脚本test.sh内容如下:

#!/bin/bash

p0=$0
p1=$1
p2=$2
echo "p0: "$p0
echo "p1: "$p1
echo "p2: "$p2

执行脚本: ./test.sh hello world,则执行结果如下:

p0: ./test.sh
p1: hello
p2: world
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值