下面記錄linux裡shell script學習過程中的一些筆記:
1:
根據輸入名及時間信息,創建幾個空文件
#!/bin/bash #programe: # This program creates three files, which named by user's input and date command #History: #2016/4/29 dairen First release PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH #1. 讓用戶輸入文件名 echo -e "I will use 'touch' command to creat 3 files." read -p "filename:" fileuser # 2. 為了避免使用者隨意按 Enter,在這裡檢測fileuser是不是空或者未設置(bash變量設定的部分) filename=${fileuser:-"filename"} #詳見p316 # 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%H%M%S) # 今天的日期hour minute second file1=${filename}${date1} # 變量相加 file2=${filename}${date2} file3=${filename}${date3} # 4. touch "$file2" # 創建空文件 touch "$file2" touch "$file3" #中文輸入super+空格man 命令是個好東西,要經常使用2:
total=$(($firstnu*$secnu)) echo -e "\nThe result of $firstnu x $secnu is: $total"其中對於數值運算可以:
declare -i total=$firstnu*$secnu但是他們都不能進行小數運算,因為bash shell 裡面默認支持到整數的數據3:
test的使用
#!/bin/bash # Program: # User input a filename, program will check the flowing: # 1.) exist? 2.) file/directory? 3.) file permissions #history: #2016/5/3 dairen 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="regulare file" test -d $filename && filetype="directory" test -r $filename && perm="readable" test -w $filename && perm="$perm writable" #變量相加 test -x $filename && perm="$perm executable" # 4. 開始輸出資訊! echo "The filename: $filename is a $filetype" echo "And the permissions are : $perm"4:
【】
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:usr/local/sbin:~/bin read -p "input y/n " yn [ "$yn" == "y" -o "$yn" == "Y" ] && echo "is y" && exit 0 [ "$yn" == "n" -o "$yn" == "N" ] && echo "is n" && exit 0 echo "i don't know what your choice is"5:
帶參數的shell腳本
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "The script name is ==> $0" echo "Total parameter number is ==> $#" [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here." \ && exit 0 echo "Your whole parameter is ==> '$@'" echo "The 1st parameter ==> $1" echo "The 2nd parameter ==> $2"6:
shift來使參數變量號碼偏移
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "Total parameter number is ==> $#" echo "Your whole parameter is ==> '$@'" shift # 進行第一次『一個變量的 shift 』 echo "Total parameter number is ==> $#" echo "Your whole parameter is ==> '$@'" shift 3 # 進行第二次『三個變量的 shift 』 echo "Total parameter number is ==> $#" echo "Your whole parameter is ==> '$@'"7:
netstat 查看目前主機打開的網絡接口有哪些
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH testing=$(netstat -tuln | grep ":80") if [ "$testing" != "" ];then echo "www is running in your system" fi testing=$(netstat -tuln | grep ":22") if [ "$testing" != "" ];then echo "ssh is running in your system" fi testing=$(netstat -tuln | grep ":21") if [ "$testing" != "" ];then echo "ftp is running in your system" fi testing=$(netstat -tuln | grep ":25") if [ "$testing" != "" ];then echo "mail is running in your system" fi testing=$(netstat -tuln | grep ":631") if [ "$testing" != "" ];then echo "cups is running in your system" fi exit 08:根據輸入的date和現在的date,計算出天數
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH echo "calculate days between two date" read -p "inpute a date in the futere .ex>20170809" date2 stringtest=$(echo $date2 | grep '[0-9]\{8\}') echo "$date2 $stringtest" if [ "$stringtest" == "" ];then echo "format wrong!!" exit 1 fi declare -i date_future=$(date --date="$date2" +%s) declare -i date_now=`date +%s` declare -i date_s=$(($date_future-$date_now)) if [ "$date_s" -lt "0" ];then echo "your inpute date is not in the future" else declare -i date_days=$((date_s/60/60/24)) echo "days between now and your date is:"$date_days"." fi9:while do done、 until do done
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH while [ "$testing" == "" ] do read -p 'inpute a number please' num testing=$(echo $num | grep '[0-9]') done s=0 i=0 while [ "$i" != "$num" ] do s=$(($s+$i)) i=$(($i+1)) done echo "the sum from 0-num is: $s"10:列出指定目錄下所有文件的權限
#!/bin/bash # Program: # User input dir name, I find the permission of files. # History: PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # 1. 先看看這個目錄是否存 read -p "Please input a directory: " dir if [ "$dir" == "" -o test ! -d "$dir" ]; then echo "The $dir is NOT exist in your system." exit 1 fi # 2. 開始測試文件 filelist=$(ls $dir) # 列出所有在該目錄下的文件名稱 for filename in $filelist do perm="" test -r "$dir/$filename" && perm="$perm readable" test -w "$dir/$filename" && perm="$perm writable" test -x "$dir/$filename" && perm="$perm executable" echo "The file $dir/$filename's permission is $perm " done11:使用ping命令判斷網絡狀態 seq 1 100 、 for do done、ping -c 1 -w 1 192.168.1.n
rogram # Use ping command to check the network's PC state. # History PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH network="192.168.1" # 先定義一個網域的前面部分! for sitenu in $(seq 1 100) # seq 為 sequence(連續) 的縮寫之意 do # 底下的程序在取得 ping 的回傳值是正確的還是失敗的! #發送一次包,間隔1s,返回結果重定向到無底洞 ping -c 1 -w 1 ${network}.${sitenu} &> /dev/null && result=0 || result=1 # 開始顯示結果是正確的啟動 (UP) 還是錯誤的沒有連通 (DOWN) if [ "$result" == 0 ]; then echo "Server ${network}.${sitenu} is UP." else echo "Server ${network}.${sitenu} is DOWN." fi done12:
13: