歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

Shell腳本基本操作練習

Shell腳本基本操作練習   這裡主要是熟悉了shell的基本操作,包括變量賦值引用修改、函數的使用、信號的獲取及一些判斷方法等,具體詳見代碼:   #!/bin/sh   str="Hello World !" echo "${str}aha"   #'' echo '$str'   #file name echo "the name of this shell script is $0"   #first parameter , $1---$N echo "the first parameter is $1"   #all of the parameter echo "all of the parameter is $*"   #num of the parameter echo "the num of the parameter is $#"   #PID of this process echo "this running pid is $$"   #the return value of the front one  echo "return value of the front one is $?"   #the reference of the return val echo "the current directory is "`pwd`   #-eq == str=5 str2=5 if [ "${str}" -eq "${str2}" ] ;then echo "$str equal $str2" else echo "no equal" fi   #-ge >= str=5 str2=10 if [ "$str2" -ge "$str" ];then echo "$str2 >= $str" else echo "no ge" fi   #-gt > ; -le <= ; -ne != ; = ==; != != ;    #str no null if [ "$str" ] ; then echo "$str is not null" else echo "null" fi   #-n str len of str > 0 if [ -n "$str" ] ; then echo "$str len > 0" else echo "len < 0" fi   #-z str len of str == 0   #dir filename="./test_dir" if [ -d $filename ]; then echo "$filename is dir" else echo "$filename is not dir" fi   #file filename="./test_dir" if [ -f $filename ]; then echo "$filename is file" else echo "$filename is not file" fi   #-r file : is read #-s file : len of file > 0 #-w file : is write #-x file : is run #   #case switch str=5   case $str in 1) echo "$str is 1" ;; 2) echo "$str is 2" ;; 5) echo "$str is 5" ;; *)echo "unknow val" esac     #fun add(){         local temp;         temp=`expr $1 + $2`         sum=`expr $temp + $3`         return $sum }   #more information about expr , access http://www.cnblogs.com/kelin1314/archive/2011/01/11/1933019.html   echo "we will use function add 3+8+10" add 3 8 10 echo "the sum val is $?"   #trap : catch the signal(SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM)   trap catch_signal INT catch_signal(){         echo "I catch the SIGINT !!!"         sleep 1 }   for i in 6 5 4 3 2 1;do echo "$i seconds until system failure , you can use CTRL+C ~" sleep 1 done echo "system failure"   運行結果如下:     tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/SHELL$ ./test_shell.sh    Hello World !aha $str the name of this shell script is ./test_shell.sh the first parameter is  all of the parameter is  the num of the parameter is 0 this running pid is 7736 return value of the front one is 0 the current directory is /mnt/hgfs/e/Lessons/MyExercise/UtilLibs/SHELL 5 equal 5 10 >= 5 5 is not null 5 len > 0 ./test_dir is dir ./test_dir is not file 5 is 5 we will use function add 3+8+10 the sum val is 21 6 seconds until system failure , you can use CTRL+C ~ 5 seconds until system failure , you can use CTRL+C ~ ^CI catch the SIGINT !!! 4 seconds until system failure , you can use CTRL+C ~ ^CI catch the SIGINT !!! 3 seconds until system failure , you can use CTRL+C ~ ^CI catch the SIGINT !!! 2 seconds until system failure , you can use CTRL+C ~ 1 seconds until system failure , you can use CTRL+C ~ system failure tiger@ubuntu:/mnt/hgfs/e/Lessons/MyExercise/UtilLibs/SHELL$   
Copyright © Linux教程網 All Rights Reserved