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.
Am trying to give a multiple condition for an user input , and am using Nested IF for that.
But somehow, am getting command not found error , i searched in the net and tried whatever i can. but no luck ? its bit urgent and this is an simple script but not getting an solution. Kindly assist
Code:
#!/bin/bash
echo "Please select the environment for deployment [ QA, SIT , PROD ]"
read $env
if ( "$env" -eq QA ); then
cp -rf * /dest/
echo " QA config file has been copied"
fi
if [ "$env" == SIT ] ; then
cp * /dest/
echo "SIT Config file has been copied"
fi
if [ "$env" == PROD ] ; then
cp * /dest/
echo " PROD config file has been copied "
else
echo " Please enter the correct environment "
Any help would be really appreciated.
whats the problem in the script ??
Last edited by rameshpaul_cog; 05-08-2012 at 09:40 AM.
Reason: Code separation
read $env says "read into the variable named by the value of env". The $ operator can be thought of as meaning "the value of".
Couple of problems with if ( "$env" -eq QA ); then. Firstly ( <commands> ) means "start a subshell and run <commands> in it". Elsewhere you correctly use [ <test expression> ] -- so why not here?! Secondly -eq is a numeric equality test operator. To compare strings, use = or ==.
Incidentally, the more recent [[ <test expression> ]] is better than the traditional [ <test expression> ] for reasons explained here. Good to get into the habit of using it.
Finally the last fi is missing.
Please use CODE tags when posting code to preserve the indentation and make it easier to read. This can most easily be done by switching to advanced mode and using the # button.
and even after the fi is given in the last its giving error as " syntax error near unexpected token `else' "
what would be the reason ?
I somehow cut shorted the code for testing purpose :
Code:
#!/bin/bash
# Read product area
parm_env="$env" | tr '[a-z]' '[A-Z]'
echo "Please select the environment for deployment [ QA, SIT , PROD ]"
read $env
if [[ "$env" == "QA" ]]
then
cp -rf * /dest/
echo "QA config file has been copied"
fi
else
echo " Please enter the correct environment ":
Last edited by rameshpaul_cog; 05-08-2012 at 09:56 AM.
Reason: .
Please re-read the information supplied by catkin, specifically the first line. What might not be clear is, when you store value in a variable in bash the variable is written
plainly, ie just env (although bad choice for a variable name as it is also a command, but I digress), whereas $env is used to retrieve the value stored in the variable.
In your second example, have a close look at where you have placed the "fi", remembering it terminates an "if" condition which may or may not include an else (hint)
I got that now, I have changed the variable name.
But somehow the user input is not taken into account and its giving the else output only ?
Code:
echo "Please select the environment for deployment [ QA, SIT , PROD ]"
read $environ
if [[ "$environ" == QA ]]
then
cp -rf * /dest/
echo "QA config file has been copied"
else
echo " Please enter the correct environment ":
fi
yeah exactly i include $ while read itself thats the mistake.
and later i removed it and it looks good.
Am very new to this thats why not much sure of things..
thanks for letting me know
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.