Shell指令碼對於Linux下的系統管理員和營運的人來說很重要。最近看了一下Shell指令碼,為了系統地學習一下Shell指令碼,我看了一下《鳥哥的Linux私房菜基礎學習篇》第三版,其中的第13章講了一下Shell script的學習。可以到鳥哥的網站:第十三章、學習 Shell
Scripts看看這一章的樣本,對於學習Shell指令碼初學者入門很不錯!
編寫shell script的良好習慣
1、script的功能;
2、script的版本資訊;
3、script的作者與聯絡方式;
4、script的版本聲明方式;
5、script的History (記錄)
6、script內較特殊的命令,使用“絕對路徑”的方式來執行;
7、script執行時需要的環境變數預先聲明和設定。
sh01.sh
#!/bin/bash# program:# This program show "Hello World!" in your screen# History:# 2013/04/21 15:31 ccf First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "Hello World! \a \n"exit 0
sh02.sh
#!/bin/bash# Program# User inputs his first name and last name. Program shows his full name.# History# 2013/04/21,by ccf,First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -p "Please input your first name: " firstname # 提示使用者輸入read -p "Please input your last name: " lastname # 提示使用者輸入echo -e "\nYour full name is: $firstname $lastname" #結果由螢幕輸出
sh03.sh
#!/bin/bash# Program#Program creates three files, which named by user's input#and date command# History:# 2013/04/21 Sunday ccf First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin:~/binexport 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"
sh04.sh
#!/bin/bash# Program:# User inputs 2 integer numbers; program will cross these two numbers.# History:# 2013/04/21 ccf19881030 First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHecho -e "You should input 2 numbers, I will cross them!\n"read -p "first number: " firstnumread -p "second number: " secondnumtotal=$(($firstnum * $secondnum))echo -e "\nThe result of $firstnum * $secondnum is ==> $total"
sh05.sh
#!/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 ReleasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport 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"
sh06.sh
#!/bin/bash# Program:# This program shows the user's choice# History:# 2013/05/14 ccf First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHread -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 0echo "I don't know what your choice is" && exit 0
可以通過sh sh01.sh或者chmod a+x sh01.sh;./sh01.sh來執行shell指令碼。