Convert to case statements from if/elif


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert to case statements from if/elif
# 1  
Old 05-20-2014
Convert to case statements from if/elif

Hello, I wrote the case on code but it mistakes. I am not sure.

If/elif code:
Code:
#!/bin/ksh
you=$LOGNAME

hour=`date | awk '{print substr($4, 1, 2)}'`

print "The time is: $(date)"

if (( hour > 0 && $hour < 12 ))

then

     print "Good morning, $you!"

elif (( hour == 12 ))

then

     print "Lunch time!"

elif (( hour > 12 && $hour < 16 ))

then

     print "Good afternoon, $you!"

else

     print "Good night, $you!"

fi

Change Case code:

Code:
#!/bin/ksh

you=$LOGNAME
hour=`date | awk '{print substr($4, 1, 2)}'`
print "The time is: $(date)"

case $hour in

(hour > 0 && $hour < 12 ) echo "Good morning, $you!";;
(hour == 12 ) echo "Lunch time!";;
(hour > 12 && $hour < 16 ) echo "Good afternoon, $you!;;
echo "Good night, $you!";;

esac

Did I mistake this the case?

Thanks,
# 2  
Old 05-20-2014
case does not work that way. It matches strings and does not contain logical statements. if / else is what you needed.

You can do it with case by pure string matching but it'd be awkward;

Code:
case "$hour" in
[0-9]|1[01]) echo "good morning" ;;
12) echo "lunch time"
1[3-6]) echo "good afternoon" ;;
*) echo "good night" ;;
esac

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-20-2014
Yes, a case statement can only use pattern matching, not numerical comparisons...
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-20-2014
An unrelated comment if you do not mind:

Code:
hour=`date | awk '{print substr($4, 1, 2)}'`

Pipes are great but sometimes unnecessary. Many times the first program have the figure or ability already built-in.

Code:
hour=`date "+%H"`

This User Gave Thanks to Aia For This Post:
# 5  
Old 05-20-2014
I got some errors.

Code:
#!/bin/ksh

you=$LOGNAME
hour=`"+%H"`
print "The time is: $(date)"

case "$hour" in
[0-9]|1[0-1]) echo "good morning" ;;
12) echo "lunch time"
1[3-7]) echo "good afternoon" ;;
*) echo "good night" ;;

esac

It said, "line 5: syntax error at line 10: `)' unexpected"
# 6  
Old 05-20-2014
The double semicolon is missing in
Code:
12) echo "lunch time" ;;

And, a bit earlier, date is missing.
# 7  
Old 05-20-2014
I forgot the ;; after "lunch time".

Also, your "+%H" command does not work. That's not a command, that's an argument. You should do this:

Code:
hour=$(date +%H)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To convert Lower case to Upper Case

There is a script where we pass the parameter in lower case: say: . ./scriptName pArameter #!/bin/ksh echo "`date` Entering $0 Reloading the $1 table " mname1=$1 (code to login MYSQL Database) Truncate table $mname1; exit ! Since now there is a limitaion of MYSQL that it accept... (5 Replies)
Discussion started by: ambarginni
5 Replies

2. Shell Programming and Scripting

If, elif, else statements help

I am in need of some help. I am not an advance scripter or programmer, I have novice knowledge. I cannot get a script to work as expected. The script basically does an FTP (which I have work perfectly) and writes to a log. However, I want the script to echo "Not Connected" if the FTP fails to... (2 Replies)
Discussion started by: anthonyon
2 Replies

3. Shell Programming and Scripting

Not able to exit from case statements

Hi all, I wrote the following simple shell script to perform addition, subtraction, multiplication and division. In the below program, i am not able to exit from the script Shell Script ----------- #!/bin/sh bgcal() { cal="" echo "Enter the Option Number: \c" read cal if then... (3 Replies)
Discussion started by: uxpassion
3 Replies

4. Shell Programming and Scripting

usage of case statement in place of IF elif...

Hi, I need to carry out the back up of the data if exists...(file size not equal to zero) i tried in this way but it is not successful....where am making the mistakes? and if possible can i use case syntax in place of "if" #!/bin/ksh filename=`TZ=CST+24 date +%Y%m%d` ZERO=0... (3 Replies)
Discussion started by: aemunathan
3 Replies

5. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

6. Shell Programming and Scripting

convert upper case to lower case in ascript

I have a package to install and the installation script which does it . The files/directories names in the script are all lower case but the actual package has everything in upper case - file names, directories . I don't want to rename directories and files in the package - it has a lot of them . ... (2 Replies)
Discussion started by: vz6zz8
2 Replies

7. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies

8. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

9. Shell Programming and Scripting

Problem with my case statements

Hi there, Im having some problems with this function, I pass two arguments to the function $1 $2 (Arguments are month and date inputted by the user) for some reason the case always fails... however in the cases defined below where it shouldnt fail the result is: if it fails with input... (6 Replies)
Discussion started by: Darklight
6 Replies

10. Shell Programming and Scripting

case statements

i need to use a case statement to do something when the user enters nothing at the prompt. i know about the if statement and that isnt' what i'm interested in using for this. i want to use case. heres the scenerio. a program asks a user for an input. i want to use a case statement to... (1 Reply)
Discussion started by: Terrible
1 Replies
Login or Register to Ask a Question

Featured Tech Videos