LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash help (https://www.linuxquestions.org/questions/linux-newbie-8/bash-help-4175447215/)

druadunc91 01-25-2013 01:56 PM

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?

jpollard 01-25-2013 02:13 PM

-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"

druadunc91 01-25-2013 02:15 PM

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

YankeePride13 01-25-2013 02:28 PM

Quote:

Originally Posted by druadunc91 (Post 4877571)
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.

druadunc91 01-25-2013 03:35 PM

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

jpollard 01-25-2013 03:41 PM

And the question is?

Habitual 01-25-2013 04:05 PM

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

druadunc91 01-25-2013 04:06 PM

Quote:

Originally Posted by Habitual (Post 4877640)
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

druuna 01-25-2013 04:09 PM

Quote:

Originally Posted by druadunc91 (Post 4877617)
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:

druadunc91 01-25-2013 04:10 PM

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?

suicidaleggroll 01-25-2013 04:27 PM

Quote:

Originally Posted by druadunc91 (Post 4877643)
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.

druadunc91 01-26-2013 02:08 PM

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

druuna 01-26-2013 02:24 PM

Quote:

Originally Posted by druadunc91 (Post 4878113)
[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 ('').

druadunc91 01-26-2013 02:28 PM

That was my bad on typing here. My script has "". Still have same error

druuna 01-26-2013 02:30 PM

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.


All times are GMT -5. The time now is 11:16 PM.