(shell練習1)zenity圖形介面之資訊對話方塊和單複選對話方塊,以及while,for迴圈的練習
阿新 • • 發佈:2018-12-22
涵蓋內容包括
1,shell的while迴圈
2,shell的for迴圈
3,shell關於字串的擷取(精華之處)
4,shell的選擇分支語句if
5,zenity資訊對話方塊
6,zenity單選對話方塊
7,zenity複選對話方塊
8,shell不傳引數函式
9,shell傳引數函式
#!/bin/bash CD="cd" LS="ls" RM="rm" CP="cp" FIND="find" GREP="grep" OTHERS="others" ## 自定義函式1,不傳遞引數 cdFunction() { ##2,資訊對話方塊 zenity --info --width 300 --height 200 --text="用法:cd (引數),進入到某個目錄中,cd ..是返回到上一級目錄,cd ../..是返回到上上一級目錄,cd(回車) 進入使用者主目錄,cd ~進入使用者主目錄,cd -返回進入此目錄之前所在的目錄,cd !$把上個命令的引數作為cd引數使用。" } lsFunction() { zenity --info --width 300 --height 200 --text="用法:ls (選項) (引數),選項-l,所有輸出資訊用單列格式輸出,不輸出為多列,選項-t,用檔案和目錄的更改時間排序,引數一般為檔案或者目錄。" } rmFunction() { zenity --info --width 300 --height 200 --text="用法:rm (選項) (引數),選項-f,即使原檔案屬性設為唯讀,也可以刪除,無需逐一詢問,選項-r,將目錄及以下之檔案逐一刪除。" } cpFunction() { zenity --info --width 300 --height 200 --text="用法:cp (選項) (引數),選項-a,這個選項通常在複製目錄時使用,它保留連結、檔案屬性,並複製目錄下的所有內容,選項-r,若給出的原始檔是一個目錄檔案,此時將複製該目錄下所有的子目錄和檔案。" } ## 自定義函式2,傳遞引數 checkFunction() { declare i if [ $1 == 1 ] then echo $2 else tmp=$2 for((i=0;i<$1;i++)) do str=${tmp%%|*} tmp=${tmp#*|} echo $str done fi } ########## function 1 while((1)) do ##1,列表單選框 select_command=$(zenity --list --radiolist --width 300 --height 400 --text="linux 常見命令" --column="選擇" --column="內容列表" TRUE $CD FALSE $LS FALSE $RM FALSE $CP); ## 處理自定義函式的結果 if [ "$select_command" == "$CD" ] then cdFunction elif [ "$select_command" == "$LS" ] then lsFunction elif [ "$select_command" == "$RM" ] then rmFunction elif [ "$select_command" = "$CP" ] then cpFunction else break fi done ########## function 2 while((1)) do ##2,列表複選框 select_option=$(zenity --list --checklist --width 300 --height 400 --text="查詢命令" --column="選擇" --column="內容列表" FALSE $FIND FALSE $GREP FALSE $LS FALSE $OTHERS); if [ "$select_option" == "$FIND|$GREP|$LS|$OTHERS" ] then checkFunction 4 $select_option elif [ "$select_option" == "$FIND|$GREP|$LS" ] then checkFunction 3 $select_option elif [ "$select_option" == "$FIND|$GREP|$OTHERS" ] then checkFunction 3 $select_option elif [ "$select_option" == "$FIND|$LS|$OTHERS" ] then checkFunction 3 $select_option elif [ "$select_option" == "$GREP|$LS|$OTHERS" ] then checkFunction 3 $select_option elif [ "$select_option" == "$FIND|$GREP" ] then checkFunction 2 $select_option elif [ "$select_option" == "$FIND|$LS" ] then checkFunction 2 $select_option elif [ "$select_option" == "$FIND|$OTHERS" ] then checkFunction 2 $select_option elif [ "$select_option" == "$GREP|$LS" ] then checkFunction 2 $select_option elif [ "$select_option" == "$GREP|$OTHERS" ] then checkFunction 2 $select_option elif [ "$select_option" == "$LS|$OTHERS" ] then checkFunction 2 $select_option elif [ "$select_option" == "$FIND" ] then checkFunction 1 $select_option elif [ "$select_option" == "$GREP" ] then checkFunction 1 $select_option elif [ "$select_option" == "$LS" ] then checkFunction 1 $select_option elif [ "$select_option" == "$OTHERS" ] then checkFunction 1 $select_option else break fi done exit 0