標籤:shell 編程
1.編寫shell 指令碼,計算1-100 的和;
#!/bin/bashsum=0;for((i=0;i<=100;i++)){sum=$(( $sum + $i ))}echo $sum
2)編寫shell 指令碼,要求輸入一個數字,然後計算出從1 到輸入數位和,要求,如果輸入的數字小於1,則重新輸入,直到輸入正確的數字為止;
#!/bin/bashnumsave=0read -p "please input your number:" numwhile true;doif test $num -lt 0;then read -p "please again:" numelse breakfidonefor((i=0;i<=$num;i++)){ numsave=$(($numsave+$i))}echo $numsave
3)編寫shell程式,根據要求對給定目錄進行備份和恢複。
功能:
選擇1,對輸入的目錄進行備份,備份目錄為:/tmp
選擇2,實現對備份目錄的恢複,恢複到目前的目錄下
選擇3,退出
要求用函數實現:
backup():實現目錄的備份,產生.tar.gz檔案
restore():實現目錄的恢複
testdir():對輸入的目錄名進行判斷,若目錄名不存在,給出錯誤提示。
#!/bin/bashdir=$1#ls -ld ./$dirbackup(){ tar -cvf /tmp/$dir.tar ./$dir}restore(){ tar -xvf /tmp/$dir ./$dir}testdir(){ if test ! -d $dir;thenecho "this isn‘t dir"#exit 0fi}testdirwhile true;doselect opt in ‘BackUp Dir‘ ‘Restore Dir‘ ‘Exit‘ ;do case $opt in ‘BackUp Dir‘)#there isn‘t number backup ;; ‘Restore Dir‘) restore ;; *) exit 0 ;; esac donedone
Shell指令碼編程