LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 01-25-2013, 01:56 PM   #1
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Rep: Reputation: Disabled
Bash help


Hey there,

I'm new here and currently taking Bash Programming in college. I'm having a little problem right now with my code

i'm trying to have my program output the date/time whenever a user inputs the word "time" when my program prompts them to enter something

so far this is what the code looks like and I'm getting an integer expression expected


Quote:
clear
echo; echo; echo
echo -n Enter something: ' '
read x
if [ "$x" -eq "time"]
then
echo date
fi
what am I forgetting?
 
Old 01-25-2013, 02:13 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 1,692

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425
-eq is a numeric comparison.

You want either == (or = when used in an expression), also suggest space after the closing " on "time".

Run down the bash manpage to "Conditional Expressions"
 
1 members found this post helpful.
Old 01-25-2013, 02:15 PM   #3
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
that worked, thanks! I'll be back for more help soon, probably

question: with if statements, if you have an if inside an if, do you need a fi at the end of the inner if statement

example:

Quote:
if [blah blah]
then
echo `blah`
if [blah1 blah1]
then
echo `blah1`
fi
fi

Last edited by druadunc91; 01-25-2013 at 02:26 PM.
 
Old 01-25-2013, 02:28 PM   #4
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 166

Rep: Reputation: 34
Quote:
Originally Posted by druadunc91 View Post
that worked, thanks! I'll be back for more help soon, probably

question: with if statements, if you have an if inside an if, do you need a fi at the end of the inner if statement

example:
yes.
 
Old 01-25-2013, 03:35 PM   #5
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
Ok,

so more problems.

Here's my code:
Quote:
clear
echo;echo;echo
echo -n Enter something: ' '
read x
if [ "$x" = "time"]
then
echo `date`
elif [ "$x" = "Directory"
then
echo `pwd`
fi
else [ "$x" = "Who"]
then
echo `who`
fi
When I enter "Directory" as my input, it tells me where I am at in my assignment. So the the output for me looks like this:
Quote:
/home/druadunc/csc490/bin
but there is more. after that it shows:
Quote:
./task02: line 12: syntax error near unexpected token 'else'
so im getting the correct output, but im getting an error message for my last if that I have. the statement bolded is the one with the error message

Last edited by druadunc91; 01-25-2013 at 03:43 PM.
 
Old 01-25-2013, 03:41 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 1,692

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425
And the question is?
 
Old 01-25-2013, 04:05 PM   #7
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Slack14_64_Multilib
Posts: 2,715
Blog Entries: 3

Rep: Reputation: 626Reputation: 626Reputation: 626Reputation: 626Reputation: 626Reputation: 626
Quote:
Here's my code:...
doesn't
Code:
elif [ "$x" = "Directory"
need a closing "]"?, so
Code:
elif [ "$x" = "Directory" ]
?

2 of the 3 braces need spaces before the closing right bracket.
time" ] and who" ] NOT
time"] or who"]

http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

Last edited by Habitual; 01-25-2013 at 04:08 PM.
 
Old 01-25-2013, 04:06 PM   #8
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
doesn't
Code:
elif [ "$x" = "Directory"
need a closing "]"?, so
Code:
elif [ "$x" = "Directory" ]
?
sorry, i forgot to type then in the post...i have it in my coding
 
Old 01-25-2013, 04:09 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by druadunc91 View Post
Code:
clear
echo;echo;echo
echo -n Enter something: ' '
read x
if [ "$x" == "time" ]  # you need == and not = / missing space between " and ]
then 
echo `date`
elif [ "$x" == "Directory" ]  # == -> = / missing square bracket
then
echo `pwd`
# if             # you need to use elif instead of if  else
#else
elif [ "$x" == "Who" ]  # == -> = / also needs an extra space.
then
echo `who`
fi
And those are to make the code work.

You might want to have a look at these:

Bash resources:
 
2 members found this post helpful.
Old 01-25-2013, 04:10 PM   #10
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
thanks for the help and info druuna!

on the who command, how do you get each user to appear on their own line so its not all mashed up?

Last edited by druadunc91; 01-25-2013 at 04:13 PM.
 
Old 01-25-2013, 04:27 PM   #11
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,408

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
Quote:
Originally Posted by druadunc91 View Post
thanks for the help and info druuna!

on the who command, how do you get each user to appear on their own line so its not all mashed up?
You don't need to use echo for any of those commands, as that's what they'll do anyway. Just run the command, eg:
Code:
elif [ "$x" == "Who" ]
then
   who
fi
Same goes for date and pwd. You use the back ticks or $() when you want to execute something and pass the output into something else, such as a variable for parsing later on. Passing it into echo is rather pointless, as that's what the command is going to do anyway (plus echo will jumble up the line ends).

You may also want to start indenting your if blocks, it makes larger codes infinitely easier to read.

Last edited by suicidaleggroll; 01-25-2013 at 04:33 PM.
 
Old 01-26-2013, 02:08 PM   #12
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
added another elif statement, because my assignment seems to contain a lot of them. Anyways, all my scripts are working except for the last elif statement.

This is the error I get when I execute the script using the "trigger" work if you will.

Quote:
line 20: syntax error near unexpected token `elif'
line 20: elif [ "$x" == "Quit" ]'
Thats the error, section of code it goes to

Quote:
elif [ "$x" == "Quit' ]
then
echo Something
so, whats wrong with the script? I even tried changing echo Something to just `exit` to see if it would work, and it doesn't
 
Old 01-26-2013, 02:24 PM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by druadunc91 View Post
[code]elif [ "$x" == "Quit' ]
then
echo Something
You can't mix double and single quotes. A set of either has their own behavioural purpose (in short: one does allow expanding (""), the other doesn't ('').
 
Old 01-26-2013, 02:28 PM   #14
druadunc91
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Original Poster
Rep: Reputation: Disabled
That was my bad on typing here. My script has "". Still have same error
 
Old 01-26-2013, 02:30 PM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Please post the complete script, or at least the complete if-then-elif-fi part that doesn't work. Also post the exact error it throws.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 09:50 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM


All times are GMT -5. The time now is 05:10 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration