shell腳本-條件和循環(huán)

一. 條件

1. 判斷用戶是不是root

#!/bin/bash

test=$(env | grep USER | cut -d "=" -f 2)

if [ "$test" == "root" ]; then
    echo current login user is root
fi

if [ "$test" == "root" ]
    then
    echo i am root
fi

2. 判斷磁盤容量大小

#!/bin/bash

rate=`df -h | grep "sdb1" | awk '{print $5}' | cut -d "%" -f 1`

echo $rate

if [ $rate -ge 4 ]
    then
        echo / if full
fi

3. 判斷是不是目錄

#!/bin/bash

read -t 30 -p "please input a dir: " dir

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

4. 判斷httpd運行狀態(tài), 如果stop, 則start

#!/bin/bash

test=`ps -ef | grep httpd | grep -v grep`

if [ -n "$test" ]
    then
        echo "httpd is start"
    else
        echo 'httpd is stop'
        /etc/init.d/httpd start
fi

5. shell計算器

#!/bin/bash

read -t 30 -p 'please input first num: ' num1
read -t 30 -p 'please input second num: ' num2

read -t 30 -p 'please input operator: ' ope

sum=''
if [ -n "$num1" -a -n "$num2" -a -n "$ope" ]
    then
    # 判斷是不是數值
    test1=`echo $num1 | sed 's/[0-9]//g'`
    test2=`echo $num2 | sed 's/[0-9]//g'`
    if [ -n "$test1" -o -n "$test2" ]
        then
        echo "invalid number"
        exit 11
    else
        if [ "$ope" == "+" ]
            then
            sum=$(($num1 + $num2))
        elif [ "$ope" == "-" ]
            then
            sum=$(($num1 - $num2))
        elif [ "$ope" == "*" ]
            then
            sum=$(($num1 * $num2))
        elif [ "$ope" == "/" ]
            then
            sum=$(($num1 / $num2))
        else
            echo "invalid operator"
            exit 12
        fi
    fi
else
    echo "invilid number or operator"
    exit 10
fi

if [ -n "$sum" ]
    then
    echo "$num1 $ope $num2 = $sum"
fi

6. case練習

#!/bin/bash

read -t 30 -p "are you sure [y|n] : " choose

case "$choose" in
    # 正則匹配
    Y|y|[Y|y])
        echo "yes"
        ;;
    "n")
        echo "no"
        ;;
    *)
        echo "other"
        ;;
esac

二. 循環(huán)

1. 批量解壓縮

#!/bin/bash

cd /root/tar
echo $?;

ls *.tar.gz > ls.log
ls *.tgz >> ls.log

files=`cat ls.log`
echo $files
for file in $files
    do
        tar -zxf $file &> /dev/null
    done

rm -rf ls.log

2. 求和

#!/bin/bash

s=0;

for(( i=1; i<=100; i=i+1))
    do
        s=$(( $s + $i))
    done
echo $s

3. 批量添加用戶, 并賦予初始密碼

#!/bin/bash

read -t 30 -p "please input username-prefix: " user
read -t 30 -p "please input count: " count
read -t 30 -p "please passwd: " passwd

if [ -z "$user" -o -z "$count" -o -z "$passwd" ]
    then
    echo "invalid param"
    exit 13
fi

test_count=`echo $count | sed 's/[0-9]//g'`
if [ -z "$test_count" ]
    then
    # 添加用戶
    for(( i=1;i<=$count;i=i+1 ))
        do
            /usr/sbin/useradd $user$i &> /dev/null
            echo $passwd | /usr/bin/passwd --stdin $name$i &> /dev/null
        done
fi

4. 批量刪除用戶

#!/bin/bash

users=$( cat /etc/passwd | grep adduser | cut -d ":" -f 1 )
#users=`cat /etc/passwd | grep /bin/bash | grep -v root`

for user in $users
    do
       /usr/sbin/userdel -r $user
    done

5. while循環(huán)

#!/bin/bash

# 求兩個數的累加和

read -t 30 -p "please start num: " start_num
read -t 30 -p "please end num: " end_num

sum=0

while [ $start_num -le $end_num ]
    do
        sum=$(( $start_num + $sum ))
        start_num=$(( $start_num + 1 ))
    done

echo $sum

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 最近開始學習Linux一些基礎的知識,其中就有關于shell腳本的,今天不是要寫什么高大上的東西,是給和我一樣的初...
    我在鄱陽湖邊閱讀 776評論 0 0
  • 本文全面系統(tǒng)地介紹了shell腳本調試技術,包括使用echo, tee, trap等命令輸出關鍵信息,跟蹤變量的值...
    liuzg0734閱讀 1,000評論 0 14
  • 第一次仔細觀察它,是在一天早上,那天陽光明媚,準備和老公一起帶孩子出去走走。 老公帶著大寶在街上吃早餐,我?guī)е?..
    桃灼妖妖閱讀 393評論 0 1
  • 以前的段子講:985是軟臥,211是硬臥,普通一本二本是硬座,三本是站票,??茢D廁所,其他的趴火車頂上。只要到了目...
    Crazy夢溪閱讀 419評論 0 0
  • 你用顏色和字母宣告了你的速度 綠色不加任何字母的是慢車 那個慢悠悠的速度 像是疲憊不堪的老人 走走停停 綠色加字母...
    朵先森閱讀 140評論 0 1

友情鏈接更多精彩內容