Linux Shell Scripting If-else and Case


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Linux Shell Scripting If-else and Case
# 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:
Mod Comment 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 03: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:
Mod Comment 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.
This User Gave Thanks to Don Cragun For This Post:
# 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.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers. int=Server1,Server2,Server3 qa=Server4,Server5,Server6 prod=Server7,Server8,Server9 echo "Enter the Environment i.e int,qa,prod" read env case $env in int) ## Need command where all the... (9 Replies)
Discussion started by: nareshreddy443
9 Replies

2. Shell Programming and Scripting

Use case insensitive variable in ksh shell scripting using sed or awk

I am using a variable called $variable in a pattern search to print from a starting variable to a constant value. the variable search should be case in sensitive. i tired using Ip at the end in the below command. but in ksh it is not working. sed -n "/$variable/,/constant/p" file i also... (11 Replies)
Discussion started by: johnjs
11 Replies

3. Shell Programming and Scripting

array + if in linux shell scripting

Hi, I am having two set of files with different number of columns and rows. A set of files have only single row with 20 columns. B set of files have 1000s of rows with 5 columns. both set contains equal number of files. I want to save all the 20 columns of A in variables one by one and... (21 Replies)
Discussion started by: CAch
21 Replies

4. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

5. Shell Programming and Scripting

Linux/Unix shell scripting vs Perl

Hi, I have general question: i have good working Perl script with .pl extension, and now I have to migrate all to another Linux box, and I was told that I can use only shell scripting, so I'm not sure how different those two things are, will it work without any changes . Is there anything smart I... (6 Replies)
Discussion started by: trento17
6 Replies

6. Shell Programming and Scripting

Shell Scripting Linux

In shell Can I pass " to a program ? If not what is the work around ..? Can I define * as a wild card charecter that will return all the values from the files ..? I am trying to create a tool that will return the values from a property file . This tool has to support all the wild card... (1 Reply)
Discussion started by: app1sxv
1 Replies

7. Shell Programming and Scripting

How to handle case insensitive in shell scripting

Hi All, I am new to this forum also new to shell scripting. I have a requirement to read a text from a file which can be in any case like LOGFILE or LogFile or logfile or lOgfILE etc.. Can you guys help me out. Thanks, Bacsi01 (1 Reply)
Discussion started by: bacsi01
1 Replies

8. Linux

Linux/Unix Shell Scripting Book

I want to learn Linux/Unix shell scripting, I searched this forum but got some results for Unix Admin books and general Linux books. Would someone recommend a good Linux Shell Scripting book? I did order one book A Practical Guide to Linux(R) Commands, Editors, and Shell Programming ... (4 Replies)
Discussion started by: thoughts
4 Replies

9. Shell Programming and Scripting

Shell scripting for Unix and/or Linux

Anyone here knows any reasonably priced courses for independent (not corporate) users that can point me to. Already know Unix commands/vi/etc but now need scripting too. Any replies will be appreciated. (2 Replies)
Discussion started by: FastFood
2 Replies

10. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question