天天看點

bash程式設計之五 字元串測試以及for循環

字元測試:

==:測試是否相等,相等為真,否則為假

!=:測試是否不等,不等為真,否則為假

>或<:将字元串轉化為ASCll值然後比較其大小

-n:判斷字元是否為空,空為真,不空為假

-z:判斷字元是否為不空,不空為真,空則為假

bash循環結構:必須有進入條件和退出條件

while

until

for:

    for  變量 in 清單;do

        循環體;

    done

如何生成清單?

{1..100}

`seq [起始數 [步進長度] ] 結束數`

declare 聲明變量

    -i 聲明為整型

    -x 聲明為環境變量

練習:

傳遞一個使用者名參數給腳本,判斷此使用者的使用者名跟其基本組的組名是否一緻,并将結果顯示出來。

#!/bin/bash

if  [ $# -lt 1 ];then

echo "Usage `basename $0` ARG ..."

exit 7

fi

if ! id $1 &>/dev/null ;then 

echo "User  $1 not exists!"

elif [  $1 == `id -n -g $1` ];then

echo "$1's user and grop are the same"

else

echo  "$1's user and grop not the same!"

傳遞3個參數給腳本,第一個為整數,第二個為算數運算符,第三個為整數,将計算結果顯示出來,并保留兩位精度。

if [ $# -ne  3 ];then 

echo  "Usage: `basename $0` ARG OPT ARG..."

s=`echo "scale=2;$1 $2 $3"|bc`

echo "$1 $2 $3=$s"

傳遞參數給腳本,參數均為使用者名,把passwd裡面記錄的此賬号的相關資訊提取儲存到/tmp/user.txt中,并要求有行号。

if [ $# -lt  1 ];then 

echo  "Usage: `basename $0` ARG  "

if  ! id  $1 &>/dev/null ;then

echo "$1  not exists!"

b=`cat  user.txt|wc -l`

j=$[$b+1]

d=`grep  "^\<$1\>" /etc/passwd`

echo "$j $d" >>user.txt

 fi 

寫一個腳本,判斷目前主機的cpu生産商,其資訊在/proc/cpuinfo檔案中vendor id 一行中

如果其生産商為AuthenticAMD,就顯示其為AMD公司;

如果其生産商為GenuineIntel,就顯示其為Intel公司

否則,就顯示其為非主流公司

Info=`cat  /proc/cpuinfo |grep  vendor|cut -d: -f2|sed 's/[[:space:]]*\(.*\)/\1/'`

if [ $Info == GenuineIntel ]||[ $Info == AuthenticAMD ];then 

echo  "${Info}'s  cpu!"

else 

echo "rubbish cpu"

寫一個腳本,給腳本傳遞三個整數并顯示其最大值和最小值。

if [ $# -ne 3 ];then

echo "Usage:`basename $0` ARG1 ARG2 ARG3"

exit 1

case $1  in

[[:digit:]]*) 

;;

*)

echo "ARG1 must be the  integer!"

esac

case $2  in

echo "ARG2 must be the  integer!"

case $3  in

echo "ARG3 must be the  integer!"

MAX=0

MIN=$1

for i in $1 $2 $3;do

if [ $MAX -lt $i ]; then

MAX=$i

if [ $MIN -gt $i ];then

MIN=$i

done

echo  "MAX:$MAX MIN:$MIN"

寫一個腳本:

1、設定變量FILE的值為/etc/passwd

2、依次向/etc/passwd中的每個使用者問好,并顯示對方的shell,例如:

    hello,root,your shell :/bin/bash

3、統計一共有多少個使用者

FILE=/etc/passwd

count=`cat /etc/passwd|wc -l`

for  i in `seq 1 $count`

do

user=`cat /etc/passwd|head -$i|tail -1|cut -d: -f1`

shell=`cat /etc/passwd|head -$i|tail -1|cut -d: -f7`

echo "Hello $user,your shell :$shell"

echo "there are $count users"

1添加10個使用者user1到user10,但要求隻有使用者不存在的情況才添加

for i  in  `seq 1 10 `;do

if  id user$i &>/dev/null ;then 

echo "user$i exists!!"

useradd user$i

echo  "useradd user$i finished !"

計算100以内所有能被3整除的正整數的和;

declare -i sum=0

for  i in  {1..100}

        if [ $[$i%3] -eq 0 ] ;then

                sum=$[$sum+$i]

        fi

echo "sum : $sum "

寫一個腳本:計算100以内所有奇數的和以及所有偶數的和

declare -i oddsum=0

declare -i evensum=0

for  i  in  `seq 1 100`

if [ $[$i%2] -eq 0 ] ; then

evensum=$[$evensum+$i]

oddsum=$[$oddsum+$i]

echo  "oddsum:$oddsum 

evensum:$evensum"

寫一個腳本分别顯示目前系統上所有預設shell為bash的使用者和預設shell為/sbin/nologin的使用者,并統計各類shell下的使用者總數,形如:

bash 3user  they are:

root  redhat centos

 shcount=`cat  /etc/passwd |grep "\<bash\>"|wc -l`

 nocount=`cat  /etc/passwd |grep "\<nologin\>"|wc -l`

user1=`cat  /etc/passwd |grep "bash\>"|cut -d: -f1`

user2=`cat  /etc/passwd |grep "\<nologin\>"|cut -d: -f1`

for  i  in $user1

shuser="$shuser $i"

for  i  in $user2

nouser="$nouser $i"

echo "bash  ${shcount}user  they are :

$shuser"

echo "nologin ${nocount}user they are:

$nouser"

本文轉自biao007h51CTO部落格,原文連結:http://blog.51cto.com/linzb/1734457 ,如需轉載請自行聯系原作者

繼續閱讀