Shell指令碼初步學習 Shell指令碼對於Linux下的系統管理員和營運的人來說很重要。最近看了一下Shell指令碼,為了系統地學習一下Shell指令碼,我看了一下《鳥哥的Linux私房菜基礎學習篇》第三版,其中的第13章講了一下Shell script的學習。 編寫shell script的良好習慣 1、script的功能;2、script的版本資訊;3、script的作者與聯絡方式;4、script的版本聲明方式;5、script的History (記錄)6、script內較特殊的命令,使用“絕對路徑”的方式來執行;7、script執行時需要的環境變數預先聲明和設定。 sh01.sh [plain] <span style="font-size:14px;">#!/bin/bash # program: # This program show "Hello World!" in your screen # History: # 2013/04/21 15:31 ccf First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "Hello World! \a \n" exit 0 </span> sh02.sh [plain] <span style="font-size:14px;">#!/bin/bash # Program # User inputs his first name and last name. Program shows his full name. # History # 2013/04/21,by ccf,First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input your first name: " firstname # 提示使用者輸入 read -p "Please input your last name: " lastname # 提示使用者輸入 echo -e "\nYour full name is: $firstname $lastname" #結果由螢幕輸出</span> sh03.sh [plain] <span style="font-size:14px;">#!/bin/bash # Program # Program creates three files, which named by user's input # and date command # History: # 2013/04/21 Sunday ccf First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin:~/bin export PATH # 1.讓使用者輸入檔案名稱,並取得fileuser這個變數 echo -e "I will use 'touch' command to create 3 files." # 純粹顯示資訊 read -p "Please input your filename: " fileuser # 提示使用者輸入 # 2.為了避免使用者隨意按【Enter】,利用變數功能分析檔案名稱是否有設定 filename=${fileuser:-"filename"} # 開始判斷是否有設定檔名 # 3.開始利用date命令來取得所需要的檔案名稱了 date1=$(date --date='2 days ago' +%Y%m%d) # 前兩天的日期 date2=$(date --date='1 days ago' +%Y%m%d) # 前一天的日期 date3=$(date +%Y%m%d) # 今天的日期 file1=${filename}${date1} # 下面三行在設定檔名 file2=${filename}${date2} file3=${filename}${date3} # 4.建立檔案名稱 touch "$file1" touch "$file2" touch "$file3" </span> sh04.sh[plain] <span style="font-size:14px;">#!/bin/bash # Program: # User inputs 2 integer numbers; program will cross these two numbers. # History: # 2013/04/21 ccf19881030 First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo -e "You should input 2 numbers, I will cross them!\n" read -p "first number: " firstnum read -p "second number: " secondnum total=$(($firstnum * $secondnum)) echo -e "\nThe result of $firstnum * $secondnum is ==> $total"</span> sh05.sh[plain] <span style="font-size:14px;">#!/bin/bash #program: # User input a filename, program will check the follwing: # 1.) exist? 2.) file/directory? 3.) file permissions #History: # 2013/05/14 ccf First Release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #1、讓使用者輸入檔案名稱,並且判斷使用者是否真的有輸入字串 echo -e "Please input a filename, I will check the filename's type and permission. \n\n" #純粹顯示資訊 read -p "Input a filename : " filename #提示使用者輸入 test -z $filename && echo "You MUST input a filename." && exit 0 #2、判斷檔案是否存在,若不存在則顯示資訊並結束指令碼 test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0 #3、開始判斷檔案類型和屬性 test -f $filename && filetype="regular file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writeable" test -x $filename && perm="$perm executable" #4、開始輸出資訊! echo "The filename: $filename is a $filetype" echo "And the permission are : $perm" </span> sh06.sh [plain] #!/bin/bash # Program: # This program shows the user's choice # History: # 2013/05/14 ccf First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "Please input (Y/N) : " yn [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0 echo "I don't know what your choice is" && exit 0 可以通過sh sh01.sh或者chmod a+x sh01.sh;./sh01.sh來執行shell指令碼。