Computing.Net > Forums > Unix > export variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

export variables

Reply to Message Icon

Name: Cara
Date: November 27, 2002 at 02:51:00 Pacific
OS: AIX 4.3
CPU/Ram: 450
Comment:

I am having trouble exporting variables set in a script to a function inside the script.

1. The variable must be set outside the function because it will be called outside as well as inside the function.

2. The file from which the error is extracted will change twice, so it can't be included in the function.

There is a way around this by writing two separate functions for the two files and setting the variables independently inside each function, but I would like to know the correct way to do this, and why the export command is not exporting the variables.


Here is a sample of the script:
___________________________________________
#set and export variables:

LOG=/tmp/tmpfile
MAIL=/tmp/tmpfile2
export LOG MAIL

#function to mail errors:

function errors
{
if [ -s $LOG ] #if $LOG exists and is > 0.
then
echo "Here are the errors:" > $MAIL
cat $LOG >> $MAIL
echo "Please check the errors." >> $MAIL

mail username@domain.com < $LOG
errors

____________________________________________

I also have a question about the test statement in this example.

The -e will return true if the file exists, -r if the file exists and is readable, and -s if the file exists, is readable, and the size is greater than 0.

In the example, it is returning true for the -e and the -r even if the file does not exist in the path and I was wondering if it was returning true because of the grep statment. The only option that tested correctly was -s when there were errors in the file.

Can anyone offer any explanantions?



Sponsored Link
Ads by Google

Response Number 1
Name: frank
Date: November 27, 2002 at 06:46:23 Pacific
Reply:

Hi Cara,

I tried what you described, but can not see the problem with the -e on AIX 4.3.

Also the function is working fine including the varaible. example below:
# Begin
set -x
LOG=/tmp/frank2
MAILZ=/tmp/tmpfile2
export LOG MAILZ

#function to mail errors:

function errors
{
set -x
if [ -e $LOG ] #if $LOG exists and is > 0.
then
echo "Here are the errors:" > $MAILZ
cat $LOG >> $MAILZ
echo "Please check the errors." >> $MAILZ
fi
}
errors
# end
If you would like to use the function multible times with different varaiables you have to use parameter for this
e.g.
filename="/tmp/tmpfile1 /tmp/tmpfile2"
for i in $filename ; do
errors $i
done

function errors ()
{
if [ -e $1 ] #if $1 exists and is > 0.
...
..
}

mfg Frank


0

Response Number 2
Name: James Boothe
Date: November 27, 2002 at 08:56:06 Pacific
Reply:

The LOG variable does not need to be exported. The errors function will always see the current value of $LOG:

LOG=/tmp/tmpfile1
errors
...
LOG=/tmp/tmpfile2
errors

If you still cannot get this to work, maybe you can post entire script.

The other approach is shown by Frank's for-loop. You may not want the for-loop here, but this is showing how to pass parameters to the errors function:

errors /tmp/tmpfile1
...
errors /tmp/tmpfile2

And notice that Frank's error function now sees the passed parameter as $1.

Regarding your -e and -r issue, I don't see a grep statement. Maybe some of your code got lost when you posted? By the way, -s tests for presence of a non-empty file regardless of readability.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: export variables

Export variables from awk www.computing.net/answers/unix/export-variables-from-awk/7261.html

export variables www.computing.net/answers/unix/export-variables/4403.html

Variables in Shell SCripting www.computing.net/answers/unix/variables-in-shell-scripting/6020.html