bash?_本?程之三 条件判断及算数运算
练习: 写一个脚本,判断当前系统上是否有用户的默认shell为bash,如果有就显示有多少个这类的用户;否则,就显示没有这类的用户。 #!/bin/bash # grep "&;bash$" /etc/passwd &> /dev/null retval=$? if [ $retval -eq 0 ] then users=`grep "&;bash" /etc/passwd | wc -l` echo "$users" else echo "no such user." fi 例:写一个脚本,判断当前系统上是否有用户的默认shell为bash,如果有就显示其中一个用户名,没有就显示没有这类的用户。 #!/bin/bash # grep "&;bash$" /etc/passwd &> /dev/null retval=$? if [ $retval -eq 0 ] then users=`grep "&;bash" /etc/passwd | head -1 | cut -d: -f1` echo "$users is one of such users." else echo "no such user." fi 例:给定一个用户,判断其UID与GID是否一样,如果一样就显示此用户为“good guy”,否则就显示是“bad guy” #!/bin/bash # username=user1 userid=`id -u $username` groupid=`id -g $username` if [ $userid -eq $grepid ] then echo "good guy." else echo "bad guy." fi shell中如何做算数运算: A=3 B=6 1、let 算数运算表达式 let c=$A+$B 2、$[算数运算表达式] C=$[$A+$B] 3、$((算数运算表达式)) C=$(($A+$B)) 4、expr 算数运算表达式,表达式中各操作数及运算符之间要有空格,而且要使用命令引用 C=`expr $A + $B` (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |