Linux Lab Write a simple menu-based file manager using bash scripts. After start
ID: 3723114 • Letter: L
Question
Linux Lab
Write a simple menu-based file manager using bash scripts. After starting the file manager repeatedly shows the user with choices that can be selected using a number as shown below:
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/exercises
What do you want to do? ===>
The file manager then waits for an input from the user. After the user has made a selection prompts the user for more input if necessary (for example, if the user has chosen 2 the file manager would ask for the name of the file that needs to be renamed, etc...) When it has enough inputs from the user the file manager carries out the requested operation.
Upon receiving correct input from the user the script then perform the function and display the result, e.g. success or error. If there was an error the file manager displays information about the error to the user.
If the user input was 0 the script stops.
You should not develop the file manager as a single monolithic function but divide it into many functions to manage complexity. For example, each menu choice should be handled by one or more functions. And perhaps the task to display the menu should be carried out by another function. Think about this exercise as developing a mid-size software and the programming language happens to be bash.
Explanation / Answer
Hello There,
PFB bash script for the requested functionality along with an output of test run:
script.sh
----------------------------------------
#!/bin/bash
print_menu () {
echo "File Manager Menu:"
echo "1) Make a directory in the current working directory"
echo "2) Change working directory"
echo "3) Rename a file or directory"
echo "4) Delete a file or a directory (and its contents)"
echo "5) Copy a file"
echo "0) Exit"
echo "Current working directory: $(pwd)"
}
create_directory(){
mkdir $1
}
change_directory(){
cd $1
}
rename_file(){
mv $1 $2
}
delete_directory(){
rm -rf $1
}
copy_file(){
cp $1 $2
}
functionality(){
while read -p "What do you want to do ==> " Choice && [[ -n "$Choice" ]] ; do
if [[ $Choice -eq 1 ]]; then
while read -p "Enter the directory Name to create:" dir && [[ -n "$dir" ]] ; do
create_directory $dir
break
done
elif [[ $Choice -eq 2 ]]; then
while read -p "Enter the directory to change to:" dir && [[ -n "$dir" ]] ; do
change_directory $dir
break
done
elif [[ $Choice -eq 3 ]]; then
while read -p "Enter the file/directory to rename:" arg1 && [[ -n "$arg1" ]] ; do
while read -p "Enter the new Name:" arg2 && [[ -n "$arg2" ]] ; do
rename_file $arg1 $arg2
break
done
break
done
elif [[ $Choice -eq 4 ]]; then
while read -p "Enter the directory to delete:" dir && [[ -n "$dir" ]] ; do
delete_directory $dir
break
done
elif [[ $Choice -eq 5 ]]; then
while read -p "Enter the file/directory to copy:" arg1 && [[ -n "$arg1" ]] ; do
while read -s -p "Enter the new Name:" arg2 && [[ -n "$arg2" ]] ; do
copy_file $arg1 $arg2
break
done
break
done
elif [[ $Choice -eq 0 ]]; then
exit 0
fi
print_menu
done
}
print_menu
functionality
---------------------------------------
Test Run:
----------------------------------------
:~/Desktop$ touch testFile
:~/Desktop$ ./script.sh
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 5
Enter the file/directory to copy:testFile
Enter the new Name:copyTestFile
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 5
Enter the file/directory to copy:testFile
Enter the new Name:copyTestFile2
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 3
Enter the file/directory to rename:copyTestFile2
Enter the new Name:renameTestFile
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 1
Enter the directory Name to create:Test
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 2
Enter the directory to change to:Test
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop/Test
What do you want to do ==> 2
Enter the directory to change to:~/Desktop
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 4
Enter the directory to delete:Test
File Manager Menu:
1) Make a directory in the current working directory
2) Change working directory
3) Rename a file or directory
4) Delete a file or a directory (and its contents)
5) Copy a file
0) Exit
Current working directory: ~/Desktop
What do you want to do ==> 0
:~/Desktop$ ls -l *testFile
-rw-rw-r-- 1 jack jack 0 Mar 5 03:55 testFile
:~/Desktop$ ls -l *estFile
-rw-rw-r-- 1 jack jack 0 Mar 5 03:56 copyTestFile
-rw-rw-r-- 1 jack jack 0 Mar 5 03:56 renameTestFile
-rw-rw-r-- 1 jack jack 0 Mar 5 03:55 testFile
:~/Desktop$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.