05-流程控制

    xiaoxiao2021-11-22  59

    select

    #!/bin/bash echo "What is your favourite color? " select color in "red" "blue" "green" "white" "black" do break done echo "You have selected $color"

    2.case

    #!/bin/bash echo "please select C to Copy,D to Delete,B to Backup" read op case $op in     C)          echo "your select is Copy"     ;;     D)  echo "your select is Delete"     ;;     B)   echo "your select is Backup"     ;;     *) echo "invalid select" esac

    3.select case

    #!/bin/bash #test select and case echo "a is 5,b is 3,choose one option to calc" a=5 b=3 select op in "a+b" "a-b" "a*b" "a/b" do break done case $op in "a+b") echo 'a+b=' `expr $a "+" $b`;; "a-b") echo 'a-b=' `expr $a "-" $b`;; "a*b") echo 'a*b=' `expr $a "*" $b`;; "a/b") echo 'a/b=' `expr $a "/" $b`;;             *) echo  "input error" esac

    4.for 

    #!/bin/bash #test for for  DAY in MONDAY TUESDAY WENSDAY THUHTHDAY FRIDAY SATARDAY SUNDAY do echo "the day is $DAY" done

    for...pkill踢已经登录的用户

    #!/bin/sh # test for  to kill process username="$1" ps aux | grep $username | awk '{ print $2 }' > /tmp/temp.pid killid=`cat /tmp/temp.pid` for PID in $killid do    /bin/kill -9 $PID 2> /dev/null done

    5.until

    #!/bin/bash #test until until [ -x /home/shell/aa.sh  ] do /bin/ls -l /usr exit 0 done

    read ....until

    #!/bin/bash echo "please input y or Y" read input until [ "$input" = "Y" ] || [ "$input" = "y" ] do  echo "error input ,please try again..."          read input done echo "Stop hear"

    6.shift

    #!/bin/bash if [ $# -le 0] then    echo "no enough parameters"   exit 0 fi sum=0 while [ $# -gt 0 ] do    sum=`expr $sum + $1 `   shift done echo $sum

    7.while

    #!/bin/bash # test while while [ -d /etc ] do     ls -l /etc     break done

    while....calc

    #!/bin/bash num=1 while [ $num -le 10 ]    do SUM=`expr $num "*" $num`         echo $SUM         num=`expr $num "+" 1` done

    8.break和continue

    #!/bin/bash echo "please select C to Copy,D to Delete,B to Backup" while true do read op case $op in     C)          echo "your select is Copy"     ;;     D)          echo "your select is Delete"     ;;     B)         echo "your select is Backup"     ;;     E)         echo "exit......."          break     ;;     *)         echo "invalid select"        continue esac done

    9.批量添加用户

    #!/bin/bash #test batch add user #/etc/passwd echo "please input username" read name echo "please input num" read num n=1 while [ $n -le $num ] do   /usr/sbin/useradd $name$n   n=`expr $n "+" 1` done #/etc/shadow echo "please input password" read passwd m=1 while [ $m -le $num ] do     echo $passwd | passwd --stdin  $name$m     m=`expr $m + 1` done

    10.批量删除用户

    #!/bin/bash #delete users echo "please input username" read name echo "please input num" read num sum=0 while [ $sum -lt $num ] do    sum=`expr $sum + 1`    /usr/sbin/userdel  -r $name$sum done

    11.输入用户名,显示用户的状态

    #!/bin/sh # display user's info /bin/echo "please input username" read username /bin/grep $username /etc/passwd > /dev/null 2> /dev/null if [ $? -eq 0 ] then       echo "username is $username" else       echo "$username is not exist!"       exit 1 fi /bin/echo #list passwd info userinfo=`/bin/grep ^$username:x /etc/passwd` userid=`/bin/echo $userinfo | awk -F : '{print $3}'` groupid=`/bin/echo $userinfo | awk -F : '{print $4}'` homedir=`/bin/echo $userinfo | awk -F : '{print $6}'` shell=`/bin/echo $userinfo  | awk -F : '{print $7}'` #get group name from GID grouptmpname=`cat /etc/group | /bin/grep :x:$groupid` groupname=`/bin/echo $grouptmpname | awk -F : '{print $1}'` echo "user id is :$userid" echo "default group is $groupname" echo "home directory is $homedir" echo "shell is $shell" echo "group members info:" # get grop members groups=`/usr/bin/groups $username` /bin/echo $groups /bin/echo #get login info userlogin=`/usr/bin/who | /bin/grep $username` if [ "$userlogin" != "" ] then      echo "$username is online!" else echo "$username is not logged in" fi

    12.输入参数比较大小

    #!/bin/sh if [ $# -ne 2 ];    then "only allowed 2 parameters!"    exit 0 fi if [ $1 -eq $2 ];    then echo "$1=$2" elif [ $1 -lt $2 ] ;    then echo "$1<$2" elif [ $1 -gt $2 ];    then echo "$1>$2"  fi

    13.函数,函数中没有局部变量,只有全局变量。

    #!/bin/bash HELP(){    echo "useage: sh function \$1 \$2 \$3" } if [ $# -ne 3 ]  then HELP else  echo "thank for you input,the three argument is 1 2 3" fi

    转载请注明原文地址: https://ju.6miu.com/read-678454.html

    最新回复(0)