1. 程式人生 > 實用技巧 >HTML隨筆——HTML5

HTML隨筆——HTML5

shell指令碼規範事項

1.指令碼第一行加指令碼直譯器:#!/bin/bash 或 #!/bin/sh
2.若指令碼中有中文,則需要在系統中加"export LANG="zh_CN.UTF-8"",並且在指令碼中重新定義字符集,使其和系統中的字符集一致
3.shell指令碼以.sh結尾,並且放到制定位置:例如"/server/scripts"
4.所以成對的符號,和迴圈語句的關鍵詞,要一次性寫完,防止遺漏
5.全域性變數全部大寫,區域性變數可以全部小寫或者使用駝峰語法進行書寫,例如:"myBook"
6.函式命名可採用首字母大寫,並且語義要清晰,例如:"createFile"
7.儘量在函式最後加上返回值,有些不會用到返回值的也一樣

二、高階命名規範:
1.常規shell用.sh結尾:例如:shell.sh
2.模組的啟動和停止指令碼統一命名為start_模組名.sh和stop_模組名.sh
3.監控指令碼一般以_mon.sh結尾
4.控制指令碼一般以_ctl.sh為字尾

執行shell指令碼的四種方式阿

  • 方式一:絕對路徑,需要當前使用者對指令碼檔案有rx許可權

    [root@Centos7 ~]# /scripts/day02/hello.sh
    -bash: /scripts/day02/hello.sh: Permission denied
    [root@Centos7 ~]# ll !$
    ll /scripts/day02/hello.sh
    -rw-r--r-- 1 root root 41 Aug 25 19:43 /scripts/day02/hello.sh
    [root@Centos7 ~]# chmod +x !$
    chmod +x /scripts/day02/hello.sh
    [root@Centos7 ~]# /scripts/day02/hello.sh
    hello world
    
  • 方式二:./指令碼檔案.sh,需要當前使用者對指令碼檔案有rx許可權

    [root@Centos7 day02]# chmod o=x hello.sh 
    [root@Centos7 day02]# ll hello.sh 
    -rwxr-x--x 1 root root 41 Aug 25 19:43 hello.sh
    [root@Centos7 day02]# su - egon
    [egon@Centos7 ~]$ cd /scripts/day02/
    [egon@Centos7 day02]$ ./hello.sh 
    bash: ./hello.sh: Permission denied
    
  • 方式三:指定直譯器來解釋執行指令碼程式,需要當前使用者對指令碼檔案有r許可權

    解釋:我們執行的是bash命令,所有使用者對bash命令都有執行許可權,所以我們只需要考慮指令碼檔案的讀許可權即可

    [root@Centos7 day02]# chmod o=- hello.sh 	#640
    [root@Centos7 day02]# su - egon
    [egon@Centos7 ~]$ cd /scripts/day02/
    [egon@Centos7 day02]$ ll
    -r-xr-x--- 1 root root 26 Aug 25 19:53 hello.sh
    [egon@Centos7 day02]$ bash hello.sh 
    bash: hello.sh: Permission denied
    [egon@Centos7 day02]$ exit
    logout
    [root@Centos7 day02]# chmod o=x hello.sh 	#641
    [root@Centos7 day02]# su - egon
    Last login: Tue Aug 25 19:53:31 CST 2020 on pts/0
    [egon@Centos7 ~]$ !cd
    cd /scripts/day02/
    [egon@Centos7 day02]$ bash hello.sh 
    bash: hello.sh: Permission denied
    [egon@Centos7 day02]$ exit
    logout
    [root@Centos7 day02]# chmod o=r hello.sh 	#644
    [root@Centos7 day02]# su - egon
    Last login: Tue Aug 25 19:54:07 CST 2020 on pts/0
    [egon@Centos7 ~]$ !cd
    cd /scripts/day02/
    [egon@Centos7 day02]$ bash hello.sh 
    hello world
    
    [root@hass-11 test]# ll /usr/bin/sh
    lrwxrwxrwx. 1 root root 4 Jun 30 02:36 /usr/bin/sh -> bash
    
    
  • 方式四:在當前bash程序中執行,需要當前使用者對指令碼檔案有r許可權

    前三種方式都是在子bash程序中執行

    [root@Centos7 ~]# echo $x
    
    [root@Centos7 ~]# source /scripts/day02/hello.sh
    hello world
    [root@Centos7 ~]# cat !$
    cat /scripts/day02/hello.sh
    x=111
    echo "hello world"
    
    [root@Centos7 ~]# echo $x
    111
    [root@Centos7 ~]# unset x
    [root@Centos7 ~]# 
    [root@Centos7 ~]# . /scripts/day02/hello.sh
    hello world
    [root@Centos7 ~]# echo $x
    111
    
    

shell指令碼除錯

1、使用dos2unix命令處理在Windows下開發的指令碼
#!/bin/bash
i=1
sum=0
while((i<=100))
do
    let i++
    ((sum+=i))
done
使用dos2unix進行格式化,會去除一些Windows的一些格式(例如空格)錯誤
---------------------------------------------------------------------------------------
2.bash命令除錯

sh [-nvx] scripts.sh
-n :不會執行指令碼,僅僅檢查語法是否有問題,並且給出錯誤提示
-v :在執行指令碼時,先將指令碼的內容輸出到螢幕上,然後執行指令碼,如果有錯誤,也會給出錯誤提示
-x :不會執行指令碼,將執行的指令碼內容輸出顯示到螢幕上。

printf "totalsum is $sum"
export PS4='+${LINENO}' #此命令可以使追蹤命令顯示每行的行號

vim相關檔案

~/.viminfo     使用者使用vim的操作歷史記錄
~/.vimrc       當前使用者的vim配置檔案 #
/etc/vimrc   系統全域性vim的配置檔案
/usr/share/vim/vim74/colors 配色魔板檔案存放路徑

注:
      1.當配置/etc/vimrc檔案時,所有使用者的vim均會受影響
      2.當編輯個人使用者家目錄下的隱藏檔案.vimrc,則只有此使用者的vim編輯受影響。

配置某一使用者的vim

編譯之後,直接生效

[root@hass-11 ~]# vim ~/.vimrc
#相容模式
set nocompatible
#設定最大歷史數
set history=100
#檢測檔案型別
filetype on
#開啟檔案型別外掛
filetype plugin on
#檔案型別縮排
filetype indent on
#設定自動讀取
set autoread
#設定滑鼠
set mouse=a
#語法檢測
syntax enable
set nofen
set fdl=0
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set smarttab
#自動縮排
set ai
set si
#自動換行
set wrap
set sw=4
set wildmenu
#顯示行號
set nu
set cmdheight=1
set lz
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
set magic
set noerrorbells
set novisualbell
set showmatch
set mat=2
#設定高亮
set hlsearch
set ignorecase
#設定編碼
set encoding=utf-8
set fileencodings=utf-8
set termencoding=utf-8
#智慧對齊
set smartindent
set cin
set showmatch
set guioptions-=T
set guioptions-=m
set vb t_vb=
set laststatus=2
set pastetoggle=<F9>
#設定背景=暗
set background=dark
#突出顯示搜尋ctermbg=黑色字元mfg=白色guifg=白色guibg=黑色
highlight Search ctermbg=black ctermfg=white guifg=white guibg=black

autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"

func SetTitle()
    if expand("%:e") == 'sh'
        call setline(1, "#!/bin/bash")
        call setline(2, "#Auther:itboy")
        call setline(3, "#Time:".strftime("%F %T"))
        call setline(4, "#Name:".expand("%"))
        call setline(5, "#Version:V1.0")
        call setline(6, "#Description:this is a test script.")
    endif
endfunc

vim命令

ngg :調到n行
0 :到行開頭
$ :到行結尾
L :移動到當前視窗最後一行 == G ,然後點"o",開始下一行編輯
H :移動到當前視窗最前面一行 ==gg 

命令列模式下:"ctrl+:"後
/old  :從上向下找"old"
%s/A/B/g :A全部替換為B "/"可以換成"#或@"
n1,n2 w filename : 將n1到n2行的內容儲存到filename檔案裡
n1,n2 co n3 : 將n1到n2行的內容複製到n3的位置下
n1,n2 m n3 : 將n1到n2行的內容剪下到到n3的位置下
! “命令”  :暫且退出vi,執行“命令”
vs filename  : 豎直分屏顯示filename ,q!退出分屏


1.多行註釋:
  1). 首先按esc進入命令列模式下,按下Ctrl + v,進入列(也叫區塊)模式;
  2). 在行首使用上下鍵選擇需要註釋的多行;
  3). 按下鍵盤(大寫)“I”鍵,進入插入模式;
  4). 然後輸入註釋符(“//”、“#”等);
  5). 最後按下“Esc”鍵。 注:在按下esc鍵後,會稍等一會才會出現註釋,不要著急~~時間很短的
 
2.刪除多行註釋:
  1). 首先按esc進入命令列模式下,按下Ctrl + v, 進入列模式;
  2). 選定要取消註釋的多行;
  3). 按下“x”或者“d”. 注意:如果是“//”註釋,那需要執行兩次該操作,如果是“#”註釋,一次即可

3.多行刪除
  1).首先在命令模式下,輸入“:set nu”顯示行號;
  2).通過行號確定你要刪除的行; 
  3).命令輸入“:32,65d”,回車鍵,32-65行就被刪除了,很快捷吧
      如果無意中刪除錯了,可以使用‘u’鍵恢復(命令模式下)
      也可以用"del"在視覺化中全刪選擇的內容

4.多行縮排
  1). 首先按esc進入命令列模式下,按下Ctrl + v,進入列(也叫區塊)模式;
  2). 在行首使用上下鍵選擇需要縮排的多行;
  3). 最後按"="即可

登入指令碼

[root@hass-11 script]# vim login.txt
#!/bin/bash
db_username="syy"
db_password="123"
read -p "請輸入使用者: " username
read -p "請輸入密碼:" password

[ $username == $db_username -a $password == $db_password ] && echo 登入成功 || echo 登入失敗

#&& 優先順序高於 ||
[root@Centos7 day02]# chmod +x login.sh 
[root@Centos7 day02]# ./login.sh 
請輸入您的賬號: xxx
請輸入您的密碼: 123
登入失敗
[root@Centos7 day02]# ./login.sh 
請輸入您的賬號: syy
請輸入您的密碼: 123
登入成功
----------------------------------------------------------------------------------------
#!/bin/bash
db_username=syy
db_password=123
user=`who am i|awk '{print $1}'`
hostname=`hostname`
count=0
lj=`/root`
while true;do
        read -p "請輸入使用者賬號: " username
        read -p "請輸入使用者密碼: " password
        ((count++))
        if [ $count -eq 3 ];then
        		echo "超出輸入次數,程式退出!!!"
        		exit 0
        fi
        if [ "${username}" = ${db_username} -a "${password}" = ${db_password} ];then
                while true;do
                        read -p "[${user}@${hostname} ${lj}]# " ml
                        if [ "$ml" = "exit" -o "$ml" = "q" -o "$ml" = "bash" ];then
                                break 2
                        else
                                $ml
                        		lj=`pwd`
                        fi
                done
        else
                echo "賬號密碼錯誤,請重新輸入!!!"
        fi
done

併發ping多個ip

[root@hass-11 script]# vim ip.sh
#!/bin/bash
for ip in $*
do
        (ping -c1 $ip &>/dev/null && echo "$ip:up" >> ping_access.log || echo "$ip:down" >> ping_error.log) &
done

實現2位數加減乘除運算

#!/bin/bash
if [ $# -eq 0 ]; then
 echo "輸入值不能為空"          //提示不能輸入空值  注:$#代表環境變數個數
 exit
fi

num=$*                       //定義一個全域性變數,方便後邊的呼叫

sum() {
sum=0
while [ $# -gt 0 ]
do
  sum=$[ $sum+$1 ]
  shift
done
echo  "數字 $num 相加的和是 $sum"
}

cha() {
 cha=$1
 shift
while [ $# -gt 0 ]
do
 cha=$[ $cha-$1 ]
 shift
done
echo  "數字 $num 相減的差是 $cha"
}                                                                                  
cheng() {
cheng=$1
shift
while [ $# -gt 0 ]
do
 cheng=$[ $cheng*$1 ]
 shift
done
echo  "數字 $num 相乘的是 $cheng"
}                                                                            
chu() {
chu=$1
shift
while [ $# -gt 0 ]
do
 chu=$[ $chu/$1 ]
shift
done
echo  "數字 $num 相除的商是 $chu"
}

read -p "請輸入 + | - | * | / :" type
case "$type" in
"+") sum $* ;;
"-") cha $* ;;
"*")cheng $* ;;
"/") chu $* ;;
*) echo " 請輸入{ +|-|*|/}"
esac

使用if語句

#!/bin/bash
# 這是一個計算器

read -t 30 -p "Please input the first number: " num1

read -t 30 -p "Please input the second number: " num2

read -t 30 -p "Please input the second operator(\"+\", \"-\", \"*\", \"/\"): " op

if [ -n "$num1" -a -n "$num2" -a -n "$op" ]
then
    # 1.校驗兩個運算元必須為數值
    test1=$(echo $num1 | sed 's/[0-9]//g')
    test2=$(echo $num2 | sed 's/[0-9]//g')
    if [ -n "$test1" -o -n "$test2" ]
    then
        echo "Please input 2 number."
        exit 1
    fi

    # 2.判斷操作符是否正確
    if [ "$op" == "+" ]
    then
       result=$(($num1 + $num2))
    elif [ "$op" == "-" ]
    then
       result=$(($num1 - $num2))
    elif [ "$op" == "*" ]
    then
       result=$(($num1 * $num2))
    elif [ "$op" == "/" ]
    then
       result=$(($num1 / $num2))
    else
        echo "Please input correct operator, like \"+\", \"-\", \"*\", \"/\"."
        exit 2
    fi

    # 3.列印結果
    echo "$num1 $op $num2 = $result"
    exit 0
else
    echo "Number and oprator must not be empty"
    exit 3
fi

使用case語句

#!/bin/bash
# 這是一個計算器

read -t 30 -p "Please input the first number: " num1

read -t 30 -p "Please input the second number: " num2

read -t 30 -p "Please input the second operator(\"+\", \"-\", \"*\", \"/\"): " op

if [ -n "$num1" -a -n "$num2" -a -n "$op" ]
then
    # 1.校驗兩個運算元必須為數值
    test1=$(echo $num1 | sed 's/[0-9]//g')
    test2=$(echo $num2 | sed 's/[0-9]//g')
    if [ -n "$test1" -o -n "$test2" ]
    then
        echo "Please input number."
        exit 1
    fi

    # 2.判斷操作符是否正確
    case $op in
        "+")
            result=$(($num1 + $num2))
            ;;
        "-")
            result=$(($num1 - $num2))
            ;;
        "*")
            result=$(($num1 * $num2))
            ;;
        "/")
            result=$(($num1 / $num2))
            ;;
        *)

            echo "Please input correct operator, like \"+\", \"-\", \"*\", \"/\"."
            exit 2
            ;;
    esac

    # 3.列印結果
    echo "$num1 $op $num2 = $result"
    exit 0
else
    echo "Number and oprator must not be empty"
    exit 3
fi

檢測硬碟根分割槽使用率

#!/bin/bash
disk_use=` df|grep "/$"|awk '{print $5}'|cut -d "%" -f 1`
if [ $disk_use -ge 90 ];then
        echo "critical"
elif [ $disk_use -ge 70 ];then
        echo "error"
elif [ $disk_use -ge 50 ];then
        echo "warning"
else
        echo "正常"
fi

系統硬體指標監控

#!/bin/bash
disk_use=`df|grep "/$"|awk '{print $5}'|cut -d "%" -f 1`
mem_free=`free|grep "Mem"|awk '{print $4}'`
mem_tatol=`free|grep "Mem"|awk '{print $2}'`
free_tatol=`echo "scale=2;$mem_free/$mem_tatol"|bc|cut -d . -f2`

if [ $disk_use gt 90 ];then
        echo "硬碟根分割槽剩餘率小於10%" | mail -s "`hostname`_`date +F%-M%-S%`_磁碟報警" [email protected]
fi

if [ $free_tatol lt 10 ];then
        echo "記憶體的剩餘率小於10%" | mail -s "`hostname`_`date +F%-M%-S%`_記憶體報警" [email protected]
fi

判斷使用者身份存在

[root@hass-11 ~]# vim yh.sh
#!/bin/bash
read -p "請輸入要判斷的使用者:" yonghu
id $yonghu &>/dev/null
if [ $? -eq 0 ];then
        echo "該使用者存在"
else
        echo "該使用者不存在"
fi

檢測埠

[root@hass-11 ~]# vim 80.sh
#!/bin/bash
netstat -lntup|grep "\bLISTEN\b"|grep "\b80\b" &>/dev/null
if [ $? -eq 0 ];then
        echo "80埠啟動正常"
else
        systemctl start httpd &>/dev/null
        if [ $? -eq 0 ];then
                echo "80埠啟動正常"
        else
                echo "80埠異常"
        fi
fi

nginx的啟動、停止、重啟和檢視狀態指令碼

該指令碼可用來管理httpd

[root@hass-11 ~]# vim case.sh 
#!/bin/bash
[ $# -eq 0 ] && exit 1
case $1 in
start)
	netstat -lntup|grep "\bLISTEN\b"|grep "\b80\b" &>/dev/null
	if [ $? == 0 ];then
		echo "nginx已經啟動"
	else
		systemctl start nginx
		if [ $? == 0 ];then
                	echo "nginx已經啟動"
		else
			echo "nginx啟動異常"
		fi
	fi	
	;;
stop)
        netstat -lntup|grep "\bLISTEN\b"|grep "\b80\b" &>/dev/null
        if [ $? == 0 ];then
                systemctl stop nginx
                sleep 1
                netstat -lntup|grep "\bLISTEN\b"|grep "\b80\b" &>/dev/null
                if [ $? == 0 ];then
                	echo "nginx停止異常"
                else
                	echo "nginx已經停止"
        else
                echo "nginx已經停止"
	fi
	;;
restart)
	netstat -lntup|grep "\bLISTEN\b"|grep "\b80\b" &>/dev/null
        if [ $? == 0 ];then
                pkill nginx
                sleep 1
                systemctl start nginx
                sleep 1
                netstat -lntup|grep "\bLISTEN\b"|grep "\b80\b" &>/dev/null
                if [ $? == 0 ];then
                        echo "nginx已經啟動"
                else
                        echo "nginx啟動異常"
                fi
		
        else
                systemctl start nginx
                if [ $? == 0 ];then
                        echo "nginx已經啟動"
                else
                        echo "nginx啟動異常"
                fi
        fi
	;;
status)
	systemctl status nginx
	;;
*)
	echo -e "語法錯誤,請輸入正確的語法\nusage: ${0} [start][stop][restart][status]"
	;;
esac

猜字遊戲

#!/bin/bash
read -p "請輸入一個整數:" num
if [ -n "${num}" ];then
        test=`echo ${num}|sed 's/[0-9]//g'`
        if [ -n "$test" ];then
                echo "只能輸入純數字哦~"
                read -p "請輸入一個整數:" num
                exit 1
        fi

        if [ ${num} -gt 18 ];then
                echo "猜大了"
        elif [ ${num} -lt 18 ];then
                echo "猜小了"
        else
                echo "猜對了"
        fi
else
        echo "沒有輸入,程式結束"
fi

#這裡 -n 後面一定要加引號

猜拳遊戲

#!/bin/bash
while true
do
        read -p "來和我猜拳8: " quan
	ran=$((RANDOM%2))
	case $ran in
	0)
	        num="石頭"
                echo -e "[電腦]: $num\n[你]: $quan"
		if [ "$quan" = "剪刀" ];then
			echo "你輸了"
		elif [ "$quan" = "石頭" ];then
			echo "重來"
		elif [ "$quan" = "布" ];then
			echo "你贏了"
		else
			echo "輸入錯誤"
		fi
	        ;;
	1)
	        num="剪刀"
                echo -e "[電腦]: $num\n[你]: $quan"
                if [ "$quan" = "布" ];then
                        echo "你輸了"
                elif [ "$quan" = "剪刀" ];then
                        echo "重來"
                elif [ "$quan" = "石頭" ];then
                        echo "你贏了"
                else
                        echo "輸入錯誤"
                fi
	        ;;
	2)
	        num="布"
                echo -e "[電腦]: $num\n[你]: $quan"
                if [ "$quan" = "石頭" ];then
                        echo "你輸了"
                elif [ "$quan" = "布" ];then
                        echo "重來"
                elif [ "$quan" = "剪刀" ];then
                        echo "你贏了"
                else
                        echo "輸入錯誤"
                fi
	        ;;
	esac
done

#在匹配字串時用了類似這樣的語句
if[ $timeofday = "yes"]; then;echo "Good morning";exit 0
如果變數timeofday的值為空,那麼就if語句就變成了if [  ="yes" ],這不是一個合法的條件。為了避免出現這種情況,我們必須給變數加上引號if [ "$timeofdat"="yes" ],這樣即使是空變數也提供了合法的測試條件,,if [  " "="yes"  ]

rsync管理指令碼

#!/bin/bash
#限制引數個數
if [ $# -ne 1 ]
        then
                echo $"usage:$0 {start|stop|restart}"
                exit 1
fi
#引數型別
if [ "$1" == "start" ];then
        rsync --daemon
        sleep 2  #休息2秒,這點很重要,停止或啟動後建議休息2秒後在判斷
                if [ $(netstat -pantu | grep rsync | wc -l) -ne 0 ];then #如果過濾的rsync不是0,則說明啟動了
                        echo "rsync is started"
                        exit 0
                else
                		systemctl start rsync
                		if [ $(netstat -pantu | grep rsync | wc -l) -ne 0 ];then
                        	echo "rsync is started"
                        	exit 0
                        else
                        	echo "rstnc 啟動異常"
                fi
elif [ "$1" == "stop" ];then
        pkill rsync &>/dev/null  #停止服務的方法很多,自選即可
        sleep 2
                if [ $(netstat -pantu | grep rsync | wc -l) -eq 0 ];then
                        echo "rsync is stopped"
                        exit 0
                fi
elif [ "$1" == "restart" ];then
        killall rsync
        sleep 1
        postKill=$(netstat -pantu | grep rsync | wc -l) #關閉前和啟動後都有對應的標識字元

        systemctl start rsyncd
        sleep 1
        postStart=$(netstat -pantu | grep rsync | wc -l)

        if [ "$postKill" -eq 0 -a "$postStart" -ne 0 ];then
                echo "rsync is restarted"
                exit 0
        fi
else
        echo $"usage:$0 {start|stop|restart}" #若沒有按照要求輸入引數,則提示後退出指令碼
	    exit 1
fi
----------------------------------或者---------------------------------------------------------
#/bin/bash
#chkconfig:2345 21 81
#description
if [ $# -ne 1 ];then
        echo $"usage:$0 {start|stop|restart}"
        exit 1
fi

case "$1" in 
"start")
        rsync --daemon
        sleep 2

        if [ $(netstat -pantu | grep rsync | wc -l) -ge 1 ];then
                        echo "rsync is started"
                        exit 0
                else
                		systemctl start rsync
                		if [ $(netstat -pantu | grep rsync | wc -l) -ne 0 ];then
                        	echo "rsync is started"
                        	exit 0
                        else
                        	echo "rstnc 啟動異常"
                fi
;;
"stop")
        killall rsync &>/dev/null
        sleep 2

        if [ $(netstat -pantu | grep rsync | wc -l ) -eq 0 ];then
                        echo "rsyncd is stopped"
                        exit 0
        fi
;;
"restart")
        killall rsync &>/dev/null
        sleep 1
        postKill=$(netstat -pantu | grep rsync| wc -l)
        systemctl start rsyncd
        sleep 1
        postStart=$(netstat -pantu | grep rsync | wc -l)

        if [ $postKill -eq 0 -a $postStart -ne 0 ];then
                echo "rsyncd is restarted"
                exit 0
        fi
;;
*)
        echo $"usage:$0 {start| stop restart}"
                exit 1
;;
esac

注:
1.修改許可權:
chmod +x /etc/init.d/rsyncd
/etc/init.d/rsyncd start  :可以執行
2.加入開機自動執行服務
  方式一:
      在上述指令碼中把bash直譯器下2行加入:
      #chkconfig: 2345 21 81
      #description 
      注:#不能丟,後面的數字要和/etc/init.d/rc3.d/下面的檔名數字不能重複

      chkconfig --add rsyncd  #加入自啟動,rsyncd 一定要放到/etc/init.d/目錄下
      chkconfig --list | grep rsyncd  #檢測自啟動
方式二:
      在/etc/rc.local檔案中加入
      /etc/init.d/rsyncd start

判斷一個目錄下的檔案型別

#!/bin/bash
read -p "請輸入要檢測的路徑: " lj
for i in `ls $lj`
do
	if [ -L "$lj"/"$i" ];then
		((link++))
	fi
        if [ -d "$lj"/"$i" ];then
                ((dir++))
	fi
        if [ -b "$lj"/"$i" ];then
                ((yb++))
        fi
        if [ -c "$lj"/"$i" ];then
                ((yc++))
        fi
        if [ -g "$lj"/"$i" ];then
                ((yg++))
        fi
        if [ -u "$lj"/"$i" ];then
                ((yu++))
        fi
        if [ -k "$lj"/"$i" ];then
                ((yk++))
        fi
        if [ -p "$lj"/"$i" ];then
                ((yp++))
        fi
        if [ -s "$lj"/"$i" ];then
                ((ys++))
        fi

        if [ -f "$lj"/"$i" ];then
                ((file++))
	fi

done

echo " 
${lj}的檢測結果:
--------------------------------------------
普通檔案和硬連結數: $file
    目錄和軟連結數: $dir
      軟連結檔案數: $link
      塊裝置檔案數: $yb
    字元裝置檔案數: $yc
    設定SGID檔案數: $yg
    設定SUID檔案數: $yu
      沾滯位檔案數: $yk
        管道檔案數: $yp
          空檔案數: $ys
"

計算所有引數(整數)之積

#!/bin/bash
function usage(){
    if [ $# -lt 2 ];then
        echo "usage: $0 num1 num2 ..."
    fi
}

function zhengshu(){
    for i in $*
    do
	((i++))
	if [ $? -ne 0 ];then
	    usage
	fi
    done
}

function getsum(){
    local sum=1
    for n in $*
    do
         ((sum*=n))
    done
    return $sum
}

function main(){
    usage $*
    zhengshu $*
    getsum $*
    echo $?
}

main $*

整數的加減,結果可以為負數

#!/bin/bash
function usage(){
    if [ $# -lt 2 ];then
        echo "usage: $0 num1 num2 ..."
    fi
}

function zhengshu(){
    for i in $*
    do
	((i++))
	if [ $? -ne 0 ];then
	    usage
	fi
    done
}

function getsum(){
    local sum=0
    for n in $*
    do
         ((sum+=n))
    done
    if [ $sum -lt 0 ];then
	echo $sum
	exit 0
    else
        return $sum
    fi
}

function main(){
    usage $*
    zhengshu $*
    getsum $*
    echo $?
}

main $*

數的加減,結果可以為負數,小數可以作為引數

#!/bin/bash
function usage(){
    if [ $# -lt 2 ];then
        echo "usage: $0 num1 num2 ..."
    fi
}

function getsum(){
    local sum=0
    for n in $*
    do
        sum=`echo "scale=2;${sum}+${n}"|bc`
    done
    echo $sum
}

function main(){
    usage $*
    getsum $*
}

main $*

數的乘積,結果可以為負數,小數可以作為引數

#!/bin/bash
function usage(){
    if [ $# -lt 2 ];then
        echo "usage: $0 num1 num2 ..."
    fi
}

function getsum(){
    local sum=1
    for n in $*
    do
        sum=`echo "scale=2;${sum}*${n}"|bc`
    done
    echo $sum
}

function main(){
    usage $*
    getsum $*
}

main $*

數的商,結果可以為負數,小數可以作為引數

#!/bin/bash
function usage(){
    if [ $# -lt 2 ];then
        echo "usage: $0 num1 num2 ..."
    fi
}

function getsum(){
    local sum=`echo "scale=2;${1}*${1}"|bc`
    for n in $*
    do
        sum=`echo "scale=2;${sum}/${n}"|bc`
    done
    echo $sum
}

function main(){
    usage $*
    getsum $*
}

main $*