常用 shell 脚本

比较运算: http://blog.youkuaiyun.com/ithomer/article/details/6836382

check the current user

#!/bin/bash

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

test if the disk is full

#!/bin/bash

test=$(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/bash

read -t 30 -p "enter a dir: " dir

if [ -d "$dir" ]
then 
    echo "is dir"
else 
    echo "not dir"
fi

test if Apache server is up

#!/bin/bash

test=$(ps -aux | grep httpd |grep -v httpd) #the script name should not contain "httpd"

if [ -n "$test" ] 
then
    echo "httpd is started" >> correct.log
else
    echo "httpd not started" >> error.log
    /etc/rc.d/init.d/httpd start
fi

make a calculator

#!/bin/bash

read -t 30 -p "please input number 1: " num1
read -t 30 -p "please input number 2: " num2
read -t 30 -p "please input operator: " oper

if [ -n num1 -a -n num2 -a -n oper ] #all not null
then
    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
    fi
fi
echo "result is: $result" 

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

test the file type

#!/bin/bash

read -p "please input a file name: " file

if [ -z "$file" ]
then
    echo "file name is null"
    exit 1
elif [ ! -e "$file" ]
then
    echo "file does not exist"
    exit 2
elif [ -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/bash

read -p "input yes or no: " -t 30 cho

case "$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
    done

for i in 1 2 3 4 5
    do
        echo $i
    done
#!/bin/bash

cd /root/test
ls *.tar.gz > ls.log
ls *.tgz >> ls.log

for filename in $( cat ls.log ) # $() is used to call linux command, like ``
do
    tar -zxf $filename &>/dev/null

done
rm -rf ls.log
#!/bin/bash

s=0
for ((i=1; i<=100; i=i+1))
do
    s=$(($s+$i))
done
echo "sum is: $s"
#!/bin/bash

read -t 30 -p "user name: " name
read -t 30 -p "number of users: " num
read -t 30 -p "password: " pass

if [ -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
    fi
fi
#!/bin/bash

for username in $(cat /etc/passwd |grep '/bin/bash' |grep -v root |grep -v yu |cut -d ":" -f 1)
do
    userdel -r $username
done
#!/bin/bash

i=0
s=0

while [ $i -le 100 ]
do
    s=$(( $s + $i ))
    i=$(( $i + 1 ))
done
echo "sum: $s"
#!/bin/bash

i=0
s=0

until [ $i -gt 100 ]
do
    s=$(( $s + $i ))
    i=$(( $i + 1 ))
done
echo "sum: $s"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值