Linux簡單的shell腳本實例

一些簡單的shell實例,不積硅步,無以至千里,不積小流,無以成江河!

1.模擬linux登錄shell

#!/bin/bash
echo -n "login:" 
read name
echo -n "password:"
read passwd
if [ $name = "syp" -a $passwd = "syp" ];then
echo "the host and password is right!"
else echo "input is error!"
fi

2.比較兩個數大小

#!/bin/bash
echo "please enter two number"
read a
read b
if test $a -eq $b
then echo "NO.1 = NO.2"
elif test $a -gt $b
then echo "NO.1 > NO.2"
else echo "NO.1 < NO.2" 
fi

3.查找/root/目錄下是否存在該文件

#!/bin/bash
echo "enter a file name:"
read a
if test  -e /root/$a 
then echo "the file is exist!"
else echo "the file is not exist!"
fi

4.for循環的使用

#!/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
    echo "$num"
done

5..查看是否當前用戶

#!/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running."
else echo "the user is not running."
fi

6.刪除當前目錄下大小爲0的文件

#!/bin/bash
for filename in `ls`
do
    if test -d $filename
    then b=0
    else    
       a=$(ls -l $filename | awk '{ print $5 }')
            if test $a -eq 0
             then rm $filename
             fi
        fi      
done

7.如果/export/um_lpp_source下有文件,那麼將其文件系統大小改爲3G

 #!/bin/bash
while line=`ls /export/um_lpp_source`
do
    if test $line=""
    then  echo "NULL"
    sleep 1
    else echo $line
    chfs -a size=3G /export/um_lpp_source
    exit 0
    fi
done

8.測試IP地址

#!/bin/bash
for i in  1 2 3 4 5 6 7 8 9 
do
    echo "the number of $i computer is "
    ping -c 1 192.168.0.$i
done

9.如果test.log的大小大於0,那麼將/opt目錄下的*.tar.gz文件拷貝到當前目錄下

 #!/bin/sh
a=2
while name="test.log"
do
   sleep 1
   b=$(ls -l $name | awk '{print $5}')
   if test $b -gt $a
   then `cp /opt/*.tar.gz .`
   exit 0
   fi
done

10.打印讀取的內容,爲下面的例子做準備

#!/bin/bash
while read name
do
echo $name
done

11.從0.sh中讀取內容並打印

#!/bin/bash
while read line
do
    echo $line
done < 0.sh

12.讀取a.c中的內容並做加1運算

#!/bin/bash
test -e a.c
while read line
do
  a=$(($line+1))
done < a.c
echo $a

13.普通無參數函數

#!/bin/bash
p ()
{
    echo "hello"
}
p

14.給函數傳遞參數

#!/bin/bash
p_num ()
{
    num=$1
    echo $num
}
for n in $@
do
    p_num $n
done

15.創建文件夾

#!/bin/bash
while :
do
    echo "please input file's name:"
    read a
    if test -e /root/$a
    then
         echo "the file is existing Please input new file name:"
    else
        mkdir $a
        echo "you aye sussesful!"
        break 
    fi
done

16.獲取本機IP地址

#!/bin/bash
ifconfig | grep "inet addr:" | awk '{ print $2 }'| sed 's/addr://g'

17.查找最大文件

#!/bin/bash
a=0
for  name in *.*
do
    b=$(ls -l $name | awk '{print $5}')
    if test $b -gt $a
    then a=$b
    namemax=$name
    fi
done
echo "the max file is $namemax"

18.查找當前網段內IP用戶,重定向到ip.txt文件中

#!/bin/bash
a=1
while :
do
    a=$(($a+1))
    if test $a -gt 255
    then break
    else
        echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
        ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g')
        echo $ip >> ip.txt
    fi
done

19.打印當前用戶

#!/bin/bash
echo "Current User is :"
echo $(ps | grep "$$" | awk '{print $2}')

20.case語句練習

#!/bin/bash
clear
echo "enter a number from 1 to 5:"
read num
case $num in
    1) echo "you enter 1"
    ;;
    2) echo "you enter 2"
    ;;
    3) echo "you enter 3"
    ;;
    4) echo "you enter 4"
    ;;
    5) echo "you enter 5"
    ;;
    *) echo "error"
    ;;
esac

21.yes/no返回不同的結構

#!/bin/bash
clear
echo "enter [y/n]:"
read a
case $a in
    y|Y|Yes|YES) echo "you enter $a"
    ;;
    n|N|NO|no) echo "you enter $a"
    ;;
    *) echo "error"
    ;;
esac

 22.內置命令的使用

#!/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.打印無密碼用戶

#!/bin/bash
echo "No Password User are :"
echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章