Script interacts with user , based on user input it operates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script interacts with user , based on user input it operates
# 1  
Old 11-15-2012
Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled.


Code:
 
#!/bin/sh
echo " Enter your choice  to continue y/Y  OR n/N to quit "
read A
if [ "$A" = "y" || "$A" = "Y" ]
then
echo " user requested to continue "
##some commands
else
echo " User requested to cancel"
exit 1
fi
exit 0

above code is throwing error, please suggest
# 2  
Old 11-15-2012
Correction:-
Code:
if [[ "$A" = "y" || "$A" = "Y" ]]

# 3  
Old 11-15-2012
i tried its throwing error, marked in red


Code:
#!/bin/sh
echo " Enter your choice  to continue y/Y  OR n/N to quit "
read A
if [ [  "$A" = "y" || "$A" = "Y" ] ]
then
echo " user requested to continue "
##some commands
else
echo " User requested to cancel"
exit 1
fi
exit 0

i passed parameter as Y, it is throwing below error

Code:
 
[ : 14 : missing ]

# 4  
Old 11-15-2012
I did not put any spaces between the square brackets! Remove the spaces and re-try.
# 5  
Old 11-15-2012
You need two "==" in your if statements. A single "=" assigns whatever is after to whatever is before. Double "==" means "is equal to".

if [[ "$A" == "y" || "$A" == "Y" ]]

---------- Post updated at 12:45 PM ---------- Previous update was at 12:43 PM ----------

Quote:
Originally Posted by orangeSunshine
You need two "==" in your if statements. A single "=" assigns whatever is after to whatever is before. Double "==" means "is equal to".

if [[ "$A" == "y" || "$A" == "Y" ]]
Actually, I guess it will work either way but two "==" would be a good habit to get into.
# 6  
Old 11-15-2012
[[ .. ]] is not POSIX syntax and as such should not be used with #!/bin/sh The correct syntax would be:
Code:
if [ "$A" = "y" ] || [ "$A" = "Y" ]
then


--
The correct bash/ksh syntax would be
Code:
if [[ $A == "y" || $A == "Y" ]]
then

or
Code:
if [[ $A == [yY] ]]
then


Last edited by Scrutinizer; 11-15-2012 at 03:43 PM..
# 7  
Old 11-15-2012
try this
if [ "$A" -eq "y" || "$A" -eq "Y" ]
and check spaces
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automaticaly create function based off user input

I am trying to create a bash script that will create new function by using the user input. The below will create the necessary files in the correct format, however when it comes to the # create function I am at a loss. If the name entered was NEWNAME and the genes were GENE1,GENE2 then two files... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

Help Me. The script should not exit until the user gives an input.

Hi everyone, I'm new here and just a beginner in linux scripting. Just want to ask for help on this one. I am trying to create a script that will accept user input (year-month and user/s). I wanted to have the script to continue running, until the user inputs a DATE and name/s of user/s. ... (2 Replies)
Discussion started by: Helskadi
2 Replies

4. UNIX for Dummies Questions & Answers

Split files based on user input

Hi All, I have a UNIX script which reads "PxyType" (read PxyType) as input from user and a file (eg : "File.json") with the list all PxyType's. Based on the user input for "PxyType" in "File.json", I want to redirect each matched line to a different file ("File1,2,3,..json"). Can you... (7 Replies)
Discussion started by: Deena1984
7 Replies

5. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range. I dont know how to go about this. I am hoping that the... (5 Replies)
Discussion started by: lostincashe
5 Replies

7. Shell Programming and Scripting

Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far,... (0 Replies)
Discussion started by: DC Slick
0 Replies

8. Shell Programming and Scripting

User Input Shell Script

Hello I am trying to create a user input shell scipt. The objective is user should enter the circuit number and the input is saved in a log file. If the user does not enter anything then the question should prompt it until the circuit no. is entered. Can any one please correct the code below.... (3 Replies)
Discussion started by: sureshcisco
3 Replies

9. Shell Programming and Scripting

Update LDIF User info based on Test User Certs ID's

Hi I need help.......... I have an Sun One Directory server LDIF file with 5000 user entries, I need to change the data to match Test ID's, so I can run a perf test. I'm way out of my league as I have not done any scripting for 10 years. There are four entries for each user in the file... (3 Replies)
Discussion started by: Macdaddy99
3 Replies

10. Shell Programming and Scripting

User input for execution of script

Hi, I need to get the user input and execute a particular script based on the input provided. For E.g. When I execute the script say Test.sh it should prompt "For which country I need to execute the script? (US/India)" Based on the input as US or India from the user the execution of... (8 Replies)
Discussion started by: yoursdavinder
8 Replies
Login or Register to Ask a Question