且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

bash编程之五 字符串测试以及for循环

更新时间:2022-09-15 22:44:31

字符测试:

==:测试是否相等,相等为真,否则为假

!=:测试是否不等,不等为真,否则为假

>或<:将字符串转化为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!"

fi


练习:

传递3个参数给脚本,第一个为整数,第二个为算数运算符,第三个为整数,将计算结果显示出来,并保留两位精度。

#!/bin/bash

if [ $# -ne  3 ];then 

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

exit 7

fi

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

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


练习:

传递参数给脚本,参数均为用户名,把passwd里面记录的此账号的相关信息提取保存到/tmp/user.txt中,并要求有行号。

#!/bin/bash

if [ $# -lt  1 ];then 

echo  "Usage: `basename $0` ARG  "

exit 7

fi

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

echo "$1  not exists!"

else

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公司

否则,就显示其为非主流公司

#!/bin/bash

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"

fi


练习:

写一个脚本,给脚本传递三个整数并显示其最大值和最小值。

#!/bin/bash

if [ $# -ne 3 ];then

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

exit 1

fi


case $1  in

[[:digit:]]*) 

;;

*)

echo "ARG1 must be the  integer!"

exit 7

;;

esac

case $2  in

[[:digit:]]*) 

;;

*)

echo "ARG2 must be the  integer!"

exit 7

;;

esac

case $3  in

[[:digit:]]*) 

;;

*)

echo "ARG3 must be the  integer!"

exit 7

;;

esac

MAX=0

MIN=$1

for i in $1 $2 $3;do

if [ $MAX -lt $i ]; then

MAX=$i

fi

if [ $MIN -gt $i ];then

MIN=$i

fi

done

echo  "MAX:$MAX MIN:$MIN"


写一个脚本:

1、设定变量FILE的值为/etc/passwd

2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,例如:

    hello,root,your shell :/bin/bash

3、统计一共有多少个用户

#!/bin/bash

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"

done

echo "there are $count users"


写一个脚本:

1添加10个用户user1到user10,但要求只有用户不存在的情况才添加

#!/bin/bash

for i  in  `seq 1 10 `;do

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

echo "user$i exists!!"

else 

useradd user$i

echo  "useradd user$i finished !"

fi

done


写一个脚本:

计算100以内所有能被3整除的正整数的和;

#!/bin/bash

declare -i sum=0

for  i in  {1..100}

do

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

                sum=$[$sum+$i]

        fi

done

echo "sum : $sum "


写一个脚本:计算100以内所有奇数的和以及所有偶数的和

#!/bin/bash

declare -i oddsum=0

declare -i evensum=0

for  i  in  `seq 1 100`

do

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

evensum=$[$evensum+$i]

else

oddsum=$[$oddsum+$i]

fi

done

echo  "oddsum:$oddsum 

evensum:$evensum"


写一个脚本分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数,形如:

bash 3user  they are:

root  redhat centos


#!/bin/bash

 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

do

shuser="$shuser $i"

done

for  i  in $user2

do

nouser="$nouser $i"

done

echo "bash  ${shcount}user  they are :

$shuser"

echo "nologin ${nocount}user they are:

$nouser"


















本文转自biao007h51CTO博客,原文链接:http://blog.51cto.com/linzb/1734457 ,如需转载请自行联系原作者