Home Man
Search
Today's Posts
Register

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

Tags
bash, linux, shellscript

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 02-26-2018
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.
# 2  
Old 02-26-2018
Quote:
Originally Posted by cindy01
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)
# 3  
Old 02-26-2018
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)
Login to Reply

« Previous Thread | Next Thread »
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
[Solved] Change Upper case to Lower case in C shell rockytodd Shell Programming and Scripting 3 11-04-2010 12:57 PM
Linux/Unix shell scripting vs Perl trento17 Shell Programming and Scripting 6 07-19-2010 03:36 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
Difference between Linux and Unix and also C and C++ and also about Shell Scripting madhav Linux 3 07-10-2006 06:42 PM
Shell scripting for Unix and/or Linux FastFood Shell Programming and Scripting 2 11-13-2002 05:07 AM
lower case to upper case string conversion in shell script dchalavadi UNIX for Dummies Questions & Answers 3 05-29-2002 12:07 AM


All times are GMT -4. The time now is 01:44 PM.

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