[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 798
  • Last Modified:

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
0
samiyous
Asked:
samiyous
  • 5
  • 4
  • 4
  • +2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now