LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 05-08-2012, 08:43 AM   #1
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Rep: Reputation: 0
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
 
Old 05-08-2012, 09:46 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
Old 05-08-2012, 09:54 AM   #3
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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: .
 
Old 05-08-2012, 10:03 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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.
Old 05-08-2012, 10:11 AM   #5
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 05-08-2012, 10:18 AM   #6
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
thanks alot .. its my bad.. i got it executed. thanks all for tjhe reply.
 
Old 05-08-2012, 10:19 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
Old 05-08-2012, 10:21 AM   #8
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
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
 
Old 05-08-2012, 10:41 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rameshpaul_cog View Post
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
 
Old 05-08-2012, 10:43 AM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
Old 05-08-2012, 10:46 AM   #11
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
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
 
Old 05-08-2012, 10:59 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 05-08-2012, 11:03 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by unSpawn View Post
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?
 
Old 05-08-2012, 11:18 AM   #14
rameshpaul_cog
LQ Newbie
 
Registered: May 2012
Posts: 19

Original Poster
Rep: Reputation: 0
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.
 
Old 05-08-2012, 12:08 PM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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,,}
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Shell program question - if error then mailx khandu Programming 4 04-20-2012 06:55 PM
Suppressing error messages in BASH Shell swiftguy121 Linux - Software 4 05-25-2007 08:59 PM
bash shell script error redirect problem..... budword Programming 4 05-09-2007 07:32 PM
Error in bash shell ppy Programming 2 11-10-2004 12:35 AM
pesky bash shell error every time I log in illiniguy3043 Linux - Newbie 1 06-12-2004 01:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration