linux學習之shell script

來源:互聯網
上載者:User

標籤:linux   shell   

一、撰寫一個script,讓使用者輸入:1.first name 2.last name,最後在螢幕上顯示:Your full name is:的內容

#!/bin/bashread -p "Please input your firstname:" firstnameread -p "Please input your lastname:" lastnameecho -e "Your full name is:$firstname $lastname"

二、使用者輸入2個變數,然後將2個變數相乘,最後輸出相乘結果

#!/bin/bashread -p "input first number:" firstnuread -p "input second number:" secnutotal=$(($firstnu*$secnu))echo -e "the result of $firstnu x $secnu is $total"

三、使用source執行script,可將變數置於父進程(環境)中


四、使用者輸入一個filename,並做如下判斷:

  1. filename的檔案是否存在,不存在就終止程式

  2. 若存在,則判斷是檔案還是目錄,並輸入結果

  3. 判斷當前身份使用者對該檔案/目錄所具有的許可權,並輸出結果

#!/bin/bashecho -e "Please input a filename,this program will check the file‘s type and permission"read -p "Input a filename:" filename#1.判斷使用者是否真有輸入字串test -z $filename&&echo "You must input a filename"&& exit 0#2.判斷檔案是否存在,不存在則終止程式test ! -e $filename&&echo "the filename $filename no 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 writable"test -x $filename&&perm="$perm executable"#4.開始輸出資訊echo "The filename: $filename is a $filetype"echo "The permisson are: $perm"


linux學習之shell script

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.