Linux shell学习笔记(三)

if语句

基本语法:

  if ....; then

    xxx

  fi

或者

  if ....; then
    ....
  elif ....; then
    ....
  else
    ....
    fi

基本的if条件命令选项有:

  - eq —比较两个参数是否相等(例如,if [ 2 –eq 5 ])
  -ne —比较两个参数是否不相等
  -lt —参数1是否小于参数2
  -le —参数1是否小于等于参数2
  -gt —参数1是否大于参数2
  -ge —参数1是否大于等于参数2
  -f — 检查某文件是否存在(例如,if [ -f "filename" ])
  -d — 检查目录是否存在

  .....

 实例

  1.用户输入文件目录,统计出该目录每种文件的总数。

#sort_file.sh
#!/bin/bash

while :
do
    read -p 'please input your dir: ' dirname
    if [ -z $dirname ];then  #判断用户是否输入为空
        continue
    else
        break
    fi
done

#根据用户的输入,来对该目录下的文件进行分类统计
for i in $(ls $dirname)  
do
        if [ -h $dirname/$i ];then  #统计链接文件
             ((link_file+=1)) 
        elif [ -f  $dirname/$i ];then #统计普通文件 
            ((regular_file+=1))
        elif [ -d  $dirname/$i ];then#统计目录文件
            ((directory_file+=1))
        fi
done

#输出分类文件的数目
echo "the sum of link_file is:$link_file"
echo "the sum of regular_file is:$regular_file"
echo "the sum of directory_file is:$directory_file"

  2.模拟登陆认证

#login.sh
#!/bin/bash

username = 'ceshi'
password ='123'

read -p 'please input your name: ' name  #获取用户名
read -p 'please input your pwd: ' pwd  #获取密码

if [ $name = $username -a $pwd= $password ];then
        echo 'login successfully'
else
       echo 'login failed'
fi

  3.成绩查询

#!/bin/bash

read -p 'your score: ' score

if [ $score -ge 90  ];then
    echo '优秀'
elif [ $score -ge 70 -a $score -lt 90 ];then
    echo '良好'
elif [ $score -ge 60 -a $score -lt 70 ];then
    echo '一般'
elif [ $score -lt 60 ];then
    echo '较差'
fi

 4.获取本机的网络地址

 1 #!/bin/bash
 2 
 3 file="/etc/sysconfig/network-scripts/ifcfg-eth0"
 4  
 5 if [ -f $file ] ;then
 6  
 7     IP=`grep "IPADDR" $file|awk -F"=" '{ print $2 }'`
 8  
 9     MASK=`grep "NETMASK" $file|awk -F"=" '{ print $2 }'`
10  
11     echo "$IP/$MASK"
12  
13     exit 1
14 fi

 

 

for语句

 基本语法:

    for循环有两种形式:

    1.for((i=1;i<=10;i++))

       do

        echo $i

     done

    2.for i in {1..10}

       do

        echo $i

     done

 实例

  1.检测内网可用IP,并将其输入到文件中保存。

 1 #ping.sh
 2 #!/bin/bash
 3 
 4 for i in {1..253}
 5 do
 6     ping -c1 192.168.16.$i &> /dev/null #测试网络是否可以用,并将测试后的信息输入到垃圾堆
 7     if [ $? -eq 0 ];then
 8         echo "192.168.16.$i,successful" 
 9         echo "192.168.16.$i" >> /ip.txt
10     fi
11 done

  2.打印九九乘法表.

 1 #jiujiu.sh
 2 #!/bin/bash
 3 
 4 for ((i=1;i<=9;i++))
 5 do
 6     for ((j=1;j<=i;j++))
 7     do    
 8             echo -n "$i * $j = $[$i*$j]"
 9      done
10      echo
11 done

 

while语句

 基本语法

    while 条件

    do 

      echo 'hello world'

    done

 实例

  1.根据用户的输入来执行cmd命令

 1 #cmd.sh
 2 #!/bin/bash
 3 
 4 user = 'ceshi'
 5 password = '123'
 6 flag=true
 7 
 8 while $flag 
 9 do
10     read -p 'please input your name: ' name #获取用户输入
11     read -p 'please input your password: ' pwd
12     if [ [$name = $user ] ] && [ [ $pwd = $password ] ];then
13         echo "login successfully"
14         while $flag
15          do
16                 read -p '>>:' cmd
17                 if [ [ $cmd = 'q' ] ];then#只要输入不为q,循环就一直进行
18                     flag = false
19                 else
20                     $cmd
21                 fi
22         done
23     fi
24 done

 

  

    

 

posted @ 2017-06-05 14:50  看雪。  阅读(153)  评论(0编辑  收藏  举报