Specifying a range within case/esac


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Specifying a range within case/esac
# 1  
Old 08-12-2010
Specifying a range within case/esac

Complete Unix beginner here. I basically have this script -
Quote:
time1=`date +%l:%M`

case `date +%H` in
"00") time2="in the early morning" ;;
"01") time2="in the early morning" ;;
"02") time2="in the early morning" ;;
"03") time2="in the early morning" ;;
"04") time2="in the early morning" ;;
"05") time2="in the morning" ;;
"06") time2="in the morning" ;;
"07") time2="in the morning" ;;
"08") time2="in the morning" ;;
"09") time2="in the morning" ;;
"10") time2="in the morning" ;;
"11") time2="in the morning" ;;
"12") time2="in the afternoon" ;;
"13") time2="in the afternoon" ;;
"14") time2="in the afternoon" ;;
"15") time2="in the afternoon" ;;
"16") time2="in the afternoon" ;;
"17") time2="in the evening" ;;
"18") time2="in the evening" ;;
"19") time2="in the evening" ;;
"20") time2="at night" ;;
"21") time2="at night" ;;
"22") time2="at night" ;;
"23") time2="at night" ;;
esac

echo "The time is$time1 $time2"
This seems to work fine. I want to try and shorten it by making it something like this -

Quote:
time1=`date +%l:%M`

case `date +%H` in
[00-04] ) time2="in the early morning" #and so on...
esac
This isn't working. I think it's probably to do with the zero padding that `date +%H` gives me, but if I use `date +%k`, I get a space at the start of single numbers, so that doesn't work either... Can anyone tell me where I'm going wrong?

Thanks in advance.
# 2  
Old 08-12-2010
Hi.

You could shorten it as such:

Code:
time1=`date +%l:%M`

case `date +%H` in
0[0-4]) time2="in the early morning" ;;
0[5-9]|1[01]) time2="in the morning" ;;
1[2-6]) time2="in the afternoon" ;;
1[7-9]) time2="in the evening" ;;
2[0-3]) time2="at night" ;;
esac

echo "The time is$time1 $time2"

This User Gave Thanks to Scott For This Post:
# 3  
Old 08-12-2010
Quote:
Originally Posted by scottn
Hi.

You could shorten it as such:

Code:
time1=`date +%l:%M`

case `date +%H` in
0[0-4]) time2="in the early morning" ;;
0[5-9]|1[01]) time2="in the morning" ;;
1[2-6]) time2="in the afternoon" ;;
1[7-9]) time2="in the evening" ;;
2[0-3]) time2="at night" ;;
esac

echo "The time is$time1 $time2"

Thank you so much!

Just one more thing, could you explain what is going on in this line of your script? Specifically, the |1[01] part.

Quote:
0[5-9]|1[01]) time2="in the morning"
Edit: Actually, just worked it out. Thanks again!

By the way, is there an easy way to get the date command to show the st/nd/rd/th suffix for days?
# 4  
Old 08-12-2010
Looking through man date, I don't see that it has that option.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case -- esac number of arguments problem

hi Scripting experts, I am using case..esac in my script .. I have given 6 option..e.g. 1 2 3 4 5 and *, howerver my script works welll for 1st 4 options but for 5 it considers * and exists. Is there a maximum limit on number of options given for case ..esac? (1 Reply)
Discussion started by: sdgawande
1 Replies

2. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

3. 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

4. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

5. UNIX for Dummies Questions & Answers

Using a case statement with a range of numbers

Hey guys, I'm trying to setup a case statement that checks a value against 5 separate ranges of numbers. Here are the things I've tried with no success. case "$AGE" in "<10") echo "You're in this decade.";; "") echo "You're in this decade.";; "") echo "You're in... (5 Replies)
Discussion started by: fufaso
5 Replies

6. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

7. UNIX for Dummies Questions & Answers

how to break within a case/esac and stay in script

Wrote the following loop to but if I use exit, then I break entirely from my script, but instead I want to break from the case/esac and go to the next line in my script. I guess I need to know how to exit gracefully from a "while (true). Also, how can I allow the user to enter upper or lowercase... (4 Replies)
Discussion started by: tumblez
4 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. 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