Solved

Shell script question -- urgent

Posted on 2003-11-24
16
761 Views
Last Modified: 2013-12-26
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
Comment
Question by:samiyous
  • 5
  • 4
  • 4
  • +2
16 Comments
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9809183
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9809187
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9809198
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 24

Expert Comment

by:shivsa
ID: 9809213
#!/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
 

Author Comment

by:samiyous
ID: 9809239
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
 
LVL 24

Expert Comment

by:shivsa
ID: 9809271
could u try mu suggestion. it was working on mine.
0
 

Author Comment

by:samiyous
ID: 9809306
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
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9809313
Change this line

echo "Please enter your choice: "

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

and check again...

0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9809317
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
 
LVL 24

Expert Comment

by:shivsa
ID: 9809335
#!/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
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 9809369
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
 

Author Comment

by:samiyous
ID: 9809499
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
 
LVL 1

Expert Comment

by:mbekker
ID: 9809509
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
 
LVL 24

Expert Comment

by:shivsa
ID: 9809513
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
 
LVL 12

Accepted Solution

by:
catchmeifuwant earned 75 total points
ID: 9809517
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
 

Author Comment

by:samiyous
ID: 9809559
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 42
repeatEnd java challenge 42 84
canBalance challenge 34 83
Change to event 1 112
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question