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