Setting variables in a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting variables in a function
# 1  
Old 03-29-2006
Setting variables in a function

I'm not quite sure what I'm doing wrong here.

I've go several jobs which print reports. Occassionally a printer will break down and reports need to be move to another printer. Rather than hard code the printer names in our scripts I'm trying to set these programatically
using our function library (Solaris, Sybase) as follows:

Sample job script
============================
#!/bin/ksh
#filename=test-funct.cmd

run_parameters()
{
PASSWORD=`cat $CIRC_PWF`
DSS_DSQUERY=CIRC125_WS
}
# MAIN
run_parameters

# Run our function
new_define_printers

# Let's check that some are set

echo "AREA24_PNTR=$AREA24_PNTR"
echo "AREA25_OFF_PNTR=$AREA25_OFF_PNTR"
echo "AREA44_OFF_PNTR=$AREA44_OFF_PNTR"
echo "AREA44_DC_PNTR=$AREA44_DC_PNTR"
echo "CUST_REL_NORTH_PNTR=$CUST_REL_NORTH_PNTR"

exit 0
============================


My function

#!/bin/ksh
function new_define_printers
{
#First get our datecl
datectl=`date +%Y%m%d`

# Then query the database
isql -U$CIRC_LOGIN -S$DSQUERY << !end > /tmp/status$$
$PASSWORD
use $CIRC_DB
go
select ltrim(rtrim(printer_name)) + '=' + ltrim(rtrim(unix_name))
from printer_lst
where eff_dt <= getdate()
and (exp_dt is null or exp_dt >= getdate())
go
!end

# Check if the database was available
grep -i 'rows affected' /tmp/status$$ > /dev/null
stat=$?
if [[ $stat = 0 ]] then # The database was available
# Then clean up the list --- Note: the vi commands must be in line one
vi - /tmp/status$$ <<EOF > /tmp/vi$$
:1
:d3
:$
dd
dd
:1,.s/^ //
:w! /tmp/result$$
:q
EOF
# If we don't have a file today we need to save it
if [[ ! -e $CIRC_OUT/printer_lst_ctl$datectl ]] then
cp /tmp/result$$ $CIRC_OUT/printer_lst_ctl$datectl
fi
else
# The database is not available. Get the lastest file
lastest=`ls $CIRC_OUT/printer_lst_ctl.* | sort | tail -1`
# Put it into /tmp/result$$
cp $latest /tmp/result$$
fi

# Then get the latest file and run our commands to set the list
for i in `cat /tmp/result$$`
do
cmd=$i
$cmd
done

# Last, do a bit of cleanup and we're done
# First, let's find the oldest file (we want to keep at least three)
cntfile=`ls $CIRC_OUT/printer_lst_ctl.* | sort | wc -l | sed -e 's/ //g'`
delfile=`ls $CIRC_OUT/printer_lst_ctl.* | sort | head -1`
if [[ $cnt -gt 3 ]] then
rm -f $delfile
fi
rm -f /tmp/status$$
rm -f /tmp/vi$$
} # end define_printers

============================

I've got everything working except my variable aren't getting set. Here's a sample of what the /tmp/result$$ file looks like:

AREA41_DC_PNTR=area41_hs1
AREA41_OFF_PNTR=area41_hs2
AREA41_PNTR=area41_hs2
AREA42_DC_PNTR=area42_hs2
AREA42_OFF_PNTR=area42_hs2
AREA42_PNTR=area42_hs2
AREA44_DC_PNTR=area44_hs2
AREA44_OFF_PNTR=area44_hs2
AREA44_PNTR=area44_hs2
AREA45_DC_PNTR=area45_hs1
AREA45_OFF_PNTR=area45_hs2
AREA45_PNTR=area45_hs2
AREA46_DC_PNTR=area46_hs1
AREA46_OFF_PNTR=area46_hs2
AREA46_PNTR=area46_hs1
AREA49_DC_PNTR=area49_hs1
AREA49_OFF_PNTR=area49_hs2
AREA49_PNTR=area49_hs2
============================


And here's the error I'm getting when I run this:

/apps/usr/circ/bin/test-funct.cmd[50]: AREA41_PNTR=area41_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA42_DC_PNTR=area42_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA42_OFF_PNTR=area42_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA42_PNTR=area42_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA44_DC_PNTR=area44_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA44_OFF_PNTR=area44_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA44_PNTR=area44_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA45_DC_PNTR=area45_hs1: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA45_OFF_PNTR=area45_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA45_PNTR=area45_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA46_DC_PNTR=area46_hs1: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA46_OFF_PNTR=area46_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA46_PNTR=area46_hs1: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA49_DC_PNTR=area49_hs1: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA49_OFF_PNTR=area49_hs2: not found
/apps/usr/circ/bin/test-funct.cmd[50]: AREA49_PNTR=area49_hs2: not found

============================

Where's my flaw? Smilie
# 2  
Old 03-29-2006
I fugured it out!!!

All I needed to do what change this:

for i in `cat /tmp/result$$`
do
cmd=$i
$cmd
done


To this:

for i in `cat /tmp/result$$`
do
cmd=$i
export $cmd
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Setting variables in UNIX

Hi, I am a beginner in Unix. Now I am learning setting up variables. However, I am receiving an error. Can anyone please help me with it My command as Test=unixprogramming returns the error command not found. (I am using FreeBSD Unix and in my terminal, it is ~% instead of $ . is the... (2 Replies)
Discussion started by: kgopan
2 Replies

2. Shell Programming and Scripting

Passing variables and setting them

So I'm writing a script to generate pairwise scores for how similar two strings are, and while I've been able to get it to work on a single script, I've been unable to iterate it. So suppose I have a file thus 1234567890 1234567890 1234567899 first I need to assign two lines, by their... (3 Replies)
Discussion started by: viored
3 Replies

3. Shell Programming and Scripting

setting and displaying variables

Hello, I need a little help. 1. Edit /etc/profile so that all users are greeted upon login. 2. For the root account, set the prompt to something like "Danger!! root is doing stuff in \w", preferably in a bright color such as red or pink or in reverse video mode. Thanks for help. (4 Replies)
Discussion started by: zhshqzyc
4 Replies

4. UNIX for Dummies Questions & Answers

Setting up environment variables

Hi all, This is my first post here. I need to set up a few environment variables with a shell script. Some are hard-coded, but some should come from other commands or as input from the user. How do I do that? For example, I need to export a variable as such: export DISPLAY=127.0.0.1:8.0 ... (2 Replies)
Discussion started by: exchequer598
2 Replies

5. UNIX for Dummies Questions & Answers

Setting Environment Variables

#!/bin/bash if ; then ASS1_DATA_DIR=./ echo $ASS1_DATA_DIR export ASS1_DATA_DIR echo "data dir" fi if ; then ASS1_OUTPUT_DIR=./ export ASS1_OUTPUT_DIR fi I want to create a new environment variable ASS1_DATA_DIR and ASS1_OUTPUT_DIR in bash and set them to the current... (4 Replies)
Discussion started by: bigubosu
4 Replies

6. UNIX for Dummies Questions & Answers

Need help with setting up environment variables

hi all, I would appreciate if some one could explain me the difference between setting up the variables as shown below HOME=${HOME:-"/home/user1"} HOME=/home/user1 (1 Reply)
Discussion started by: SSSB
1 Replies

7. UNIX for Dummies Questions & Answers

Setting up variables

Hi all, I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using: . ./script_name XX where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
Discussion started by: solar_ext
3 Replies

8. UNIX for Advanced & Expert Users

setting some variables

i have a file .NAMEexport MY_NAME=JOE when i do this at the command prompt #. .NAME $echo MY_NAME $JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh #echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Discussion started by: new2ss
7 Replies

9. UNIX for Dummies Questions & Answers

Setting numbered variables

Normally I would post in the shell scripting area, but this is so basic I thought I'd best put it in the dummy area! I want to set a series of numbered variables. I have a loop which increments a variable called $i with each loop. I want to name variables with this number e.g. var1, var2, var3... (1 Reply)
Discussion started by: michaeltravisuk
1 Replies

10. UNIX for Dummies Questions & Answers

setting environment variables ???

Hello, I want to set some environment variables with this script: ip=$@ echo Remote Computer: $ip PERLDB_OPTS="CallKomodo=$ip:9000 RemotePort=$ip:9010 PrintRet=0" export PERLDB_OPTS PERL5LIB=/opt/komodo export PERL5LIB echo PERLDB_OPTS: $PERLDB_OPTS echo PERL5LIB: $PERL5LIB But it... (5 Replies)
Discussion started by: Gargamel
5 Replies
Login or Register to Ask a Question