Question: Write a function which takes a filename as input. 1)If the file exist, it will display the content of the file. 2)Next use the case
Write a function which takes a filename as input. 1)If the file exist, it will display the content of the file. 2)Next use the case structure to display a command menu, and prompt the user for selection. Depending on the users selection, the user is allowed to write, append, edit the file, or exit the menu. 3)In case the users selection is invalid, prompt the user no valid selection is made. 4)If the file doesnt exist it will open the file in vi or vim. So you can write in the file and save it. 5)The program should complete all 3 Task examples below. COMMAND MENU Enter 1 write to file (Using cat) Enter 2 append to file (Using cat) Enter 3 edit file (Open vi) Enter 4 exit menu (show Bye and exit from program) Task(1) Enter a filname. fruits Displaying the file: banana apple orange COMMAND MENU Enter 1 to write to file Enter 2 to append to file Enter 3 to edit file Enter 4 to exit menu Enter an option: 4 BYE BYE Task(2) Enter a filename. newFile Creating a new file called newFile : Task(3) Enter a filename. country Displaying the file: USA Canada Brazil, Lets continue writing. COMMAND MENU Enter 1 to write to file Enter 2 to append to file Enter 3 to edit file Enter 4 to exit menu Enter an option: 6 You did not enter valid option. _______________________ Expert Answer Anonymous Anonymous answered this Was this answer helpful? fileoperations() { if [ -f "$1" ] then cat $1 else echo "Creating a new file called $1:" vi $1 fi echo "Enter 1 to write to file" echo "Enter 2 to append to file" echo "Enter 3 to edit file" echo "Enter 4 to exit menu" echo "Enter an option:" read option case $option in 1) cat>$1 ;; 2) cat>>$1 ;; 3) vi $1 ;; 4) echo "BYE BYE" break ;; *) echo "Invalid option";; esac } Note: u can write in the script using the below lines echo "Enter a filename." read file fileoperations $file _________ My questions: So how you check an existing file? because It will only prompt to enter a new file name and create a new file. How to check all 3 tasks on bash shell?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
