[英]Bash Script add, delete, users, directories and files
嗨,大家好,我需要為我的課堂創建一個bash腳本,而且我還沒有參加過任何教該如何做的課程。 bash腳本菜單必須具有以下內容:
Please choose one of the following option:
a- Create a File
b- Create a Directory
c- Delete a File
d- Delete a Directory
e- Create a User
f- Delete a User
q- Quit
Enter your choice: a
What would you like to name your file (including the path to the location to where you want the file to be): ~/Data
~/Data: File Created successfully.
Enter your choice: q
我嘗試自己進行搜索,但由於我從未做過搜索,所以不知道從哪里開始。 我至少知道如何執行它,並且我知道如何使用命令來完成所有這些操作,但是我不知道如何創建bash腳本文件。 我會很感激您的幫助和一些解釋,因此至少可以嘗試理解這一點。 如果有幫助,我的課使用的是最新版本的fedora。 教授還希望我們使用納米
10-15頁的報告有什么問題? 包括研究和寫作在內,您每頁看一個小時。 小菜一碟...
您欠了很多啤酒:
#!/bin/bash
while [ "$SEL" != q ]; do
cat >&2 << MENU
Enter a number to launch:
a - Create a File
b - Create a Directory
c - Delete a File
d - Delete a Directory
e - Create a User
f - Delete a User
q - Exit
MENU
printf "\n Enter your choice: "
read SEL
SEL=${SEL,,}
case $SEL in
a ) printf "\nWhat would you like to name your File?\n"
printf "(including the path to the file location): "
read -r fname
[ -n $fname ] && touch "$fname" || \
printf "error: invalid filename.\n"
unset fname
;;
b ) printf "\nWhat would you like to name your Directory?\n"
printf "(including the path to the directory location): "
read -r dname
[ -n $dname ] && mkdir -p "$dname" || \
printf "error: invalid directory name.\n"
unset dname
;;
c ) printf "\nWhat File would you lime to delete?\n"
printf "(including path): "
read -r fname
[ -w "$fname" ] && rm "$fname" || \
printf "error: invalid filename or insufficent permission.\n"
unset fname
;;
d ) printf "\nWhat Directory would you lime to delete?\n"
printf "(including path): "
read -r dname
[ -d "$dname" -a -w "$dname" ] && rm -r "$dname" || \
printf "error: invalid directory or insufficent permission.\n"
unset dname
;;
e ) printf "\nEnter user name to add: "
read -r uname
[ -n $uname ] && useradd $uname
unset uname
;;
f ) printf "\nEnter user name to delete: "
read -r uname
[ -n $uname ] && userdel $uname
unset uname
;;
q ) exit 0
;;
* ) printf "\nError. Please enter a valid selection.\n"
;;
esac
done
注意:請確保將來將您的幫助退給有需要的人...
希望您的助教或教授能提供更多幫助。 這是一個基本程序,可能會對您有所幫助。 這不是您提交答案的方法,而是您學習的方法(請注意,我對bash知之甚少)。 我確實喜歡您借此機會解決了bash問題,而不是撰寫報告。
首先將其復制到nano中。 雖然刪除行號。 那將是一個痛苦的過程。 將文件另存為test.sh。 從命令行鍵入: chmod 755 test.sh
要運行該程序,請鍵入./test.sh
我將內聯發表評論。
1 #!/bin/bash
2
3 # create a function that will have echo statements
4 # to print instructions on the screen
5 function print_menu() {
6 echo Please choose on of the following option:
7 echo a - Create a file
8 echo b - Create a directory
9 echo c - Delete a file
10 echo d - Delete a directory
11 echo e - Create a user
12 echo f - Delete a user
13 echo q - quit
14 }
至此,我們已經編寫了一個函數,可以通過使用print_menu
調用該函數來重用。 您會在進一步閱讀時看到。
15
16 # keep on looping until user presses q
17 while true; do
18
我們想要顯示打印菜單,然后向用戶提問,根據答案,我們將做一些工作,然后重復顯示打印菜單和yada yada的循環。
19 # print the menu
20 print_menu
21 # ask user to choose an option
22 read -p "Enter your choice: " choice
到目前為止,我們已經向用戶顯示了菜單並要求輸入一個選項。 理想情況下,我們應該從用戶輸入等中進行錯誤檢查,但我們將在其他時間介紹這些概念。 choice
是一個變量(或存儲桶),將存儲用戶鍵入的任何內容。 如果用戶鍵入a
,然后choice
斗將包含a
。 要從該存儲桶中檢索信息,我們使用$choice
23 # based on user's choice, do variety of things
24 case $choice in
25 a) read -p "Name of file to create: " file
26 touch $file
27 echo Created file $file
28 echo ----
29 ;;
請查看http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html ,以獲取有關如何在bash中編寫case
語句的更多信息。 我們使用此case
語句評估存儲桶$choice
內容。
然后我們告訴bash,如果選擇的是a
,請詢問有關要創建的文件的另一個問題。 將答案存儲在稱為文件的存儲桶中。 並使用$file
使用存儲在名為file的存儲桶中的值
touch
命令創建文件。 然后,我們向用戶提供一些反饋,然后通過鍵入;;
結束該案例。
30
31 b) read -p "Name of directory to create: " dir
32 mkdir -p $dir
33 echo Created directory $dir
34 echo ---
35 ;;
36
37 c) read -p "Name of file to delete: " file
38 rm $file
39 echo Deleted file $file
40 echo ---
41 ;;
42
43 d) read -p "Name of directory to delete: " dir
44 rm -rf $dir
45 echo Deleted directory $dir
46 ;;
以上所有與選擇a
相同。 您可以添加更多代碼來處理其余情況。
47
48 q) echo Goodbye
49 break;;
如果用戶鍵入q
,我們給予反饋,我們break
從出來while
循環。
50
51 *) echo Nothing selected. Try again
52 ;;
如果用戶沒有上述選擇,我們將只提供反饋並返回while循環的頂部。
53 esac
54 # sleep 3 seconds to give user time to digest the output
55 sleep 3
56 done
57
我們睡眠3秒鍾,然后返回while循環的頂部。
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.