Homework & Coursework Questions

Students must use and complete the template provided. If you do not use the template your post may be deleted! Special homework rules apply here.

Linux Shell Scripting If-else and Case


👤 Login to reply

    #1  
Old 02-26-2018
cindy01 cindy01 is offline
Registered User
 
Linux 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.

2. Relevant commands, code, scripts, algorithms:
Code:
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 ;;


3. The attempts at a solution (include all code and scripts):
Code:
#!/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


4. 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).
Moderator's Comments:
Linux Shell Scripting If-else and Case Please use CODE tags as required by forum rules.
Without CODE tags, all internal sequences of blank characters (i.e., <space>s and <tab>s) are converted to a single <space> and leading and trailing blanks are completely removed.

Last edited by Don Cragun; 02-26-2018 at 02:52 AM.. Reason: Add CODE tags.
Sponsored Links
    #2  
Old 02-26-2018
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Quote:
Originally Posted by cindy01 View Post
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.

2. Relevant commands, code, scripts, algorithms:
Code:
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 ;;


3. The attempts at a solution (include all code and scripts):
Code:
#!/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


4. 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).
Moderator's Comments:
Linux Shell Scripting If-else and Case Please use CODE tags as required by forum rules.
Without CODE tags, all internal sequences of blank characters (i.e., <space>s and <tab>s) are converted to a single <space> and leading and trailing blanks are completely removed.
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.
The Following User Says Thank You to Don Cragun For This Useful Post:
cindy01 (02-26-2018)
Sponsored Links
    #3  
Old 02-26-2018
RudiC RudiC is online now Forum Staff  
Moderator
 
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.
The Following User Says Thank You to RudiC For This Useful Post:
cindy01 (02-26-2018)
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell scripting with case statement nareshreddy443 Shell Programming and Scripting 9 09-13-2017 02:59 PM
Use case insensitive variable in ksh shell scripting using sed or awk johnjs Shell Programming and Scripting 11 06-18-2012 11:38 AM
array + if in linux shell scripting CAch Shell Programming and Scripting 21 08-05-2011 06:05 PM
Shell Scripting Linux app1sxv Shell Programming and Scripting 1 01-19-2010 04:21 AM
How to handle case insensitive in shell scripting bacsi01 Shell Programming and Scripting 1 09-24-2009 05:57 AM



All times are GMT -4. The time now is 07:50 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?