練習: 寫一個腳本,判斷當前系統上是否有用戶的默認shell爲bash,若是有就顯示有多少個這類的用戶;不然,就顯示沒有這類的用戶。shell
#!/bin/bashbash
#ide
grep "\<bash$" /etc/passwd &> /dev/nullit
retval=$?class
if [ $retval -eq 0 ]引用
thengrep
users=`grep "\<bash" /etc/passwd | wc -l`腳本
echo "$users"di
elseview
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
一、let 算數運算表達式
let c=$A+$B
二、$[算數運算表達式]
C=$[$A+$B]
三、$((算數運算表達式))
C=$(($A+$B))
四、expr 算數運算表達式,表達式中各操做數及運算符之間要有空格,並且要使用命令引用
C=`expr $A + $B`