LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2013, 11:39 AM   #31
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled

hey guys, im back again with more questions and help

here is my code:

Code:
clear
echo;echo;echo
echo -n Enter something here: " "
read x
hour="$(date +%H)"
case "$x" in
        "Directory")
                pwd
                ;;
        "Date")
                date
                ;;
        "Who")
                who
                ;;
        #"Time")
                 case "$hour" in
                        [ "$hour" -ge "7" ] && [ "$hour" -le "12" ])
                               echo "Morning"
                               ;;
                        [ "$hour" -ge "12" ] && [ "$hour -le "17" ])
                               echo "Afternoon"
                               ;;
                        [ hour -ge "17" ] && [ "$hour" -le "20" ])
                               echo "Evening"
                               ;;
               esac
               ;;
        "Quit")
                echo "Bitch"
                ;;
        *)
                [ ${#x} -gt 20 ] && echo "Over achiever!!!"
                ;;
        #*)
        #       [ $x =~ ^-?[0-9]+$ ] && echo "You entered a number."
        #       ;;

esac
for the most part, it works...the only part, or I should say, parts, that doesn't work are the lines that are commented out. what do I do to fix these and with your answer, could you also give an explanation as to what you did?

Thanks again

EDIT: I took the %M out to look for just the hour and I'm still getting an error at line 18 and this is what the error says. Explanation of what is wrong would be greatly appreciated, i've encountered this error so many times and can seem to never find the problem

Quote:
Enter something here: Time
./task03: line 18: syntax error near unexpected token `"$hour"'
./task03: line 18: ` [ "$hour" -ge "7" ] && [ "$hour" -le "12" ])'

Last edited by druadunc91; 02-11-2013 at 03:27 PM.
 
Old 02-11-2013, 11:48 AM   #32
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Using "date +%H%M" to determine the hour is not going to work - you are running both hour and minute into a single string. So the minimum value you get will be "100" (no minutes past 1AM). What you likely want is just %H (hours).
 
Old 02-11-2013, 03:23 PM   #33
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
so just by taking out the %M it should fix at least that one part since i'm just looking for the time between hours to determine if it is either morning, afternoon, or evening?
 
Old 02-11-2013, 03:28 PM   #34
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
That should do it.
 
Old 02-11-2013, 04:34 PM   #35
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
i tried that and it didn't work...you can look at my previous post with the code in it. i added the error i'm getting from the code and bolded the line in which the error occurred. also, when i uncomment out the code that I'm having problems with, it causes the rest of my commands to not work when I test my program. And the error I'm getting is the same one on line 18 whether I type in Date to run the date command or Who for the who command

Last edited by druadunc91; 02-11-2013 at 04:39 PM.
 
Old 02-11-2013, 04:39 PM   #36
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
You can't use "case" with an 'if'-like structure: you used it correctly the first time (when you matched against e.g. "Directory", "Date") but for the second one you'll need to use if/elif/else instead.
 
Old 02-11-2013, 04:42 PM   #37
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Snark1994 View Post
You can't use "case" with an 'if'-like structure: you used it correctly the first time (when you matched against e.g. "Directory", "Date") but for the second one you'll need to use if/elif/else instead.
that would be easier, but my instructor for this assignment wants us to specifically use case statements and nothing else. I understand that what I have for "Time" is the pattern for the case statement, but how do I get it to recognize the pattern and match the time with the corresponding time of day, such as if it is 7:01am, using military time since %H keeps it as a 24hr clock, to say morning?
 
Old 02-11-2013, 04:50 PM   #38
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
You could pattern-match, I guess:

Code:
case "$hour" in
    0*)
        echo "Morning"
        ;;
    1[7-9])
        echo "Evening"
        ;;
    1*)
        echo "Afternoon"
        ;;
esac
(not tested, but hopefully should work)

EDIT: Just seen that you use less than or equals, not less than - but seeing as this is a class assignment I'm sure you can figure out how to extend the examples I gave

Last edited by Snark1994; 02-11-2013 at 04:51 PM.
 
Old 02-11-2013, 04:57 PM   #39
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
Snark, thanks for the example. Now a question or two concerning your example. what does 1[7-9] mean? 7-9 is the pattern that the code is looking for and if it finds it, it would output evening and same thing for morning and afternoon? also, with your 1*) could I do 1[12-17] for afternoon, sticking with the military time that %H does?
 
Old 02-11-2013, 05:04 PM   #40
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Look at, say http://tldp.org/LDP/Bash-Beginners-G...ect_07_03.html.

1[7-9] should match 17, 18 and 19. 1* should match anything beginning with a '1' (which is why it is placed after 1[7-9]), but you could do 1[2-7] instead, yes (1[12-17] won't work).
 
Old 02-11-2013, 06:54 PM   #41
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
thanks for the help, its starting to click with me.

more questions. if there is no command line arguments, how would you get the program to output a specified command. example: you press return without typing anything in and the output is, say, the command who. how would you do that without getting too complex
 
Old 02-12-2013, 07:16 AM   #42
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
If I understand you correctly, match for an empty string and run

Code:
echo $(who)
 
Old 02-12-2013, 09:41 AM   #43
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Do the same match, but drop the echo... Just use the who command directly.

The only time the $(...) syntax is useful is if you are putting the result into a variable.
 
1 members found this post helpful.
Old 02-13-2013, 03:52 AM   #44
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by jpollard View Post
Do the same match, but drop the echo... Just use the who command directly.

The only time the $(...) syntax is useful is if you are putting the result into a variable.
Ah thanks, never realised that worked !
 
Old 02-14-2013, 01:56 PM   #45
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
thanks for all the help guys. more questions


here is my code. it all works, but im having a problem with one part.

Code:
clear
echo;echo;echo
echo -n Enter something here: " "
read x
a="$(date +%H)"
case "$x" in
        [0-9]*)
                case "$x" in
                        [0-9])
                                echo "You entered a single digit number"
                        ;;
                        [0-9]*[a-z]*)
                                echo "$x"
                        ;;
                        [0-9]*[A-Z]*)
                                echo "$x"
                        ;;
                        [0-9]*)
                                echo "Big number!!!"
                        ;;
                esac
        ;;
        "Directory")
                pwd
                ;;
        "Date")
                date
                ;;
        "Who")
                who
                ;;
        "Time")
                case "$a" in
                        [0700-1159])
                                echo "Morning"
                                ;;
                        [1200-1700])
                                echo "Afternoon"
                                ;;
                        [2100-0400])
                                echo "Evening"
                                ;;
                        [0400-0700])
                                echo
                                ;;
                esac
        "Quit")
                echo "Bitch"
                ;;
             *)
                [ ${#x} -gt 20 ] && echo "Over achiever!!!"
                ;;
esac
the part that im having problems with is my Time pattern. when i execute the program and type in "Time", it doesn't output anything. It should output, corresponding with the time, morning, afternoon, evening, or nothing if its between 4am and 7am. i know its falling through somewhere around that area, but I cant seem to find where its falling through. I'm not getting any errors, and everything else outputs as it should. just not that time part

EDIT: I have it as %H for military time so I reflected that within the nested case statement
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Share And Discover Cool Bash Tricks With Bash One-Liners LXer Syndicated Linux News 0 01-30-2012 10:50 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 06:52 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 02:14 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM


All times are GMT -5. The time now is 07:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration