Shell script question -- urgent

Hi guys,
I am facing a problem here with shell script.
I am trying to write a very simple script that reads a value from a user and if choice is 1 (for example) we copy file A from one location to another.
If the user choice is 2, we copy file B from another location.
To begin I tried to write a simple if statement but it didn't work.. here is what I did:

#!/bin/bash
echo "Please enter your choice: "
response=""
read $response
if [ $response=1 ]; then
echo "You chose 1"
elif [ response=2 ]
echo "You chose 2"
else
echo "Wrong choice"
fi

I get the error unexpected fi and sometimes unmatched if.
Anybody has a solution??
Another issue: When I try to run the shell script file using
$ ./test.sh
I get the message file was is not found ( I am sure it is there )
However, if I go to /bin and do:
$ bash file_path
it works but gives the above error.
Any suggestions for this too?

Regards
samiyousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

catchmeifuwantCommented:
I presume the error occure due to the semocoln in the  script...I have modified the script...check it out..

#! /bin/bash
echo "Please enter your choice: "
response=""
read $response
if [ "$response" = "1" ]
then
echo "You chose 1"
elif [ "$response" = "2" ]
then
echo "You chose 2"
else
echo "Wrong choice"
fi


********************

Once you save the script,change the mode and give execute permission

chmod 777 test.sh (This will avoid the file not found error)
0
sunnycoderCommented:
Hi samiyous,

> When I try to run the shell script file using
> $ ./test.sh
> I get the message file was is not found ( I am sure it is there )
make sure the script has execute permissions set
chmod +x test.sh

Cheers!
Sunny:o)
0
sunnycoderCommented:
samiyous,
> if [ $response=1 ];
you need to give a space on eithe side of = ... also it is a good idea to enclose the strings in " " although it is not mandatory
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

shivsaCommented:
#!/bin/bash
echo "Please enter your choice: "
response=""
read response
if [ $response=1 ]; then
echo "You chose 1"
elif [ response=2 ]; then
echo "You chose 2"
else
echo "Wrong choice"
fi
0
samiyousAuthor Commented:
Hi, I have tried the modified script by catchmeifuwant and also added the permissions. Now, I am using this version:

#! /bin/bash
echo "Please enter your choice: "
response=""
read $response
if [ "$response" = "1" ]
then
echo "You chose 1"
elif [ "$response" = "2" ]
then
echo "You chose 2"
else
echo "Wrong choice"
fi

However I got this error when I try to run:

$ bash /u03/oracle/le2021appl/xxx/11.5.0/sql/testing/test.sh
Please enter your choice:
1
': not a valid identifierx/11.5.0/sql/testing/test.sh: read: `
/u03/oracle/le2021appl/xxx/11.5.0/sql/testing/test.sh: line 8: syntax error near
 unexpected token `elif'
/u03/oracle/le2021appl/xxx/11.5.0/sql/testing/test.sh: line 8: `elif [ "$respons
'" = "2" ]
$

Still I cannot run it from its location, I get:
$ ./test.sh
: No such file or directory

Any ideas??
Regards
0
shivsaCommented:
could u try mu suggestion. it was working on mine.
0
samiyousAuthor Commented:
I did shivsa and It produced the following output:

$ bash  /u03/oracle/le2021appl/xxx/11.5.0/sql/testing/test.sh
Please enter your choice:
1
': not a valid identifierx/11.5.0/sql/testing/test.sh: read: `response
/u03/oracle/le2021appl/xxx/11.5.0/sql/testing/test.sh: line 7: syntax error near
 unexpected token `elif'
/u03/oracle/le2021appl/xxx/11.5.0/sql/testing/test.sh: line 7: `elif [ response=
' ]; then
$

By the way, so that you don't say re-install bash.. I am trying this code on 3 different machines!!! AND IT DOESN'T WORK
0
catchmeifuwantCommented:
Change this line

echo "Please enter your choice: "

to echo "Please enter your choice "  (remove the ":")

and check again...

0
catchmeifuwantCommented:
and also try executing it from the same directory where the sript is present..

cd /u03/oracle/le2021appl/xxx/11.5.0/sql/testing/
./test.sh
0
shivsaCommented:
#!/bin/bash
echo "Please enter your choice: "
response=""
read response
if [ $response = 1 ]; then
echo "You chose 1"
elif [ $response = 2 ]; then
echo "You chose 2"
else
echo "Wrong choice"
fi
# /tmp/1.sh
Please enter your choice:
1
You chose 1
# bash -version
GNU bash, version 2.05.0(1)-release (sparc-sun-solaris2.9)
Copyright 2000 Free Software Foundation, Inc.
0
catchmeifuwantCommented:
Try this....

#!/bin/bash
set -x                  -------- Comment /Uncomment for debugging

echo "Please enter your choice: "

read response
echo "Response is :" $response

if [ "$response" = "1" ]
then
echo "You chose 1"
elif [ "$response" = "2" ]
then
echo "You chose 2"
else
echo "Wrong choice"
fi
0
samiyousAuthor Commented:
Hi,
I have a very strange phenomenon happening..
When I write the first line:
#!/bin/bash
It always refuses to run from the directory where the sh is located and it produces the error:

$ chmod 777 *
$ test.sh
: No such file or directory

However, if I remove the first line, it runs but produces errors???
Here is the code I tried and the output:

#set -x                  -------- Comment /Uncomment for debugging
echo "Please enter your choice: "
read response
echo "Response is :" $response
if [ "$response" = "1" ]
then
echo "You chose 1"
elif [ "$response" = "2" ]
then
echo "You chose 2"
else
echo "Wrong choice"
fi


$ test.sh
Please enter your choice:
test.sh[3]: response^M: is not an identifier
Response is :
test.sh[5]: syntax error at line 8 : `elif' unexpected
$

I don't know why it didn't wait for me to provide response.
By the way what is the first line doing???
0
mbekkerCommented:
Hi samiyous,

A problem in your script is:

read $response

You must leave out the "$". The shell substitutes this line to: 'read ' because $response is "".

The examples from the others also doesn't contain a "$" after the read.

Good luck!
0
shivsaCommented:
The first line tell environment to run this script in bash shell.

set -x is used to debug scripts. it print the line which it is goin to execute.
0
catchmeifuwantCommented:
Note this :  response^M

try converting the file to unix format first before executing...this will remove the control charcters..

dos2unix test.sh
chmod 777 test.sh
./test.sh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiyousAuthor Commented:
It worked finally and the reason is that I was modifying the file on my machine and then putting it  (by ftp) to the target machine in binary mode!!!
So some characters were wrongly interpreted.

Thanks guys so much for the help.. it was really great

Does anyone knows how to specify AND OR and NOT conditions

thanks a lot
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.