If condition - else is never executed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If condition - else is never executed
# 1  
Old 04-25-2015
If condition - else is never executed

In the following code snippet, 'if' part is always executed irrespective of whether I enter 'y' or 'n'. I am unable to figure out my mistake even after spending couple of hours. Can someone point out the mistake? Thank you!

Code:
echo "Do you want to delete?(y/n):"
read USERINPUT
echo $USERINPUT

if [ $USERINPUT="y" ]
then 
	echo "Deleted."
elif [ $USERINPUT="n" ]
then 
	echo "Not deleted."
fi

# 2  
Old 04-25-2015
Space are very important in shell scripts:
Code:
echo "Do you want to delete?(y/n):"
read USERINPUT
echo $USERINPUT

if [ "$USERINPUT" = "y" ]
then 
	echo "Deleted."
elif [ "$USERINPUT" = "n" ]
then 
	echo "Not deleted."
fi

The command: [ $USERINPUT="y" ] (assuming there are no whitespace characters in the expansion of $USERINPUT is a single argument that is not an empty string (which always evaluates to TRUE). With spaces you have a test for string equality. Note also the double quotes around the values read from your user. Without them, your script can break in unexpected ways if the user types in an empty line or types in a line containing more than one "word".
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-25-2015
Thanks a lot Don Cragun, for pointing out my mistake. It works now! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script is not getting executed

Hi All, I have written a programme where i need to work in one session but when that session get executed it doesnt execute other programme till I write exit. Below is the script #!/bin/bash echo 'Going to start' exectrusteduser.com sh.com cd /UAT_Logs/CDCI_LOGS/SEEDADM pwd echo... (6 Replies)
Discussion started by: ripudaman.singh
6 Replies

2. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Command is executed twice

Hi I try to run a command using bash script. The script should now look like this: #!/bin/bash case "$1" in start) sudo su - cispmgm -c "/usr/local/jdk/bin/java -Dworking.dir=/opt/ibm/cisp -Denvironment.target= -Xms512M -Xmx1024M -classpath... (3 Replies)
Discussion started by: nikolai.straess
3 Replies

4. Shell Programming and Scripting

Command is executed twice

Hi I try to run a command using bash script. The script should now look like this: #!/bin/bash case "$1" in start) sudo su - cispmgm -c "/usr/local/jdk/bin/java -Dworking.dir=/opt/ibm/cisp -Denvironment.target= -Xms512M -Xmx1024M -classpath... (1 Reply)
Discussion started by: nikolai.straess
1 Replies

5. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. AIX

Script not getting executed via cron but executes when executed manually.

Hi Script not getting executed via cron but executes successfully when executed manually. Please assist cbspsap01(appuser) /app/scripts > cat restart.sh #!/bin/ksh cd /app/bin date >>logfile.out echo "Restart has been started....." >>logfile.out date >>logfile.out initfnsw -y restart... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. Shell Programming and Scripting

ifdown not executed

I have a binary that runs ifdown (to bring down wireless interface). When i run it from php, it is not being run. Is it because the Apache server (php) is run as non-root user? Need Help! :confused: How can i fix this problem? (1 Reply)
Discussion started by: xerox
1 Replies

9. Shell Programming and Scripting

shell "if" condition not executed

Hi, The below script should should create a file "COUNTFILE.log" if not found. But I am trying to run this and it is not creating the file . #!/bin/sh find . -name "COUNTFILE.log" > /dev/null 2>&1 if ; then echo 0> COUNTFILE.log fi Please advice why it is not creating... (8 Replies)
Discussion started by: himvat
8 Replies

10. Solaris

which processes executed first

When we login to a solaris terminal then which processes executed first? I beleive that first system profile file and user .profile gets executed. So please let me know what happens exactly. The question may like this that which rocesses arre executed first while loging into unix server? (1 Reply)
Discussion started by: surjyap
1 Replies
Login or Register to Ask a Question