腳本一千題


原腳本文件


[root@localhost script]# cat test
1 2 50 90 1050 796 228

a b c d e f g h i j k l
good high tall full qualify happy cool


1、預先設置每一行的分割符號爲兩個換行

[root@localhost script]# awk 'BEGIN { ORS="/n/n" }; 1' test
1 2 50 90 1050 796 228/n/n/n/na b c d e f g h i j k l/n/n/n/ngood high tall full qualify happy cool/n/n


2、最後的字段加入一個換行符

[root@localhost script]# awk 'NF {print $0 "/n"}' test
1 2 50 90 1050 796 228/n
a b c d e f g h i j k l/n
good high tall full qualify happy cool/n


3、顯示當前行,所在文件中的行號

[root@localhost script]# awk '{print FNR "/t" $0}' test
1/t1 2 50 90 1050 796 228
2/t
3/ta b c d e f g h i j k l
4/tgood high tall full qualify happy cool

4、當前行在每次處理文件的行號

[root@localhost script]# awk '{print NR "/t" $0}' test
1/t1 2 50 90 1050 796 228
2/t
3/ta b c d e f g h i j k l
4/tgood high tall full qualify happy cool

5、使用簡單樣式輸出

[root@localhost script]# awk '{printf ("%5d : %s/n",NR,$0)}' test
    1 : 1 2 50 90 1050 796 228/n    2 : /n    3 : a b c d e f g h i j k l/n    4 : good high tall full qualify happy cool/n #行號佔用5位,不足補空格


6、顯示行號

[root@localhost script]# awk '{print NR}' test
1
2
3
4

7、計算行數,效果類似wc  -l

[root@localhost script]# awk 'END {print NR}' test
4

8、計算每一行的和

[root@localhost script]# awk '{s=0;for (i=1;i<=NF;i++)s=s+$i;print s}' test
2217
0
0
0

9、計算文件中所有字段的和

[root@localhost script]# awk '{for (i=1;i<NF;i++) s=s+$i};END {print s}' test
1989

10、將每個字段用其絕對值代替

[root@localhost script]# awk '{for (i=1;i<NF;i++) if ($i < 0) $i = -$i;print}' test
1 2 50 90 1050 796 228

a b c d e f g h i j k l
good high tall full qualify happy cool

11、計算文件中總的字段和(例如計算單詞數)

[root@localhost script]# awk '{total = total + NF};END {print total}' test
26

12、計算匹配指定信息的總行數

[root@localhost script]# awk '/Linux/ {n++}; END {print n+0}' test
0


使用test1文件

[root@localhost script]# cat test1
1 23 566 90 189 290 6666 13569
27 89 289 367 4899 10999 209999 3000000
26 39 409 366 758 629 590 256
1024 2048 3072 4096 5120 6144 7168 8192


13、計算文件所有字段的總和

[root@localhost script]# awk '{for (i=1;i<=NF;i++) s=s+$i};END {print s}' test1
3288000

14、計算每一行的和

[root@localhost script]# awk '{s=0;for(i=1;i<=NF;i++)s=s+$i;print s}' test1
21394
3226669
3073
36864

15、顯示當前行的字段數,並輸出當前行

[root@localhost script]# awk '{print NF ":" $0}' test1
8:1 23 566 90 189 290 6666 13569
8:27 89 289 367 4899 10999 209999 3000000
8:26 39 409 366 758 629 590 256
8:1024 2048 3072 4096 5120 6144 7168 8192

16、顯示每行最後一個字段的內容

[root@localhost script]# awk '{print $NF}' test1
13569
3000000
256
8192

17、顯示最後一行的最後一個字段

[root@localhost script]# awk '{field = $NF};END {print field}' test1
8192

18、顯示字段數大於4的行

[root@localhost script]# awk 'NF > 4' test1
1 23 566 90 189 290 6666 13569
27 89 289 367 4899 10999 209999 3000000
26 39 409 366 758 629 590 256
1024 2048 3072 4096 5120 6144 7168 8192

19、顯示最後一個字段大於4的行

[root@localhost script]# awk '$NF > 4' test1
1 23 566 90 189 290 6666 13569
27 89 289 367 4899 10999 209999 3000000
26 39 409 366 758 629 590 256
1024 2048 3072 4096 5120 6144 7168 8192

字符串反向輸出


20、編輯腳本使用字符串反轉


vim 1.sh

#!/bin/bash


STR=$1

LEN=${#STR}


for ((i=$LEN;i>=0;i--))

do

    echo -n "${STR:i:1}"

done

    echo -e "\n"


21、使用rev命令,對字符串進行反轉


[root@localhost script]# echo ABCD | rev
DCBA


22、使用sed命令s參數來替換


[root@localhost script]# echo 'ABC'|sed 's/\(.\)\(.\)\(.\)/\3\2\1/g'
CBA

23、使用awk命令


[root@localhost script]# echo ABC | awk '{for(i=1;i<=length;i++){line=substr($0,i,1) line}} END {print line}'
CBA

24、使用python實現


[root@localhost script]# echo ABCD | python -c 'print raw_input() [::-1]'
DCBA

25、創建用戶student1到student50,指定組爲student組!而且每個用戶需要設定一個不同的密碼!


[root@localhost ~]# cat studentadd.sh
#!/bin/bash

for i in `seq 1 50`

do

   useradd student$i -G student

   echo "student$i" |passwd --stdin student$i

done

26、 編寫shell腳本,將/usr/local/src目錄下大於100k的文件轉移到/tmp目錄下:






27、打印最後一個字段

[root@localhost ~]# echo -e "line1 f1 f2 f3\n line2 f4 f5 f6"|awk '{print $NF}'
f3
f6

28、打印倒數第二個字段

[root@localhost ~]# echo -e "line1 f1 f2 f3\n line2 f4 f5 f6"|awk '{print $(NF-1)}'
f2
f5

29、一個每一行中第一個字段值累加的例子:

[root@localhost ~]# seq 5|awk 'BEGIN{ sum=0; print "總和: " }{ print $1"+";sum+=$1 } END {print "等於"; print sum}'
總和:
1+
2+
3+
4+
5+
等於
15


30、輸出1-100相加的總和

[root@localhost ~]# seq 1 100 |awk 'BEGIN {sum=0} {sum+=$1} END{print sum}'
5050

31、查找/root目錄下大於1M的文件並刪除它


[root@localhost ~]# find /root -type f -size +1M |xargs rm -rf


32、使用awk 找出文件大於10K的文件,並打印其文件名


[root@localhost ~]# ll|sed 1d|awk '{if($5 > 10000) print $9}'
test1
test2

33、計算兩個數的商


#!/bin/bash

var1=10
var2=20

var3=$(expr $var2 / $var1)
echo The result is $var3

執行結果:


root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# ./test6.sh
The result is 2

34、使用方括號進行算數運算


root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# cat test7.sh
#!/bin/bash

var1=100

var2=200

var3=$[ 5 * ( $var1 + $var2 ) ]

echo "$var3"


計算結果:


root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# ./test7.sh
1500

35、使用方括號計算兩個數的商


root@iZ2ze5amm8uy6o4jjpvtybZ:~/script# cat test8.sh
#!/bin/bash


var1=100

var2=50

var3=$[ $var1 / $var2 ]

echo $var3

36、查看用戶zhangwenqiang是否存在於系統中


#!/bin/bash


testuser=zhangwenqiang


if grep $testuser /etc/passwd


then


            echo "The bash files for user $testuser are:"

           

            ls   -a  /home/$testuser/.b*


else


           echo "The user $testuser does not exist on the system"


fi


37、檢測用戶和其下的目錄是否存在


#!/bin/bash


testuser=hello


if grep $testuser /etc/passwd


then


         echo "$testuser is exist on the system"


elif


       ls -d  /home/$testuser


then


        echo "The user $testuser is not exist on the system"

 

        echo "However, $testuser has a directory."


fi


38、測試幾個數字的大小


#/bin/bash


if grep $testuser /etc/passwd


then


           echo  "$testuser is exist on the system"


elif


            ls   -d   /home/$testuser


then


            echo "The user $testuser is not exist on the  system"


           echo "However  ,$testuser has a directory."


fi


39、查看字符串var1是否大於字符串var2


#!/bin/bash


var1=baseball

var2=hockey


if [ $var1 > $var2 ]


then


        echo "$var1 is greater than $var2"


else


        echo "$var1 is less than $var2"


fi


40、判斷字符串是否爲空


var1=testing


var2=


if  [ -n $var1 ];then


      echo "The string '$var1' is not empty"!


else


      echo  "The string '$var2' is empty"!


fi


if [ -z $var2 ];then

     

then


       echo  "The string '$var1' is empty"


else


       echo "Ths string "$var2"  is not empty"


fi


41、遍歷下一個城市的名稱


#!/bin/bash


for i in shanghai beijing hangzhou guangzhou shenzheng


do


       echo "The next city is $i"


done


43、從命令讀取值


#!/bin/bash


file="/root/test"


for city in $(cat $file)


do


      echo  next  is  a  beautiful $city


done


44、判斷目錄下的文件是目錄,還是文件


#!/bin/bash


for file in /root/*


do


      if [  -d $file ];then


      echo "$file is a directory"


      elif [ -f $file ];then


      echo "$file is a file"

 

      else


       echo  "No such file"


fi


done


45、使用for循環輸出數列


for (( i=1 ; i<=10 ; i++ ))


do


      echo  "The next  number is $i "


done


46、使用多個變量


#!/bin/bash


for ((  a=10,b=1;b<=10;a--,b++ ))


do


         echo $a - $b


done


47、使一個變量從100,遞減爲1


#!/bin/bash


a=100


while [ $a -gt 1 ]


do


         echo  "The next number is $a"


         a=$[ $a  - 1]


done


48、使用util語句循環


var1=100


until [ $var1 -eq 0 ]


do


        echo  $var1


        var1=$[$var1 - 25]


done


49、使用循環嵌套


for  (( a=1;a<=3;a++ ))


        do 

            

                echo "Start Inner loop: $a"           

 

               for  (( b=1;b<=3;b++ ))


                       do

                                 echo "loop:  $b"

                       done

        done


50、使用until和while混合循環


     var3=3


     until [ $var3 -eq 0 ]


     do


              echo "The Outer loop is: $var3 "


               var2=1


               while [ $var2 -lt 5 ]


               do

                     

                   var3=$(echo scale=4; $var1 / $var2|bc)


                   echo "The inner loop is: $var3"


                  var2=$[$var2 + 1]


              done 


            var1=$[$var1 - 1]


  done         


51、使用break 命令跳出循環


#!/bin/bash


      for  i  in 1 2 3 4 5 6 7 8 9

             do

                  echo "The next number is $i"

            

                  if [ $i -eq 5 ];then

 

                        break


                      fi

               

             done


52、 使用if語句退出for循環


#!/bin/bash


          i=1


         while [ $i -lt 5 ]


           do


                  if [ $i -eq 3 ];then


                       break


                  fi


                  echo "The next number is $i"


               i=$[$i + 1]


           done


         echo "End the while loop"


53、使用break跳出外部循環


#!/bin/bash


           for (( a=1;a<=5;a++ ))


             do


                   echo "The Outer loop is $a"


                  for (( b=1;b<=100;b++ ))


                      do


                         if  [ $b -gt 4 ];then


                          break  2


                     echo "The Inner loop is $b"


                      done


              done


54、使用continue語句


#!/bin/bash


       for (( a=1;a<=100;a++ ))


            do


                  if [ $a -gt  5 ] && [$a -lt 20];then


                       continure


                  fi


           echo  "The benifit number is $a"


           done                       


55、定義繼續循環的等級


#!/bin/bash


       for (( a=1;a<=5;a++ ))


            do


                 for (( b=1;b<=10;b++ ))


                  do

                         if [ $b -gt 5] && [ $b -lt 8 ];then


                             continue 2


                         fi


                        c=$[$a * $b]


                        echo "$a multiple $b is $c"


                done


done

                      

56、 查看目錄下文件的數量


#!/bin/bash


file="/root/local/bin"


for i in $file


              do


               number=`ls -l $file|grep "^-"|wc -l`


              done


               echo "There are $number files in the directory."


57、查找並顯示文件下的可執行文件


#!/bin/bash


IFS=:


for folder in $PATH


             do


                   echo  "$folder :"


                      for   i   in   $folder


                                      do


                                           if [ -x $i ];then


                                                echo "$i"


                                           fi


                                      done


              done


58、添加幾個用戶


#!/bin/bash


input="user.csv"


IFS=“,”


while IFS="," read -r userid,name


do


      echo "adding $userid"


     useradd -c $name -m  $userid


done  < $input


59、求斐波那契數列的值


#!/bin/bash


factorial=1


for (( number=1;number<=$1;number++ ))


do

      factorial=$[$factorial * $number]


done


     echo $factorial


60、輸入兩個數並做乘法運算


#!/bin/bash


echo "Please input a number $1"


echo "Please input a number $2"


multinumber=$[$1 * $2]


echo "$1 multiply $2 is $multinumber."


61、查看腳本的名稱


#!/bin/bash


echo "The script name is $0"


62、顯示不包含路徑的腳本名稱


#!/bin/bash


name=$[basename $0]


echo "The script number is $name."


63、檢測是否存在數據


#!/bin/bash


if [ -n "$1" ];then


       echo "Hello $1,nice to meet you!"


else


      echo "Sorry ,your don't identify yourself."


fi


64、統計腳本攜帶參數的個數


#!/bin/bash


echo "There are $# parameters for the script."


65、輸入兩個數並求它們的和


#!/bin/bash


if [ $# -ne 2 ];then


       echo "Please input two numbers."


else


       total=$[ $1 + $2 ]


      echo "The total value is $total."


fi


66、顯示腳本最後的一個參數


#!/bin/bash


params=$#


echo The last parameter is $params


echo The last parameter is ${!#}


67、顯示腳本的所有參數


#!/bin/bash


echo


echo "The script all parameter is $*"


echo "The script all parameter is $@"


echo


68、顯示$@與$*的區別


echo


count=1


for param in $*


do


echo "The \$* parameter #$count = $param"


count=$[$count + 1]


done


echo


count=1


for param in $@


do


echo "The \$@ parameter #$count = $param


count=$[$count + 1]


done


69、移動變量


#!/bin/bash


count=1


echo


while [ -n "$1" ]


do


echo "The next parameter is  #$count is $1"


count=$[$count + 1]


shift


done


70、使用shift移動多個位置


#!/bin/bash


echo "The orgin parameter : $*"


shift 2


echo "Here's the new parameter $1"


71、使用case語句與shift語句相結合


#!/bin/bash


while [ -n "$1" ]

do

case $1 in

             -a)  echo "Find -a option" ;;

             -b)  echo "Find -b option";;

             -c)  echo "Find -c option";;

             *)  echo "$1 is not an option";;

        esac

        shift

done


72、遍歷給定的參數,跳過破折號


#!/bin/bash


while [ -n "$1" ]


do


      case $1 in


                   -a) echo "Find -a option" ;;

                   -b) echo "Find -b option";;

                   -c)  echo "Find -c option";;

                   --) break

                        shift ;;

                    *)  echo "$1 is not an option" ;;

       esac

   

count=1


for param in $@


do


           echo   "$count :  $param"

          

           count=$[$count + 1]


done


73、寫一個程序,計算自己來到這個世界的天數


#!/bin/bash


echo "Please input your age"


read age


echo "You live in this world for $[ 365 * $age ] days"


74、寫一個程序,輸出你的姓名


#!/bin/bash


echo "Please  Input your name"


read -p firstname lastname


echo "Your name is $lastname $firstname"


75、特殊REPLY的使用


#!/bin/bash


read -p "input your name"


echo


echo "hello $REPLY,nice to meet you."


76、使用-t 指定一個定時計,在規定的時間,沒有輸入變量的,設定一個提出的狀態


#!/bin/bash


if  read -t 5 -p "echo please input your name:" name


then


         echo "Hello $name ,nice to meet you."


else


        echo "Sorry,you input is too slow."


fi


77、編輯一個判斷的程序


#!/bin/bash


if  read -n1 -p “please input your choice: Y|N"  answer


then


     case  $answer in


              Y|y)


                    echo  "Continue on" ;;


             N|n)


                   echo "Good Bye"


esac


fi


78、輸入你的密碼


#!/bin/bash


read -s -p "Please input your password Y|N" pass


echo


echo "Is  your password $pass"


79、統計腳本中的文件的數量


#!/bin/bash


count=1


cat /root/script/test | while read line


do

       

            Line:  $count  line

 

            count=$[ $count + 1]


done


echo "Finish process the file"


80、使用從文本讀入文件並輸出


#!/bin/bash


exec 0 < testfile


count=0


while read line


do


       Line:  #$count  $line

       count=$[ $count + 1 ]


done


echo "Have finished process the file"


81、使用tee命令多重重定向


#!/bin/bash


date | tee test


82、使用Trap命令忽略信號 SIGINT


#!/bin/bash


trap "echo 'sorry I have trapped Ctrl-C' " SIGINT


count=1


while [ $count -le 10 ]


do


        echo " Loop:  #$count "


        sleep  1


        count=$[ $count + 1 ]


done


echo "This is the end of script."


83、用trap命令捕獲腳本的退出


#!/bin/bash


trap "echo 'Goodbye..'" EXIT


count=1


while [ $count -le ]


do


      echo  "Loop:  #$count "  EXIT


      sleep  1


      count=$[ $count + 1 ]


done


84、修改或移除捕獲


#!/bin/bash


trap "echo 'Sorry ...Ctrl-C is trapped'"  SIGINT


count=1


while [ $count -le 5 ]


do


    echo "The first Loop: #$count"

 

    sleep  1


    count=$[ $count + 1 ]


done


trap  "echo 'I modified the trap' "  SIGINT


count=1


while [ $count -le 6 ]


do


echo "The second Loop:   #$count"


count=$[ $count + 1 ]


done


echo "The end of script."


85、查看系統分配給腳本的PID


#!/bin/bash


echo "Script process ID: $$"


count=1


while [ $count -le 10 ]


do


echo "Loop: #$count"


count=$[ $count + 1]


done


echo "End of script ..."


86、利用&將另外一個作業作爲後臺進程啓動,出於簡化的目的,腳本的輸出被重定向到文件中,避免出現在屏幕上。jobs顯示這些進程、進程的PID及其狀態。


[root@system1 script]# ./6.sh > test.out &
[1] 1873
[root@system1 script]# jobs
[1]+  Running                 ./6.sh > test.out &
[root@system1 script]# jobs -l
[1]+  1873 Running                 ./6.sh > test.out &
[root@system1 script]# cat test.out
 The script process id is 1873
Loop: #1
Loop: #2
Loop: #3
Loop: #4
Loop: #5
Loop: #6
Loop: #7
Loop: #8
Loop: #9
Loop: #10
Completed the program.
[1]+  Done                    ./6.sh > test.out

87、使用bg重啓一個後臺的作業


[root@system1 script]# ./6.sh
 The script process id is 1995
Loop: #1
Loop: #2
^Z
[1]+  Stopped                 ./6.sh
[root@system1 script]# bg
[1]+ ./6.sh &
Loop: #3
[root@system1 script]# Loop: #4
Loop: #5
Loop: #6
^C
[root@system1 script]# Loop: #7
Loop: #8
Loop: #9
Loop: #10
^C
[root@system1 script]# Completed the program.

[1]+  Done                    ./6.sh

88、使用nice命令來設置命令啓動時的優先級


[root@system1 script]# nice -n 10 ./6.sh > test2.out &
[1] 2137
[root@system1 script]# ps -p 2137 -o pid,ppid,ni,cmd
  PID  PPID  NI CMD
 2137  1671  10 /bin/bash ./6.sh

89、Nice  改變進程的優先級


[root@system1 script]# nice -n 10 ./6.sh > test2.out &
[1] 2137
[root@system1 script]# ps -p 2137 -o pid,ppid,ni,cmd
  PID  PPID  NI CMD
 2137  1671  10 /bin/bash ./6.sh



































































   




























































      






  


       
























































































             










































 



 











































































      

      






























































































































  






















 






















































 
























  


     




























          


  


                        




                                  

    



        



              

    







                                        









                              


                            




        





















     
































































 











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