Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

simple if/then issue in korn shell

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-30-2009
guessingo guessingo is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 26 April 2016, 3:13 PM EDT
Posts: 57
Thanks: 1
Thanked 0 Times in 0 Posts
simple if/then issue in korn shell

When I run this, it tests to Y, but why? If I take the double quotes off of test, i get the same.

# !/bin/ksh
TEST="N"
if [ "$TEST"="Y" ]; then
echo $TEST" yes"
else
echo "no"
fi
Sponsored Links
    #2  
Old Unix and Linux 09-30-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 5 December 2016, 10:48 PM EST
Location: Zürich
Posts: 8,598
Thanks: 343
Thanked 1,093 Times in 949 Posts
Hi.

Code tags would make this readable (i.e. I see three errors at a glance, and it's not obvious if you typed this or pasted it)..

The error is


Code:
"$TEST"="Y"

This would evaluate to true (as a non-empty string in itself.)

Should be


Code:
[ "$TEST" = "Y" ]

(with spaces either side of the =)

You also have a space between # and ! on line 1.
Sponsored Links
    #3  
Old Unix and Linux 09-30-2009
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 6 December 2016, 11:29 PM EST
Location: Amsterdam
Posts: 11,103
Thanks: 440
Thanked 3,111 Times in 2,751 Posts
Also worth mentioning is that the preferred method for testing in Korn Shell is to use the [[ ... ]] construct instead of [ ... ]. There are several advantages, one of them is that it is much less picky about quoting and empty strings. For instance you could write your script without any double quotes:

Code:
#!/bin/ksh
TEST=N
if [[ $TEST = Y ]]; then
  echo $TEST yes
else
  echo no
fi

    #4  
Old Unix and Linux 09-30-2009
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 7 December 2016, 4:52 AM EST
Location: Outside Paris
Posts: 4,799
Thanks: 18
Thanked 589 Times in 522 Posts
or even:

Code:
#!/bin/ksh
TEST=Y
[[ $TEST = Y ]] && echo $TEST yes || echo no

Sponsored Links
    #5  
Old Unix and Linux 09-30-2009
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 5 December 2016, 10:48 PM EST
Location: Zürich
Posts: 8,598
Thanks: 343
Thanked 1,093 Times in 949 Posts
There's something to be said for both [[ ... ]] and && || and something against.


Code:
/root/tmp # [[ Y = Y ]] && paaa y || echo b
-ksh: paaa: not found [No such file or directory]
b

Personally I prefer [ ... ] and quotes where needed, and an if [ ... ]; then else where the && part could itself fail.

[[ ]] is testing the test, and leaves you not knowing necessarily why the inner test failed. Was the condition false? Was the variable not declared when it should have been?

Just a preference.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help with simple korn scripting samnyc Shell Programming and Scripting 4 01-15-2009 05:21 PM
simple issue.. the_learner UNIX for Advanced & Expert Users 2 01-17-2008 04:44 PM
Unix Korn Shell Array Issue (SunOS) Janus Shell Programming and Scripting 5 11-21-2006 08:19 PM
how to convert from korn shell to normal shell with this code? forevercalz Shell Programming and Scripting 21 11-23-2005 02:18 AM
KORN Shell - Spawn new shell with commands frustrated1 Shell Programming and Scripting 2 04-20-2005 03:23 PM



All times are GMT -4. The time now is 08:26 AM.