If statement in linxu shell

本文详细介绍了bash3.0中的新特性,包括字符串比较、整数操作、文件操作等,通过实例展示了如何使用这些功能,并提供了练习题加深理解。

Be careful of the blank in the following statements.

 

[[]] is a new feature in bash 3.0 for string comparision.

{{}} is a new feature in bash 3.0 for integer comparision.

so we can use "==, >=, <=" supersede "-eq, -gt, -lt"

 

Example:

 

if [[ "1" == "1" ]]; then

    echo "1 equals 1"

fi

 

exit

 

 

$(()) expression can be used to do the operations with the integer, like

 

sum=$((1+1))

echo $sum

 

Then the output is 2.

 

Other way to the same result is:

declare -i sum=1+1 or sum=$[1+1]

 

 

others:

accounts=cat "/etc/passwd" | cut -d ":" -f1

for account in accounts

do

    echo "$account"

done

 

 

if [ ! -e "somefile"]; then

fi

 

rm file

rmdir dir

touch file

 

echo $$ show curent pid

 

echo $? show the last return code.

 

export | cut -c 12- which will show 12+ chars

 

exercise:

cat /etc/passwd | sort -t ":" -k 3      sort according to 3rd column separated by ":"

uniq -c show the count which occurs multiply.

 

cat /etc/passwd | tr -d ':' remove ":"

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值