linux shell編程

來源:互聯網
上載者:User

標籤:

linux shell編程

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

1 #!/bin/bash2 #Program:3 #        User inputs his first name and last name. Program shows his full name.4 read -p "Please input your first name: " firstname5 read -p "Please input your last name: " lastname6 echo -e "\nYour full name is:$firstname $lastname"

2)撰寫一個script(用[]測試指令來完成),完成如下功能:1. 當執行一個程式的時候,這個程式會讓使用者選擇 Y 或 N , 2. 如果使用者輸入 Y 或 y 時,就顯示『 OK, continue 』 3. 如果使用者輸入 n 或N 時,就顯示『 Oh, interrupt !』 4. 如果不是 Y/y/N/n 以內的其他字元,就顯示『 I don‘t know what your choice is 』

1 #!/bin/bash2 #Program:3 #       This program will show the user‘s choice4 read -p "Please input (Y/N): " yn5 [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK,continue" && exit 06 [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh,interrupt!" && exit 07 echo "I don‘t know what is your choice" && exit 0

 

3)將上一個程式用if...then...的方法來完成。

1 #!/bin/bash2 read -p "Please input (Y/N):" yn3 if [ "$yn" == "Y" -o "$yn" == "y" ]; then4 echo "OK,continue"5 elif [ "$yn" == "N" -o "$yn" == "n" ]; then6 echo "Oh,interrupt!"7 else8 echo "I don‘t know what is your choice"9 fi

4)分析下面指令碼程式的功能:

#!/bin/bash

#提示使用者輸入

read -p "please input a directory:" dir 

#判斷檔案是否存在,若不存在則顯示資訊並結束指令碼    

if [ "$dir == "" -o ! -d "$dir" ];then    

echo "the $dir is not exit in your system."

      exit 1

fii

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" 

done

 1 #!/bin/bash 2 #提示使用者輸入 3 read -p "Please input a directory: " dir 4 #判斷檔案是否存在,若不存在則顯示資訊並結束指令碼 5 if [ "$dir" = ""  -o ! -d "$dir" ]; then 6     echo "the $dir is not exit in your system" 7     exit 1 8 fi 9 filelist = $(ls $dir)10 for filename is $filelist11 do12    perm = ""13 #開始判斷檔案類型和屬性14    test -r "$dir/$filename" && perm = "$perm readable"15    test -w "$dir/$filename" && perm = "$perm writable"16    test -x "$dir/$filename" && perm = "$perm executable"17 #開始輸出資訊18    echo "the file $dir/$filename‘s permission is $perm"19 done

linux 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.