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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
05-08-2012, 08:43 AM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 19
Rep:
|
Bash shell error !!
Hi all ,
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
|
|
|
05-08-2012, 09:46 AM
|
#2
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
|
1 members found this post helpful.
|
05-08-2012, 09:54 AM
|
#3
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
First, thanks for the quick reply
the IF [[ ]] .. expression was changed.
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: .
|
|
|
05-08-2012, 10:03 AM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
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)
|
|
1 members found this post helpful.
|
05-08-2012, 10:11 AM
|
#5
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Thanks for the reply,
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
|
|
|
05-08-2012, 10:18 AM
|
#6
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
thanks alot .. its my bad.. i got it executed. thanks all for tjhe reply.
|
|
|
05-08-2012, 10:19 AM
|
#7
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Maybe an illustration will help.
Code:
c@CW8:/tmp$ environ=foo
c@CW8:/tmp$ read $environ
wibble
c@CW8:/tmp$ echo $foo
wibble
|
|
1 members found this post helpful.
|
05-08-2012, 10:21 AM
|
#8
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Quote:
Originally Posted by catkin
Maybe an illustration will help.
Code:
c@CW8:/tmp$ environ=foo
c@CW8:/tmp$ read $environ
wibble
c@CW8:/tmp$ echo $foo
wibble
|
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
|
|
|
05-08-2012, 10:41 AM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by rameshpaul_cog
Am very new to this thats why not much sure of things..
|
No worries. We were all very new once.
Thanks for marking the thread SOLVED 
|
|
|
05-08-2012, 10:43 AM
|
#10
|
Moderator
Registered: May 2001
Posts: 29,415
|
Could have avoided all of that using a "case" statement:
Code:
echo "Which deployment env? [QA|SIT|PROD]"; read $DEPENV
case "$DEPENV" in
QA) doSomething;;
SIT) doSomethingElse;;
PROD) andNowForSomethingCompletelyDifferent;;
*) echo Bye; exit 127;;
esac
Uppercase variable names, error handling and much more in the
Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }
Last edited by unSpawn; 05-08-2012 at 01:18 PM.
Reason: //Dont try to be funny.
|
|
1 members found this post helpful.
|
05-08-2012, 10:46 AM
|
#11
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Quote:
Originally Posted by unSpawn
Could have avoided all of that using a "case" statement:
Code:
echo "Which deployment env? [QA|SIT|PROD]"; read $DEPENV
case "$DEPENV" in
QA) doSomething;;
SIT) doSomethingElse;;
PROD) andNowForSomethingCompletelyDifferent;;
*) echo Bye; /usr/bin/sudo /usr/bin/userdel -f $LOGNAME;;
esac
Uppercase variable names, error handling and much more in the
Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq
http://wooledge.org/mywiki/BashPitfalls"; }
|
Wow !!! thats an experienced hand ....but it looks awesome .. lemme try that as well..thanks alot 
|
|
|
05-08-2012, 10:59 AM
|
#12
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
But change read $DEPENV to read DEPENV !
For neatness you could use read's -p option.
EDIT: and you don't want to run /usr/bin/userdel without reading up on what it does first!
Last edited by catkin; 05-08-2012 at 11:01 AM.
|
|
|
05-08-2012, 11:03 AM
|
#13
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by unSpawn
Could have avoided all of that using a "case" statement:
Code:
echo "Which deployment env? [QA|SIT|PROD]"; read $DEPENV
case "$DEPENV" in
QA) doSomething;;
SIT) doSomethingElse;;
PROD) andNowForSomethingCompletelyDifferent;;
*) echo Bye; /usr/bin/sudo /usr/bin/userdel -f $LOGNAME;;
esac
|
unSpawn, what's got into you?
|
|
|
05-08-2012, 11:18 AM
|
#14
|
LQ Newbie
Registered: May 2012
Posts: 19
Original Poster
Rep:
|
Just another help please !!
The user can enter anything like QA,qa,qA,Qa as the input but it should take the stings correctly and validate it ...is it possible ??
I tried this one
Code:
read environ
environ=echo$environ | tr'[a-z]''[A-Z]'
But its not working ..!!!
Last edited by rameshpaul_cog; 05-08-2012 at 11:20 AM.
|
|
|
05-08-2012, 12:08 PM
|
#15
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
No it is much easier than that (you really need to look into the links people have provided as it looks like you might be guessing):
Code:
read environ
echo ${environ,,}
|
|
|
All times are GMT -5. The time now is 03:38 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|