Operating System - HP-UX
1831435 Members
3372 Online
110025 Solutions
New Discussion

Re: what is wrong with this script

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

what is wrong with this script

#!/bin/ksh

for xyz in `cat filelist`
do
con=`echo $xyz`
if [[ $con == "s" ]]
then
echo "in then"
else
echo "in else"
fi
done

I don't know what is the wrong with the script,
the problem is the login always fall in "else" section even though con="s"?

Please help me out. This script is running on linux serverm, but using /bin/ksh.


none
10 REPLIES 10
Victor Fridyev
Honored Contributor

Re: what is wrong with this script

Hi,

Why do you use double brackets ?

The right form is :
if [ $con=="s" ]; then

etc.

HTH
Entities are not to be multiplied beyond necessity - RTFM
A. Clay Stephenson
Acclaimed Contributor

Re: what is wrong with this script

First, you don't need con; simply use xyz and forget the con=`echo $xyz`

Secondly, it's "=" not "=="

Thirdly, protect you if's with quotes so that null variables don't kill you.


for xyz in $(cat filelist)
do
if [[ "${xyz}" = "s" ]]
then
echo "in then"
else
echo "in else"
fi
done
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: what is wrong with this script

Hi,

== is to be replaced with =.

It should work then.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Hanry Zhou
Super Advisor

Re: what is wrong with this script

Victor,

If I chage the way you suggested, the logic will forever fall in "then" section even though $con is not equal to "s"
none
Hanry Zhou
Super Advisor

Re: what is wrong with this script

clay,

the reason I use con=`echo $xyz` is because I need to run a command based on the value of $xyz, and assign the result to "con", so in my realy case, it is not con=`echo $xyz`. I just use it to replace my real command.

2nd, if I change the way you suggested, it will forever fall in "else" section.

none
Hanry Zhou
Super Advisor

Re: what is wrong with this script

If I have the string like " s", "s ", or " s ", how do I get rid of the empty strings in front or after "s"?
none
A. Clay Stephenson
Acclaimed Contributor

Re: what is wrong with this script

Okay do this to strip the spaces:

con=$(echo "${con}" | tr -d ' ')

If it ain't broke, I can fix that.
Tim D Fulford
Honored Contributor

Re: what is wrong with this script

Or sed

con=$(sed "s/\ //g" $(echo $xyz))

Tim
-
Hein van den Heuvel
Honored Contributor

Re: what is wrong with this script

Now that you mention spaces, indicating a worry about detailed matching you should probably start to look at regular expression.
You may want to replace the whole job with some perl. For example:

perl -ne 'if (/^\s*s\s*$/) {print "then: $_" } else {print "else: $_"}' filelist

or

perl -pe 'print (("s" eq split[0])? "then: " : "else: ")' x filelist

This looks for a line with a single "s" and optional whitespace.

re explanation:
^ = begin of line
\s = whitespace (space, tab)
\s* = zero or none whitespace
s = "the real McCoy"
$ = end of line

In the othere solution: split = splits default input line ($_) for words based on whitespace. The [0] selects the firs word.

perl arguments

-e = "script on command line"
-n = implied loop through STDIN, no print
-p = print $_ at end of implied loop itteration through STDIN

Cheers,
Hein.

perl -pe 'if (/^\s*s\s*$/) {print "then: "} else {print "else: "}' filelist


perl -pe 'print ((/^\s*s\s*$/)? "then: " : "else: ")' filelist
curt larson_1
Honored Contributor
Solution

Re: what is wrong with this script

let see
tics for comand subsitiution are obsolete
echo statement is obsolete
== for testing of equality isn't implemented

your script should something like

#!/usr/bin/ksh

for xyz in $(cat filelist)
do
con=${print $xyz}
#although con="$xyz" does the samething only faster

if [[ "$con" = "s" ]]
then
print "in then"
else
print "in else"
fi
done

and for removeing characters
print "$xyz" | tr -cd "[:alnum:]"
which will delete all characters that aren't alphanumeric