Linux Shell Scripting If-else and Case

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    This is what is asked:
    If the user enters �3�, prompt the user for two file names. Verify that the file names given already exist and are regular files. If not, display an error message. If both file names exist and are regular files, display a message showing which of the two files is older. If they�re the same, display either one (your choice).

I got my script below. I'm just wondering why is it after prompting the user, my next line of script which is the if-else part is not working.

  1. Relevant commands, code, scripts, algorithms:
3) echo "Enter first file:"
	           read filename
	   	 
		   if [ -f "$filename" ];
 		    then
  		     echo "Supplied file name does not exist"
		   else 
		     echo "Supplied file name exist"
		   fi ;;  
  1. The attempts at a solution (include all code and scripts):
#!/bin/bash

while true
do
	clear
	echo "Please enter one of the following options"
	echo "1. Move empty files"
	echo "2. Check file size"
	echo "3. Which file is newer"
	echo "4. File check rwx"
	echo "5. Exit"
	echo -e "Enter Choice:"
	read answer 
	case "$answer" in
		1) ./move_empty 
		exit 55 ;;
		2) echo "Enter a filename" 
		   read filename
		   if [ -f $filename ];
		   then ./file_size 
		   fi 
	           ;;
		3) echo "Enter first file:"
	           read filename
	   	 
		   if [ -f "$filename" ];
 		    then
  		     echo "Supplied file name does not exist"
		   else 
		     echo "Supplied file name exist"
		   fi ;;  

		5) exit ;;
esac
done
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Northern Alberta Institute of technology, AB, CA, Dan G., Dmit1532

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

How do you know it isn't working? Immediately after one of the echo commands completes, you start the next iteration of your loop. And, the first thing you do in each iteration of your loop is clear the screen.

1 Like

The script does work, but you've got the logics wrong. Check without clearing the screen between the loops. Mayhap after settimng the -vx shell options.

1 Like