1.模拟linnux登录shell

  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

2.比较两个数大小

  1. #!/bin/bash

  2. echo "please enter two number"

  3. read a

  4. read b

  5. if test $a -eq $b

  6. then echo "NO.1 = NO.2"

  7. elif test $a -gt $b

  8. then echo "NO.1 > NO.2"

  9. else echo "NO.1 < NO.2"

  10. fi

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

  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

4.for循环的使用

  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

5.查看是否当前用户

  1. #!/bin/bash

  2. echo "Please enter a user:"

  3. read a

  4. b=$(whoami)

  5. if test $a = $b

  6. then echo "the user is running."

  7. else echo "the user is not running."

  8. fi

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

  1. #!/bin/bash

  2. for filename in `ls`

  3. do

  4.    if test -d $filename

  5.    then b=0

  6.    else

  7.     a=$(ls -l $filename | awk '{ print $5 }')

  8.      if test $a -eq 0

  9.      then rm $filename

  10.      fi

  11.    fi

  12. done

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

  1. #!/bin/bash

  2. while line=`ls /export/um_lpp_source`

  3. do

  4.    if test $line=""

  5.    then echo "NULL"

  6.      sleep 1

  7.    else echo $line

  8.      chfs -a size=3G /export/um_lpp_source

  9.      exit 0

  10.    fi

  11. done

8.测试IP地址

  1. #!/bin/bash

  2. for i in 1 2 3 4 5 6 7 8 9

  3. do

  4.  echo "the number of $i computer is "

  5.  ping -c 1 192.168.0.$i

  6. done

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

  1. #!/bin/sh

  2. a=2

  3. while name="test.log"

  4. do

  5. sleep 1

  6. b=$(ls -l $name | awk '{print $5}')

  7. if test $b -gt $a

  8. then `cp /opt/*.tar.gz .`

  9. exit 0

  10. fi

  11. done

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

  1. #!/bin/bash

  2. while read name

  3. do

  4. echo $name

  5. done

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

  1. #!/bin/bash

  2. while read line

  3. do

  4. echo $line

  5. done < 0.sh

12.读取a.c中的内容并做加1运算

  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

13.普通无参数函数

  1. #!/bin/bash

  2. p ()

  3. {

  4. echo "hello"

  5. }

  6. p

14.给函数传递参数

  1. #!/bin/bash

  2. p_num ()

  3. {

  4. num=$1

  5. echo $num

  6. }

  7. for n in $@

  8. do

  9. p_num $n

  10. done

15.创建文件夹

  1. #!/bin/bash

  2. while :

  3. do

  4. echo "please input file's name:"

  5. read a

  6. if test -e /root/$a

  7. then

  8. echo "the file is existing Please input new file name:"

  9. else

  10. mkdir $a

  11. echo "you aye sussesful!"

  12. break

  13. fi

  14. done

16.获取本机IP地址

  1. #!/bin/bash

  2. ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'

17.查找最大文件

  1. #!/bin/bash

  2. a=0

  3. for name in *.*

  4. do

  5. b=$(ls -l $name | awk '{print $5}')

  6. if test $b -gt $a

  7. then a=$b

  8. namemax=$name

  9. fi

  10. done

  11. echo "the max file is $namemax"

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

  1. #!/bin/bash

  2. a=1

  3. while :

  4. do

  5. a=$(($a+1))

  6. if test $a -gt 255

  7. then break

  8. else

  9. echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')

  10. ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')

  11. echo $ip >> ip.txt

  12. fi

  13. done

19.打印当前用户

  1. #!/bin/bash

  2. echo "Current User is :"

  3. echo $(ps | grep "$$" | awk '{print $2}')

20.case语句练习

  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

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

  1. #!/bin/bash

  2. clear

  3. echo "enter [y/n]:"

  4. read a

  5. case $a in

  6. y|Y|Yes|YES) echo "you enter $a"

  7. ;;

  8. n|N|NO|no) echo "you enter $a"

  9. ;;

  10. *) echo "error"

  11. ;;

  12. esac

22.内置命令的使用

  1. #!/bin/bash
    clear
    echo "Hello, $USER"
    echo "Today 's date id `date`"
    echo "the user is :"
    who
    echo "this is `uname -s`"
    echo "that's all folks! "


23.打印无密码用户

  1. #/bin/bash

  2. echo "No Password User are :"

  3. echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')


         参考:http://blog.chinaunix.net/uid-28804901-id-3874633.html