hi guys I need to create a bash scrip for my class and I haven't taken any classes that teach how to this yet. The bash script menu has to have the following:
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
I've try to do search on my own but since I've never done it before I don't know where to start. I know how to execute it at least and i know how to do all of this with the commands but I have no idea how to create the bash script file. I would appreciate the help and some explanation so I can at least try to understand this. My class is using the newest version of fedora if that helps. Also the professor wants us to use nano for it
What's wrong with a 10-15 page report? Research and writing included, your looking at an hour-per-page. Piece of cake... Or,
You owe a lotta beer:
#!/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
Note: you make sure you return the favor to someone in need in the future...
I wish your teaching assistants or professors provided a little more assistance. This is a rudimentary program that might help you. This is not a means for you to submit as an answer, but for you to learn (note that I know little about bash). I do like that you took the opportunity to tackle the bash problem instead of writing a report.
Start with copying this into nano. Remove the line numbers though. That'll be a painful process. Save the file as test.sh. From command line type: chmod 755 test.sh
. To run the program, type ./test.sh
I'll write comments inline.
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 }
At this point we have written a function that can be reused by calling it using print_menu
. You'll see as you read further.
15
16 # keep on looping until user presses q
17 while true; do
18
We want to show the print menu, then ask the user questions, based on the answer we'll do some work and then repeat the loop of showing print menu and yada yada.
19 # print the menu
20 print_menu
21 # ask user to choose an option
22 read -p "Enter your choice: " choice
So far we have shown the user the menu and asked to enter a choice. We should ideally do error checking from user input and such, but we'll cover those concepts some other time. choice
is a variable (or bucket) that will store whatever user typed. If user typed a
, then choice
bucket will contain a
. To retrieve information from that bucket, we use $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 ;;
Check out http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html for more information on how to write case
statements in bash. We use this case
statement to evaluate contents of bucket $choice
.
Then we tell bash, if the choice was a
, ask another question about file to create. Store the answer in bucket called file. And use the value contained in bucket called file by using $file
touch
command creates the file. Then we provide some feedback to the user and then close out that case by typing ;;
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 ;;
All the above is just the same as choice a
. You can add more code to handle rest of the cases.
47
48 q) echo Goodbye
49 break;;
If the user typed q
, we give feedback and we break
out from the while
loop.
50
51 *) echo Nothing selected. Try again
52 ;;
If none of the above choices were given by the user, we will just provide feedback and go back to the top of the while loop.
53 esac
54 # sleep 3 seconds to give user time to digest the output
55 sleep 3
56 done
57
We sleep for 3 seconds and then go back to the top of while loop.
Hope this helps out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.