Convert to case statements from if/elif
May 20, 2014 at 4:50pm UTC
Hello, I wrote the case on code but it mistakes. I am not sure.
If/elif code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/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,
May 21, 2014 at 3:12pm UTC
Do you realise this is a subforum for C++ questions?
May 25, 2014 at 9:21pm UTC
mutexe, no. It's Unix/Forum Programming.
May 25, 2014 at 11:59pm UTC
Welcome to Unix Board wrote:In this forum, users can to talk about any topic related to programming in C++ for UNIX and Linux.
May 26, 2014 at 2:43am UTC
While I agree with mutexe and NT3 pointing out this is a C++ forum for Unix/Linux, I'm not the kind of guy to turn away someone after asking a question. I've been debating on whether to answer since NT3's reply, but I'm going to answer...Just try not to ask anymore non C++ *nix questions here.
Your first code is wrong, it should be:
From what I can tell (( )) is a bash (#!/bin/sh) construct but korn shell (#!/bin/ksh) still uses [[ ]]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/bin/ksh
you=$LOGNAME
hour=`date | awk '{print substr($4, 1, 2)}' `
print "The time is: $(date)"
if [[ hour -gt 0 && hour -lt 12 ]]; then
print "Good morning, $you!"
elif [[ hour -eq 12 ]]; then
print "Lunch time!"
elif [[ hour -gt 12 && hour -lt 16 ]]; then
print "Good afternoon, $you!"
else
print "Good night, $you!"
fi
case would be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/bin/ksh
you=$LOGNAME
hour=`date | awk '{print substr($4, 1, 2)}' `
print "The time is: $(date)"
case "$hour" in
[0-11]* ) print "Good morning, $you!"
;;
12 ) print "Lunch time!"
;;
[13-16]* ) print "Good afternoon, $you!"
;;
* )
print "Good night, $you!"
;;
esac
May 26, 2014 at 6:56am UTC
I'm looking at 'man ksh' (of ksh-20120801-10.el6_5.5.x86_64):
A case command executes the list associated with the first pattern that matches word. The form of the patterns is the same as that used for file-name generation .
In other words, [0-11]* is not a good pattern.
Test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
case "$hour" in
0[0-9] )
1[01] )
print "Good morning, $you!"
;;
12 )
print "Lunch time!"
;;
1[3-6] )
print "Good afternoon, $you!"
;;
* )
print "Good night, $you!"
;;
esac
May 26, 2014 at 11:29am UTC
keskiverto wrote:In other words, [0-11]* is not a good pattern.
I just use what I had learn from book examples. Looking online, tutorials I'm finding so far does recommend doing *[0-11] instead. Your code won't work though due to syntax error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
case "$hour" in
0[0-9] )
;&
1[01] )
print "Good morning, $you!"
;;
12 )
print "Lunch time!"
;;
1[3-6])
print "Good afternoon, $you!"
;;
* )
print "Good night, $you!"
;;
esac
Last edited on May 26, 2014 at 11:38am UTC
Topic archived. No new replies allowed.