Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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
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 ('').
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.