The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
foreach loop + 2 variables JamesGoh Shell Programming and Scripting 3 06-04-2008 03:36 AM
While loop with Multiple variables amit1_x Shell Programming and Scripting 4 04-02-2008 06:35 AM
adding variables for, for loop jazz High Level Programming 3 03-13-2006 04:23 AM
Writing to variables within a loop Sniper Pixie UNIX for Dummies Questions & Answers 0 03-02-2006 09:11 AM
for loop with multiple variables ? fosterian Shell Programming and Scripting 3 03-01-2004 05:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-09-2007
Tornado's Avatar
Tornado Tornado is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2006
Location: Melbourne
Posts: 249
using variables outside a while loop

Hi Guys,

I have a scripts that uses a while loop to read a file and set 2 variables.
How can I do this so the variables can be used outside the while loop ?

Below is an example....
Code:
# ./junk2 -m -e user
EXE=user master=TRUE
    DB_TAG=PRODUCT
 In loop MST=MST=testsvr1:3110
 In loop ARGS= db=CANDE pw=CANDE
ARGS=
MST=
#
This is the inputfile - junk.dat
Code:
#
# DB_TAG DB_NAM DB_PASS DB_SIR DB_PORT DB_DIR DB_MAST DB_JOURNAL DB_NOTE
#
# $Id: sirtab,v 0.1 2005/11/30 06:41:13 18 Exp 18 $
#
# Changes:
# 22/12/06  Changed OLD_PRODUCT etc to PRODUCT and sir2002.
#
DEVMENT         CANDE   CANDE
#
REBUILD         CANDE   CANDE
#
PRODUCT         CANDE   CANDE
KEYPUNCH        CANDE   CANDE
ARCHIVE         CANDE   CANDE
AQUARIUM        CANDE   CANDE
#
This is the script
Code:
#!/bin/sh
#set -x
# the question is - why are the variables ARGS and MST not available outside the second loop
#

MORE="TRUE"
while [ ${MORE} = "TRUE" ]
do
  case "${1}" in
    -ex | -EX | -e | -E)
      shift
      if [ "${1}X" != "X" ]
      then
    EXE="${1}"
    shift
      else
    echo No Execution Member Specified
      fi
      ;;
    -mst | -MST | -m | -M)
      shift
      MASTER="TRUE"
      ;;
    *)
      MORE="FALSE"
      ;;
  esac
done

echo "EXE=${EXE} master=${MASTER}"

SIRTAB="junk.dat"

AGAIN="TRUE"
while [ ${AGAIN} = "TRUE" ]
do
  if read DB_TAG DB_NAM DB_PAS
  then
    if [ "${DB_TAG}X" = "PRODUCTX" ]
    then

         echo "    DB_TAG=${DB_TAG}"

        MST="MST=`uname -n`:3110"
        echo " In loop MST=${MST}"

        unset ARGS

        if [ "${DB_NAM}" != "-" ]
        then
      ARGS="${ARGS} db=${DB_NAM}"
        fi

        if [ "${DB_PAS}" != "-" ]
        then
      ARGS="${ARGS} pw=${DB_PAS}"
      echo " In loop ARGS=${ARGS}"
        fi
     fi
  else
    AGAIN="FALSE"
  fi
done < ${SIRTAB}

echo "ARGS=${ARGS}"
echo "MST=${MST}"

exit 0
  #2 (permalink)  
Old 02-09-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
See what is wrong here.

Whoa... We almost went a week without someone asking this question!
  #3 (permalink)  
Old 02-09-2007
Tornado's Avatar
Tornado Tornado is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2006
Location: Melbourne
Posts: 249
Thanks for that..
Changing from /bin/sh to /bin/ksh worked fine.
Sponsored Links
Closed Thread

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 04:16 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