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
Awk script in DOS and Linux behaves differently :( vidyak Windows & DOS: Issues & Discussions 2 04-01-2009 12:07 PM
"$variable" is not behaving as a string in my script yabhi_22 UNIX for Dummies Questions & Answers 3 02-06-2009 01:20 PM
Script behaving differently on two servers mhssatya UNIX for Advanced & Expert Users 5 09-13-2006 03:28 PM
Script behaving differently in Crontab.. newtoxinu Shell Programming and Scripting 4 10-01-2004 02:22 PM
Script behaving differently in Crontab.. newtoxinu UNIX for Advanced & Expert Users 1 09-30-2004 05:00 PM

 
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
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 04-22-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
Thumbs down Why is a variable behaving differently in ksh script.

Guys i have strange behaviour with command output being saved in a variable instead of a tmp file.

1. I suck command output into a variable

Sample command output


Code:
# cleanstats

DRIVE INFO:
----------

Drv    Type      Mount Time  Frequency   Last Cleaned         Comment
***    ****      **********  *********   ****************     *******
  0    hcart3*   51.9        0                N/A
  1    hcart3*   55.9        0                N/A
  2    dlt*      0.8         0                N/A
  3    dlt*      0.0         0                N/A
  4    dlt*      0.2         0                N/A
  5    dlt*      0.0         0                N/A

MEDIA INFO:
----------

media   media  robot  robot  robot  side/  optical  # mounts/      last
 ID     type   type     #    slot   face   partner  cleanings    mount time
-------------------------------------------------------------------------------
CLN206  DLT_CL NONE     -      -     -       -           0     12/26/2001 08:22
CLN207  DLT_CL NONE     -      -     -       -           0     03/10/2002 10:00
CLN205  DLT_CL NONE     -      -     -       -           0     08/18/2002 06:40
CLN703  DLT_CL NONE     -      -     -       -           0     03/29/2003 05:11
CLN701  DLT_CL NONE     -      -     -       -          20     00/00/0000 00:00
CLN635  DLT_CL NONE     -      -     -       -           0     11/21/2003 04:32
CLN219  DLT_CL NONE     -      -     -       -           0     07/13/2004 06:25
CLN636  DLT_CL NONE     -      -     -       -          14     01/22/2006 08:15
CLN211  DLT_CL TLD      1     21     -       -          12     10/31/2008 22:44
CLN209  DLT_CL NONE     -      -     -       -           0     12/13/2006 22:13
CLN210  DLT_CL NONE     -      -     -       -           0     10/19/2008 06:31

2. I then print that variable to screen for debug and its as expected
3. I then print that variable to an awk statement in an if test and it doesn't work as it should.
4. If the output is captured in a tempfile and the same awk statement is used but taking input from the tmpfile then it does work.

The below code shows one way which doesn't work and one which does. Can anyone shed any light as to why?
I'm obviously trying to use variables instead of tmpfiles everywhere.
I can't understand it because the print statement for debug shows the output as expected.


Code:
#!/bin/ksh

....script contents, variable assignment blah blah....

# Suck clean stats to variable
CSTAT=$(cleanstats)

# Or put in tmpfile
cleanstats > ${TMPFILE}

print "CSTAT contains [${CSTAT}]"  #DBG

# Check tape exists in library with free cleanings
# If tape is in unit but with no cleanings OR
# If tape is NOT in unit (TLD), the string returned is empty thus matching -z test

# (section 1)
if [[ -z "$( print ${CSTAT} | nawk '$3 == "TLD" && $8 != "0" {print}' )" ]];then
      .... do stuff ....
fi

# Above doesnt work, but below does

# (section 2)
#if [[ -z "$( nawk '$3 == "TLD" && $8 != "0" {print}' < ${TMPFILE} )" ]];then
#      .... do stuff ...
#fi

Using the sample output above will cause the first section to match when it shouldn't, yet the second section works and just passes by the if.

Anyone shed any light on it? IFS is set as newline as standard.

Cheers

Last edited by lavascript; 04-22-2009 at 10:23 AM..
 

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:43 AM.


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