常用 shell 脚本

来源:互联网 发布:windows nginx重启 编辑:程序博客网 时间:2024/04/29 22:24

比较运算: http://blog.csdn.net/ithomer/article/details/6836382

check the current user

#!/bin/bashtest=$(env | grep USER | cut -d "=" -f 2)if [ "$test" == "yu" ] then    echo "the user is yu"fi

test if the disk is full

#!/bin/bashtest=$(df -h |grep sda2 | awk '{print $5}' |cut -d "%" -f 1)if [ "$test" -ge "90" ] then    echo "is full"fi

test if is a directory

#!/bin/bashread -t 30 -p "enter a dir: " dirif [ -d "$dir" ]then     echo "is dir"else     echo "not dir"fi

test if Apache server is up

#!/bin/bashtest=$(ps -aux | grep httpd |grep -v httpd) #the script name should not contain "httpd"if [ -n "$test" ] then    echo "httpd is started" >> correct.logelse    echo "httpd not started" >> error.log    /etc/rc.d/init.d/httpd startfi

make a calculator

#!/bin/bashread -t 30 -p "please input number 1: " num1read -t 30 -p "please input number 2: " num2read -t 30 -p "please input operator: " operif [ -n num1 -a -n num2 -a -n oper ] #all not nullthen    test1=$(echo $num1 | sed 's/[0-9]//g') #substitute number to ''    test2=$(echo $num2 | sed 's/[0-9]//g')    if [ -z "$test1" -a -z "$test2" ] #quotes are needed, test all null    then        if [ "$oper" == "+" ]        then            result=$(($num1 + $num2)) #double ()        elif [ "$oper" == "-" ]        then            result=$(($num1 - $num2))        elif [ "$oper" == "*" ]        then            result=$(($num1 * $num2))        elif [ "$oper" == "/" ]        then            result=$(($num1 / $num2))        else            echo "not valid operator"            exit 10        fi    else        echo "not valid number"        exit 11    fifiecho "result is: $result" 

after execute the script, echo $? to check the exit code

test the file type

#!/bin/bashread -p "please input a file name: " fileif [ -z "$file" ]then    echo "file name is null"    exit 1elif [ ! -e "$file" ]then    echo "file does not exist"    exit 2elif [ -f "$file" ]then    echo "file is a regular file"elif [ -d "$file" ]then    echo "file is a directory"else    echo "file is an other file"fi

case

#!/bin/bashread -p "input yes or no: " -t 30 chocase "$cho" in    "yes")        echo "choose is yes"        ;;    "no")        echo "choose is no"        ;;    *)        echo "choose is not yes or no"        ;;esac

for loop

for parameter in value1 value2 value3    do        procedure    donefor i in 1 2 3 4 5    do        echo $i    done
#!/bin/bashcd /root/testls *.tar.gz > ls.logls *.tgz >> ls.logfor filename in $( cat ls.log ) # $() is used to call linux command, like ``do    tar -zxf $filename &>/dev/nulldonerm -rf ls.log
#!/bin/bashs=0for ((i=1; i<=100; i=i+1))do    s=$(($s+$i))doneecho "sum is: $s"
#!/bin/bashread -t 30 -p "user name: " nameread -t 30 -p "number of users: " numread -t 30 -p "password: " passif [ -n $name -a -n $num -a -n $pass ]then    test_num=$(echo $num |sed 's/[0-9]//g')    if [ -z "$test_num" ]    then        for ((i=1; i<$num; i=i+1))        do            /usr/sbin/useradd $name$i &>/dev/null            echo  $pass | /usr/bin/passwd --stdin $name$i &>/dev/null        done    fifi
#!/bin/bashfor username in $(cat /etc/passwd |grep '/bin/bash' |grep -v root |grep -v yu |cut -d ":" -f 1)do    userdel -r $usernamedone
#!/bin/bashi=0s=0while [ $i -le 100 ]do    s=$(( $s + $i ))    i=$(( $i + 1 ))doneecho "sum: $s"
#!/bin/bashi=0s=0until [ $i -gt 100 ]do    s=$(( $s + $i ))    i=$(( $i + 1 ))doneecho "sum: $s"
0 0