Solved

Shell script question -- urgent

Posted on 2003-11-24
16
752 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now