Shell指令碼編程

來源:互聯網
上載者:User

標籤: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指令碼編程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.