bash

来源:互联网 发布:png转ico软件 编辑:程序博客网 时间:2024/06/06 05:50

转自http://blog.csdn.net/jb19900111/article/details/13507815


自己写了一下小的shell实例,虽然很小,但所有的大的程序都是由小的模块堆积起来的,程序员一定要懂得一种脚本的书写,而我,只会在Linux下工作,所以就只能写linux的shell脚本了,呵呵,本文会陆续更新,给自己加油!

1.模拟linnux登录shell

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash    ##! /bin/sh 是指此脚本使用/bin/sh来解释执行,#!是特殊的表示符,其后面根的是此解释此脚本的shell的路径。
  2. echo -n “login:”   #echo -n 不换行输出
  3. read name   #read命令接收标准输入(键盘)的输入,或者其他文件描述符的输入。得到输入后,read命令将数据放入一个标准变量中。
  4. #在上面read后面的变量只有name一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量。如果输入数据个数过多,则最后剩下的所有值都给最后一个变量,如果太少输入不会结束。
  5. echo -n “password:”  
  6. read passwd  
  7. if [ name = "cht" -a passwd = “abc” ];then  #使用一个定义过的变量,只要在变量名前面加美元符号($),=判断字符串是否相等,[  ….  -a  ….. ]  相当于 “与”   -o 表示 或
  8. echo “the host and password is right!”  
  9. else echo “input is error!”  
  10. fi  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. echo -n “login:”   
  3. read name  
  4. echo -n “password:”  
  5. read passwd  
  6. if [ name = "cht" -a passwd = “abc” ];then  
  7. echo “the host and password is right!”  
  8. else echo “input is error!”  
  9. fi  
#!/bin/bashecho -n "login:" read nameecho -n "password:"read passwdif [ $name = "cht" -a $passwd = "abc" ];thenecho "the host and password is right!"else echo "input is error!"fi

2.比较两个数大小

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. echo “please enter two number”  
  3. read a  
  4. read b  
  5. if test a -eq b   #test 整数1 –eq 整数2                        整数相等 
  6. then echo “NO.1 = NO.2”  
  7. elif test a -gt b  #test 整数1 –gt 整数2                         整数1大于整数2 
  8. then echo “NO.1 > NO.2”  
  9. else echo “NO.1 < NO.2”   
  10. fi  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. echo “please enter two number”  
  3. read a  
  4. read b  
  5. if test a&nbsp;-eq&nbsp;b  
  6. then echo “NO.1 = NO.2”  
  7. elif test a&nbsp;-gt&nbsp;b  
  8. then echo “NO.1 > NO.2”  
  9. else echo “NO.1 < NO.2”   
  10. fi  
#!/bin/bashecho "please enter two number"read aread bif test $a -eq $bthen echo "NO.1 = NO.2"elif test $a -gt $bthen echo "NO.1 > NO.2"else echo "NO.1 < NO.2" fi

3.查找/root/目录下是否存在该文件

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. echo “enter a file name:”  
  3. read a  
  4. if test  -e /root/$a   #test –e File                                           文件是否存在 (常用)
  5. then echo “the file is exist!”  
  6. else echo “the file is not exist!”  
  7. fi  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. echo “enter a file name:”  
  3. read a  
  4. if test  -e /root/$a   
  5. then echo “the file is exist!”  
  6. else echo “the file is not exist!”  
  7. fi  
#!/bin/bashecho "enter a file name:"read aif test  -e /root/$a then echo "the file is exist!"else echo "the file is not exist!"fi

4.for循环的使用

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. clear  #刷新屏幕
  3. for num in 1 2 3 4 5 6 7 8 9 10   #列表是一组值(数字、字符串等)组成的序列,每个值通过空格分隔。每循环一次,就将列表中的下一个值赋给变量num
  4. do  
  5.     echo ”$num”  
  6. done  

[plain] view plain copy
print?
  1. #!/bin/bash  
  2. clear  
  3. for num in 1 2 3 4 5 6 7 8 9 10  
  4. do  
  5.     echo ”$num”  
  6. done  
#!/bin/bashclearfor num in 1 2 3 4 5 6 7 8 9 10do    echo "$num"done

5..查看是否当前用户

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. echo “Please enter a user:”  
  3. read a  
  4. b=(whoami) &nbsp;#使用一个定义过的变量,只要在变量名前面加美元符号()即可
  5. if test a&nbsp;=&nbsp;b  #test 字符串1=字符串2                    字符串是否相等,若相等返回true 
  6. then echo “the user is running.”  
  7. else echo “the user is not running.”  
  8. fi  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. echo “Please enter a user:”  
  3. read a  
  4. b=(whoami)&nbsp;&nbsp;</span></li><li class="alt"><span>if&nbsp;test&nbsp;a = $b  
  5. then echo “the user is running.”  
  6. else echo “the user is not running.”  
  7. fi  
#!/bin/bashecho "Please enter a user:"read ab=$(whoami)if test $a = $bthen echo "the user is running."else echo "the user is not running."fi

6.删除当前目录下大小为0的文件

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. for filename in `ls`  
  3. do  
  4.     if test -d filename #<span style="font-family:'microsoft yahei'; line-height:35px">test –d File &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 文件并且是目录&nbsp;</span></span></li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;b=0&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#ff0000">&nbsp;&nbsp;&nbsp;a=(ls -l filename&nbsp;|&nbsp;awk&nbsp;'{&nbsp;print&nbsp;$5&nbsp;}')&nbsp;&nbsp;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size:10px">&nbsp;&nbsp;&nbsp;if&nbsp;test&nbsp;a -eq 0   #test 整数1 –eq 整数2                        整数相等 
  5.              then rm $filename  
  6.              fi  
  7.         fi        
  8. done  

[plain] view plain copy
print?
  1. #!/bin/bash  
  2. for filename in `ls`  
  3. do  
  4.     if test -d filename&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;b=0&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a=(ls -l filename&nbsp;|&nbsp;awk&nbsp;'{&nbsp;print&nbsp;$5&nbsp;}')&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;test&nbsp;a -eq 0  
  5.              then rm $filename  
  6.              fi  
  7.         fi        
  8. done  
#!/bin/bashfor filename in `ls`do    if test -d $filename    then b=0    else           a=$(ls -l $filename | awk '{ print $5 }')            if test $a -eq 0             then rm $filename             fi        fi      done

7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1.  #!/bin/bash  
  2. while line=`ls /export/um_lpp_source`  //把目录/export/um_lpp_source下的文件赋给line变量
  3. do  
  4.     if test line="<span style="font-size:10px">" &nbsp;#<span style="color:rgb(85,85,85); font-family:'microsoft yahei'; line-height:35px">if test &nbsp;(表达式为真)&nbsp;</span></span></li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;&nbsp;echo&nbsp;"NULL" &nbsp;#输出字符串NULL</li><li>&nbsp;&nbsp;&nbsp;&nbsp;sleep&nbsp;1<span style="font-size:10px"> &nbsp;#<em>sleep 1</em> 睡眠1秒</span></li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;echo&nbsp;line  
  5.     chfs -a size=3G /export/um_lpp_source   #把/export/um_lpp_source的大小改为3G
  6.     exit 0  
  7.     fi  
  8. done  

[plain] view plain copy
print?
  1.  #!/bin/bash  
  2. while line=`ls /export/um_lpp_source`  
  3. do  
  4.     if test line=""&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;&nbsp;echo&nbsp;"NULL"&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;sleep&nbsp;1&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;echo&nbsp;line  
  5.     chfs -a size=3G /export/um_lpp_source  
  6.     exit 0  
  7.     fi  
  8. done  
 #!/bin/bashwhile line=`ls /export/um_lpp_source`do    if test $line=""    then  echo "NULL"    sleep 1    else echo $line    chfs -a size=3G /export/um_lpp_source    exit 0    fidone

8.测试IP地址

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. for i in  1 2 3 4 5 6 7 8 9   
  3. do  
  4.     echo “the number of i&nbsp;computer&nbsp;is&nbsp;"&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;ping&nbsp;-c&nbsp;1&nbsp;192.168.0.i  #-c指定要求完成的回应次数
  5. done  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. for i in  1 2 3 4 5 6 7 8 9   
  3. do  
  4.     echo “the number of i&nbsp;computer&nbsp;is&nbsp;"&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;ping&nbsp;-c&nbsp;1&nbsp;192.168.0.i  
  5. done  
#!/bin/bashfor i in  1 2 3 4 5 6 7 8 9 do    echo "the number of $i computer is "    ping -c 1 192.168.0.$idone

9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件拷贝到当前目录下

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1.  #!/bin/sh  
  2. a=2  
  3. while name=”test.log”  
  4. do  
  5.    sleep 1  #sleep 1 睡眠1秒
  6.    b=(ls&nbsp;-l&nbsp;name | awk ’{print 5}')&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;if&nbsp;test&nbsp;b -gt $a  #test 整数1 –gt 整数2                         整数1大于整数2 
  7.    then `cp /opt/*.tar.gz .`  
  8.    exit 0  
  9.    fi  
  10. done  
[plain] view plain copy
print?
  1.  #!/bin/sh  
  2. a=2  
  3. while name=”test.log”  
  4. do  
  5.    sleep 1  
  6.    b=(ls&nbsp;-l&nbsp;name | awk ’{print 5}')&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;if&nbsp;test&nbsp;b -gt $a  
  7.    then `cp /opt/*.tar.gz .`  
  8.    exit 0  
  9.    fi  
  10. done  
 #!/bin/sha=2while name="test.log"do   sleep 1   b=$(ls -l $name | awk '{print $5}')   if test $b -gt $a   then `cp /opt/*.tar.gz .`   exit 0   fidone

10.打印读取的内容,为下面的例子做准备

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. while read name  
  3. do  
  4. echo $name  
  5. done  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. while read name  
  3. do  
  4. echo $name  
  5. done  
#!/bin/bashwhile read namedoecho $namedone

11.从0.sh中读取内容并打印

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. while read line  
  3. do  
  4.     echo $line  
  5. done < 0.sh  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. while read line  
  3. do  
  4.     echo $line  
  5. done < 0.sh  
#!/bin/bashwhile read linedo    echo $linedone < 0.sh
12.读取a.c中的内容并做加1运算

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. test -e a.c  #test –e File                                           文件是否存在 (常用)
  3. while read line  
  4. do  
  5.   a=((line+1))  #内容做加1运算
  6. done < a.c  
  7. echo $a  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. test -e a.c  
  3. while read line  
  4. do  
  5.   a=((line+1))  
  6. done < a.c  
  7. echo $a  
#!/bin/bashtest -e a.cwhile read linedo  a=$(($line+1))done < a.cecho $a

13.普通无参数函数

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. ()  
  3. {  
  4.     echo “hello”  
  5. }  
  6. p  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. p ()  
  3. {  
  4.     echo “hello”  
  5. }  
  6. p  
#!/bin/bashp (){    echo "hello"}p

14.给函数传递参数

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. p_num ()  
  3. {  
  4.     num=1&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;num  
  5. }  
  6. for n in @ &nbsp; #输入参数到函数中</li><li>do&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;p_num&nbsp;n  
  7. done  


[plain] view plain copy
print?
  1. #!/bin/bash  
  2. p_num ()  
  3. {  
  4.     num=1&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;num  
  5. }  
  6. for n in @&nbsp;&nbsp;</span></li><li class=""><span>do&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;p_num&nbsp;n  
  7. done  
#!/bin/bashp_num (){    num=$1    echo $num}for n in $@do    p_num $ndone

15.创建文件夹

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. while :  
  3. do  
  4.     echo “please input file’s name:”  
  5.     read a  
  6.     if test -e /root/a &nbsp;<span style="font-size:10px">#<span style="color:rgb(85,85,85); font-family:'microsoft yahei'; line-height:35px">test –e File &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 文件是否存在 (常用)</span></span></li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"the&nbsp;file&nbsp;is&nbsp;existing&nbsp;Please&nbsp;input&nbsp;new&nbsp;file&nbsp;name:"&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mkdir&nbsp;a  
  7.         echo “you aye sussesful!”  
  8.         break   
  9.     fi  
  10. done  

[plain] view plain copy
print?
  1. #!/bin/bash  
  2. while :  
  3. do  
  4.     echo “please input file’s name:”  
  5.     read a  
  6.     if test -e /root/a&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"the&nbsp;file&nbsp;is&nbsp;existing&nbsp;Please&nbsp;input&nbsp;new&nbsp;file&nbsp;name:"&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mkdir&nbsp;a  
  7.         echo “you aye sussesful!”  
  8.         break   
  9.     fi  
  10. done  
#!/bin/bashwhile :do    echo "please input file's name:"    read a    if test -e /root/$a    then         echo "the file is existing Please input new file name:"    else        mkdir $a        echo "you aye sussesful!"        break     fidone

16.获取本机IP地址

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. ifconfig | grep “inet addr:” | awk ’{ print $2 }’| sed ‘s/addr://g’  


[plain] view plain copy
print?
  1. #!/bin/bash  
  2. ifconfig | grep “inet addr:” | awk ’{ print $2 }’| sed ‘s/addr://g’  
#!/bin/bashifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'

17.查找最大文件

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. a=0  
  3. for  name in *.*  
  4. do  
  5.     b=(ls&nbsp;-l&nbsp;name | awk ’{print 5}')&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;test&nbsp;b -gt a&nbsp;&nbsp;<span style="font-size:10px">#</span><span style="font-size:10px">test 整数1 –gt 整数2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 整数1大于整数2&nbsp;</span></li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;a=b  #更改比较值
  6.     namemax=name&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;fi&nbsp;&nbsp;</li><li>done&nbsp;&nbsp;</li><li class="alt">echo&nbsp;"the&nbsp;max&nbsp;file&nbsp;is&nbsp;namemax”  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. a=0  
  3. for  name in *.*  
  4. do  
  5.     b=(ls&nbsp;-l&nbsp;name | awk ’{print 5}')&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;test&nbsp;b -gt a&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;a=b  
  6.     namemax=name&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;fi&nbsp;&nbsp;</span></li><li class=""><span>done&nbsp;&nbsp;</span></li><li class="alt"><span>echo&nbsp;"the&nbsp;max&nbsp;file&nbsp;is&nbsp;namemax”  
#!/bin/basha=0for  name in *.*do    b=$(ls -l $name | awk '{print $5}')    if test $b -gt $a    then a=$b    namemax=$name    fidoneecho "the max file is $namemax"

18.查找当前网段内IP用户,重定向到ip.txt文件中

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. a=1  
  3. while :  
  4. do  
  5.     a=((a+1))  #从1开始不断地向上加,直到等于255时退出
  6.     if test a&nbsp;-gt&nbsp;255 &nbsp;<span style="font-size:10px">#test 整数1 –gt 整数2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 整数1大于整数2&nbsp;</span></li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;break&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;(ping -c 1 192.168.0.a&nbsp;|&nbsp;grep&nbsp;"ttl"&nbsp;|&nbsp;awk&nbsp;'{print&nbsp;$4}'|&nbsp;sed&nbsp;'s/://g') &nbsp;#-c 后面带发送次数 ,sed&nbsp;'s/://g'是吧空格全部替换为\</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip=(ping -c 1 192.168.0.a&nbsp;|&nbsp;grep&nbsp;"ttl"&nbsp;|&nbsp;awk&nbsp;'{print&nbsp;$4}'|&nbsp;sed&nbsp;'s/://g')&nbsp;&nbsp;</li><li class="alt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;ip >> ip.txt  
  7.     fi  
  8. done  


TTL,存活时间TTL是IP协议包中的一个值,它告诉网络路由器包在网络中的时间是否太长而应被丢

[plain] view plain copy
print?
  1. #!/bin/bash  
  2. a=1  
  3. while :  
  4. do  
  5.     a=((a+1))  
  6.     if test a&nbsp;-gt&nbsp;255&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;break&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;(ping -c 1 192.168.0.a&nbsp;|&nbsp;grep&nbsp;"ttl"&nbsp;|&nbsp;awk&nbsp;'{print&nbsp;$4}'|&nbsp;sed&nbsp;'s/://g')&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ip=(ping -c 1 192.168.0.a&nbsp;|&nbsp;grep&nbsp;"ttl"&nbsp;|&nbsp;awk&nbsp;'{print&nbsp;$4}'|&nbsp;sed&nbsp;'s/://g')&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;ip >> ip.txt  
  7.     fi  
  8. done  
#!/bin/basha=1while :do    a=$(($a+1))    if test $a -gt 255    then break    else        echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')        ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')        echo $ip >> ip.txt    fidone

19.打印当前用户

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. echo “Current User is :”  
  3. echo (ps&nbsp;|&nbsp;grep&nbsp;""&nbsp;|&nbsp;awk&nbsp;'{print&nbsp;2}’)  #ps显示瞬间行程 (process) 的动态,
    </em>是当前进程号,就是你的shell的进程号</li></ol>
    <div class="save_code tracking-ad" style=""><a target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt=""></a></div>
    <div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div>
    <div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><b>[plain]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span class="tracking-ad" data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 185px; top: 9618px; width: 15px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_38" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="15" height="16" name="ZeroClipboardMovie_38" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=38&amp;width=15&amp;height=16" wmode="transparent"></div></span><span class="tracking-ad" data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1"><li class="alt"><span><span>#!/bin/bash&nbsp;&nbsp;</span></span></li><li class=""><span>echo&nbsp;"Current&nbsp;User&nbsp;is&nbsp;:"&nbsp;&nbsp;</span></li><li class="alt"><span>echo&nbsp;$(ps&nbsp;|&nbsp;grep&nbsp;"
    ” | awk ’{print $2}’)  
#!/bin/bashecho "Current User is :"echo $(ps | grep "$$" | awk '{print $2}')

20.case语句练习

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. clear  
  3. echo “enter a number from 1 to 5:”  
  4. read num  
  5. case $num in  
  6.     1) echo “you enter 1”  
  7.     ;;  
  8.     2) echo “you enter 2”  
  9.     ;;  
  10.     3) echo “you enter 3”  
  11.     ;;  
  12.     4) echo “you enter 4”  
  13.     ;;  
  14.     5) echo “you enter 5”  
  15.     ;;  
  16.     *) echo “error”  #如果无一匹配模式,使用星号 * 捕获该值,再执行后面的命令
  17.     ;;  
  18. esac  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. clear  
  3. echo “enter a number from 1 to 5:”  
  4. read num  
  5. case $num in  
  6.     1) echo “you enter 1”  
  7.     ;;  
  8.     2) echo “you enter 2”  
  9.     ;;  
  10.     3) echo “you enter 3”  
  11.     ;;  
  12.     4) echo “you enter 4”  
  13.     ;;  
  14.     5) echo “you enter 5”  
  15.     ;;  
  16.     *) echo “error”  
  17.     ;;  
  18. esac  
#!/bin/bashclearecho "enter a number from 1 to 5:"read numcase $num in    1) echo "you enter 1"    ;;    2) echo "you enter 2"    ;;    3) echo "you enter 3"    ;;    4) echo "you enter 4"    ;;    5) echo "you enter 5"    ;;    *) echo "error"    ;;esac

21.yes/no返回不同的结构

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. clear  
  3. echo “enter [y/n]:”  
  4. read a  
  5. case a&nbsp;in &nbsp;#调用变量a</li><li>&nbsp;&nbsp;&nbsp;&nbsp;y|Y|Yes|YES)&nbsp;echo&nbsp;"you&nbsp;enter&nbsp;a”  
  6.     ;;  #;; 与其他语言中的 break 类似,意思是跳到整个 case 语句的最后。
  7.     n|N|NO|no) echo “you enter $a”  
  8.     ;;  
  9.     *) echo “error”  
  10.     ;;  
  11. esac  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. clear  
  3. echo “enter [y/n]:”  
  4. read a  
  5. case a&nbsp;in&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;y|Y|Yes|YES)&nbsp;echo&nbsp;"you&nbsp;enter&nbsp;a”  
  6.     ;;  
  7.     n|N|NO|no) echo “you enter $a”  
  8.     ;;  
  9.     *) echo “error”  
  10.     ;;  
  11. esac  
#!/bin/bashclearecho "enter [y/n]:"read acase $a in    y|Y|Yes|YES) echo "you enter $a"    ;;    n|N|NO|no) echo "you enter $a"    ;;    *) echo "error"    ;;esac

22.内置命令的使用

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. clear  #清除终端屏幕
  3. echo “Hello, USER" &nbsp;#USER是环境变量
  4. echo “Today ‘s date id `date`”  #显示命令date
  5. echo “the user is :”  
  6. who  #who命令能够打印 当前都有谁登录到系统中 的相关信息 
  7. echo “this is `uname -s`”  #uname -s`打印当前系统相关信息,-s或–sysname:显示操作系统名称
  8. echo “that’s all folks! ”  
[plain] view plain copy
print?
  1. #!/bin/bash  
  2. clear  
  3. echo “Hello, $USER”  
  4. echo “Today ‘s date id `date`”  
  5. echo “the user is :”  
  6. who  
  7. echo “this is `uname -s`”  
  8. echo “that’s all folks! ”  
#!/bin/bashclearecho "Hello, $USER"echo "Today 's date id `date`"echo "the user is :"whoecho "this is `uname -s`"echo "that's all folks! "

23.打印无密码用户

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash  
  2. echo “No Password User are :”  
  3. echo (cat&nbsp;/etc/shadow&nbsp;|&nbsp;grep&nbsp;"!!"&nbsp;|&nbsp;awk&nbsp;'BEGIN&nbsp;{&nbsp;FS=":"&nbsp;}{print&nbsp;1}’)  #BEGIN{ 这里面放的是执行前的语句 },从也就是第1个参数