nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Variables scope

This is a discussion on Variables scope within the Shell scripting forums, part of the Development/Scripting category; Hi! I have a problem with variables scope. Firstly, a little code... There's a loop in the loop: Code: echo ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 03-16-2007, 01:52 PM
Junior Member
User
 
Join Date: Oct 2006
Posts: 5
Rep Power: 0
fahur
Default Variables scope

Hi!

I have a problem with variables scope.

Firstly, a little code...

There's a loop in the loop:

Code:
echo `gsh list_eq all > $TEMP`

while read line
do
  SHELF=`echo $line | awk '{ print $1 }'`
  SLOT=`echo $line | awk '{ print $2 }'`
  echo `gsh get_eq_info -eq { $SHELF $SLOT } > $TEMP1`
  egrep "ProdName|ProdNo|Revision|SerialNo" $TEMP1 > $TEMP2
  while read line
  do
    PAR=`echo $line | awk '{ print $1 }'`
    VAL=`echo $line | awk '{ print $3 $4 $5 }'`
    if [ "$PAR" = "ProdName" ]; then
      PRODNAME=$VAL
      echo $PRODNAME
    elif [ "$PAR" = "ProdNo" ]; then
      PRODNO=$VAL
      echo $PRODNO
    elif [ "$PAR" = "Revision" ]; then
      REV=$VAL
      echo $REV
    else
      SERIALNO=$VAL
      echo $SERIALNO
    fi
  done < $TEMP2
  echo "$SHELF $SLOT;$PRODNAME;$PRODNO;$REV;$SERIALNO;"
done < $TEMP
Everything's OK, but when I try to display all parameters in the end (finally it will by written to the CSV file):

Code:
echo "$SHELF $SLOT;$PRODNAME;$PRODNO;$REV;$SERIALNO;"
I only see sth like this:

Code:
1 1;;;;;
So it's simple - all the parameters in the internal loop are not "viewable" (empty) outside. I echoed them inside and they are not empty.

The problem is I have to place all the params in one line, because (as mentioned above), I need them in CSV file. When I write them directly to the file, every param is placed in next line and this is not an option.

Does anyone has an idea?!

Thanks is advanced...

--
Rgds
fahur
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-16-2007, 04:25 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

initialize variables before starting while loop
Code:
SHELF=""
SLOT=""
PRODNAME=""
PRODNO=""
REV=""
SERIALNO=""
# start while loop
,,,,
,.....
..
If problem continues debug line
Code:
VAL=`echo $line | awk '{ print $3 $4 $5 }'`
Run script as
Code:
bash -x  ./script-name
Reply With Quote
  #3 (permalink)  
Old 03-16-2007, 06:38 PM
Junior Member
User
 
Join Date: Oct 2006
Posts: 5
Rep Power: 0
fahur
Default

Quote:
Originally Posted by tom
initialize variables before starting while loop
Code:
SHELF=""
SLOT=""
PRODNAME=""
PRODNO=""
REV=""
SERIALNO=""
# start while loop
,,,,
,.....
..
Unfortunatelly, doesn't work for me

Quote:
If problem continues debug line
Code:
VAL=`echo $line | awk '{ print $3 $4 $5 }'`
If I was to check if it shows the value - yes it is...

--
Rgds
fahur
Reply With Quote
  #4 (permalink)  
Old 04-13-2007, 12:56 PM
Junior Member
 
Join Date: Mar 2007
Posts: 3
Rep Power: 0
allotment is on a distinguished road
Default

Code:
echo "$SHELF $SLOT;$PRODNAME;$PRODNO;$REV;$SERIALNO;"
Just a hunch, try to remove the ;
or use harder quote like ' instead of "
otherwise debug the string comparison in the if
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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

Similar Threads

Thread Thread Starter Forum Replies Last Post
Fold Command with variables Wannabee Shell scripting 1 09-27-2007 04:28 AM
Linux set environment variables in my shell chiku Shell scripting 1 12-30-2006 05:05 AM


All times are GMT +5.5. The time now is 08:55 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36