The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-30-2009
guessingo guessingo is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 8
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
  #2 (permalink)  
Old 09-30-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,103
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.
  #3 (permalink)  
Old 09-30-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 735
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 (permalink)  
Old 09-30-2009
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,461
or even:

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

  #5 (permalink)  
Old 09-30-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,103
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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0