Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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." >> $MAILmail 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?

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
donefunction errors ()
{
if [ -e $1 ] #if $1 exists and is > 0.
...
..
}mfg Frank

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
errorsIf 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/tmpfile2And 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.

![]() |
![]() |
![]() |

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