- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: what is wrong with this script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 04:58 AM
06-15-2004 04:58 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 05:05 AM
06-15-2004 05:05 AM
Re: what is wrong with this script
Why do you use double brackets ?
The right form is :
if [ $con=="s" ]; then
etc.
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 05:06 AM
06-15-2004 05:06 AM
Re: what is wrong with this script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 05:08 AM
06-15-2004 05:08 AM
Re: what is wrong with this script
== is to be replaced with =.
It should work then.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 05:11 AM
06-15-2004 05:11 AM
Re: what is wrong with this script
If I chage the way you suggested, the logic will forever fall in "then" section even though $con is not equal to "s"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 05:21 AM
06-15-2004 05:21 AM
Re: what is wrong with this script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 05:41 AM
06-15-2004 05:41 AM
Re: what is wrong with this script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 08:07 AM
06-15-2004 08:07 AM
Re: what is wrong with this script
con=$(echo "${con}" | tr -d ' ')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 08:14 AM
06-15-2004 08:14 AM
Re: what is wrong with this script
con=$(sed "s/\ //g" $(echo $xyz))
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 11:16 AM
06-15-2004 11:16 AM
Re: what is wrong with this script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2004 01:34 PM
06-15-2004 01:34 PM
Solutiontics 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