simple if/then issue in korn shell | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

simple if/then issue in korn shell

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-30-2009
guessingo guessingo is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 30 March 2014, 10:18 PM EDT
Posts: 55
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 09-30-2009
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 13 December 2014, 6:28 PM EST
Location: Zürich
Posts: 8,489
Thanks: 333
Thanked 1,050 Times in 916 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 09-30-2009
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 December 2014, 9:54 PM EST
Location: Amsterdam
Posts: 9,695
Thanks: 300
Thanked 2,491 Times in 2,230 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 09-30-2009
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 21 December 2014, 6:28 AM EST
Location: Paris
Posts: 4,471
Thanks: 17
Thanked 482 Times in 434 Posts
or even:

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

Sponsored Links
    #5  
Old 09-30-2009
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 13 December 2014, 6:28 PM EST
Location: Zürich
Posts: 8,489
Thanks: 333
Thanked 1,050 Times in 916 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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 02:19 AM.