Why is a variable behaving differently in ksh script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why is a variable behaving differently in ksh script.
# 1  
Old 04-22-2009
PHP 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..
# 2  
Old 04-22-2009
Power

Oddly enough just setting IFS to nothing seems to have fixed it.

I'm not entirely sure why, but gathered it would around the field separator.

I tried doing this :-

Code:
oldIFS=$IFS
IFS="\n\r"

if [ ....blah

IFS=$oldIFS

.and noticed it worked correctly but the print output from awk was missing n's and r's.

Through trial and error i found setting
Code:
IFS=""

made it work.

Anyone know why?

cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why awk perform differently when using variable?

Hi Gurus, I hit a hard block in my script. when using awk command with variable, I got different result. Please see below: my test file as below: $ cat demofile.txt filename-yyyy-abcd filename-xxx-week-pass filename-xxx-week-run for testing purpose, I put 3 awk command in one script.... (7 Replies)
Discussion started by: ken6503
7 Replies

2. Filesystems, Disks and Memory

Different partitions of a drive behaving differently in Windows

I have a memory card of my Nokia N73 attached to laptop. There are a few partitions. Why all partitions behave differently? As clear from the attachments, for some partition, delete option is disabled. See 'Disk 1' which is my memory card. Here, patition 'G' (CHECK), i created in windows. The... (6 Replies)
Discussion started by: ravisingh
6 Replies

3. Shell Programming and Scripting

Same KSH behaving differently on diff servers

HI all I have written a ksh to execute PL/sql procedure and generate the log file. The script is working fine to the extent of calling the taking input, executing PL/SQL procedure. On one server the log file is getting generated properly. i,e it shows the DBMS output . The log file size was... (9 Replies)
Discussion started by: ramakrishnakini
9 Replies

4. Shell Programming and Scripting

jobs command behaving differently in script

Here is my test script: #!/bin/sh result=`jobs` echo " Jobs: "$result result=`ls` echo " LS "$result Here is the output: Jobs: LS 0 1 2 3 4 5 6 7 gcd initialize.sh #inter_round_clean.sh# inter_round_clean.sh inter_round_clean.sh~ look parallel_first_run.sh... (3 Replies)
Discussion started by: nealh
3 Replies

5. Shell Programming and Scripting

sed and cut behaving differently

I have attached a file with few records. First 2 characters of each record are binary characters. I can remove it by and it works fine. But is behaving differently and removing more than expected characters. Can someone help me in accomplishing it through sed? Thanks in advance. (13 Replies)
Discussion started by: amicon007
13 Replies

6. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

7. UNIX for Dummies Questions & Answers

"$variable" is not behaving as a string in my script

Hi All, I am using the below while syntax for reading my file but its not working. Below is the line in my file " 123 rteyu 566" when I use below code the spaces are truncated for 1st variable while read line do x=`echo "$line"|cut -c 1-8` y=`echo "$line"|cut -c 9-15` echo "$x" echo "$y"... (3 Replies)
Discussion started by: yabhi_22
3 Replies

8. UNIX for Advanced & Expert Users

Script behaving differently on two servers

All, I have a script that runs on 2 servers and there seems to be something wrong. It's producing different results on the 2 servers. Here is the script on server1 which is behaving correctly but on 2 behaving differently. 2nd server: I couldn't make out whats the error is?... (5 Replies)
Discussion started by: mhssatya
5 Replies

9. Shell Programming and Scripting

Script behaving differently in Crontab..

Hi, I wrote a script to stop a process,truncate its log files and re-start the process... We are using Progress Software in Unix ( Sun Sparc) When ever I start this progress program , it should kick off a C pgm in the background.. The script work perfectly fine when I run it from command... (4 Replies)
Discussion started by: newtoxinu
4 Replies

10. UNIX for Advanced & Expert Users

Script behaving differently in Crontab..

I posted this in Shell scripting... maybe I'll try it in this forum.. ***************** I wrote a script to stop a process,truncate its log files and re-start the process... We are using Progress Software in Unix ( Sun Sparc) When ever I start this progress program , it should kick off a... (1 Reply)
Discussion started by: newtoxinu
1 Replies
Login or Register to Ask a Question