shell结构化语句,判断,循环

命令替换

testing=`date`
testing2=$(date)

数字运算

test3=$[ 1+5 ]#只支持整数运算 
var1=10.46
var2=43.67
var3=33.2
var4=71
var5=$( bc<<EOF>1.txt
scale=4
a1=($var1*$var2)
b1=($var3*$var4)
a1+b1
EOF
)#浮点型

if-then 只能测试退出状态码语句
if -then语句

if command
then
    commands
fi

if-then-else语句

if command
then
    commands
else
    commands
fi

if-then-elif-then

if command
then
    commands
elif commands
then
    commands
fi

test语句

if test condition
then
    commands
else
    commands
fi
#若没有 condition,则返回状态码为非零,直接执行else

数值比较

n1 -eq n2 等于
n1 -ge n2 大于等于
n1 -gt n2 大于
n1 -le n2 小于等于
n1 -lt n2 小于
if [$a -gt 5]
then
    echo "大于"
fi

比较字符串

str1 = str2 检查 str1 是否和 str2 相同
str1 != str2 检查 str1 是否和 str2 不同
str1 < str2 检查 str1 是否比 str2 小
str1 > str2 检查 str1 是否比 str2 大
-n str1 检查 str1 的长度是否非0
-z str1 检查 str1 的长度是否为0
var1="abcd"
var2="abcd"
var3="ef"
var4="EF"
if [ -z $var1 ]
then
        echo "dengyu"
else
        echo "budeng"
fi

比较文件

-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 是否存在并且默认组与当前用户相同
file1 -nt file2 检查 file1 是否比 file2 新
file1 -ot file2 检查 file1 是否比 file2 旧

复合条件

[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]

(( a++))[[ a + + ) ) 双 括 号 提 供 数 值 高 级 计 算 [ [ a=r* ]] 双方括号提供字符串高级功能,如模式匹配

case:

case variable in
pattern1 | pattern2) 
    commands1;;
pattern3) 
    commands2;;
*) 
    default commands;;
esac

for命令

list="hello world"
list=$list" girl" #注意girl前的空格
for str in $list
do
        echo $str
done

更改字段分隔符

file="/etc/passwd"
IFS=$'\n'
for state in $( head -30 $file )
do
        echo  $state
done
~     
如果要指定多个 IFS 字符,只要将它们在赋值行串起来就行。
IFS=$'\n':;"
这个赋值会将换行符、冒号、分号和双引号作为字段分隔符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值