Sourcing Env file with eval works with ksh but not BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sourcing Env file with eval works with ksh but not BASH
# 1  
Old 09-02-2015
Sourcing Env file with eval works with ksh but not BASH

Hi,

I am running this on Redhat 5.10
I have a simple test script called test.sh which has the following
contents and it uses the BASH shebang.
-------------------------------------------------------------
Code:
#!/bin/bash
eval `/tmp/filereader.pl /tmp/envfile.txt`
echo "TESTPATH=$TESTPATH"

Inside /tmp/envfile.txt there are the following contents
------------------------------------------------------
Code:
export BASEPATH=/masterdir
export TESTPATH=${BASEPATH}/subdir

When I run test.sh I get the following INCORRECT output
-------------------------------------------
TESTPATH=/subdir

But if I change the shebang line to point to Korn Shell as
#!/bin/ksh and rerun test.sh I get the following CORRECT output
--------------------------------------------------
TESTPATH=/masterdir/subdir

So when I run with Korn shell I get the correct output but when I run with BASH it is not reading the contents of $BASEPATH into the TESTPATH variable. Can you please explain why .

Much appreciated.

Thanks
waavman

---------- Post updated at 02:11 PM ---------- Previous update was at 01:55 PM ----------

Missed out on mentioning what /tmp/filereader.pl does in my previous question.
It is just a simple perl script that opens the file supplied to it as argument and prints its contents line by line.
# 2  
Old 09-02-2015
Of course, the obvious fix would be to change:
Code:
eval `/tmp/filereader.pl /tmp/envfile.txt`

to:
Code:
. /tmp/envfile.txt

But, assuming you are trying to do something with your perl script other than emulate cat, what happens if you change it to:
Code:
eval $(cat /tmp/envfile.txt)

so you can determine if the problem is an interaction between your perl script and bash eval with command substitution or if there is just a problem with bash eval with command substitution.

If the cat works, we'll need to see the contents of /tmp/filereader.pl...

Just to be sure that something in the environment isn't affecting the difference in behavior between bash and ksh, please also insert:
Code:
TESTPATH=before

as a new line before the eval command in your script.
# 3  
Old 09-02-2015
Hi Don,

I am using perl script to read the envfile because eventually I would like this shell script to be used by other users who donot even have read access to /tmp/envfile.txt (using setuid bit that Perl supports)
For now however the userid that this is being run with does have read access to /tmp/envfile.txt. Or else it would not have worked even with KSH.

I tried using eval with cat instead of the perl script as you suggested.
But I notice the same behaviour as shown below.

Code:
#!/bin/bash
#eval `/tmp/filereader.pl /tmp/envfile.txt` 
eval $(cat /tmp/envfile.txt)
echo "TESTPATH=$TESTPATH"


And I get the following INCORRECT output where $BASEPATH is not substituted in the value of $TESTPATH
--------------------------------------------------------
TESTPATH=/subdir

But when I change shebang to #!/bin/ksh as below

Code:
#!/bin/ksh
#eval `/tmp/filereader.pl /tmp/envfile.txt` 
eval $(cat /tmp/envfile.txt)
echo "TESTPATH=$TESTPATH"


I get the correct output
---------------------------
TESTPATH=/masterdir/subdir

I tried adding TESTPATH=before before the eval `perlfile...` and also before eval $(cat...). It doesnot use that value of TESTPATH since it overwrites it with the value of TESTPATH from the /tmp/envfile.txt in both KSH and BASH.

As for my perl script /tmp/filereader.pl its a simple script that just reads form the input file argument and prints one line at a time

Code:
#!/usr/bin/suidperl
$envfilename=$ARGV[0];
open(FILEHANDLE,"<$envfilename") || die "Cannot Open file $envfilename";
while (<FILEHANDLE>)
{
        $lineread=$_;
        chop($lineread);
        printf("%s\n",$lineread);
}

However as you can see it does NOT seem to be an issue with the Perl script since the same variation of behaviour is exhibited between bash and ksh even when using eval $(cat /tmp/envfile.txt)


thanks
waavman
# 4  
Old 09-02-2015
Quote:
Originally Posted by waavman
I am using perl script to read the envfile because eventually I would like this shell script to be used by other users who donot even have read access to /tmp/envfile.txt (using setuid bit that Perl supports)
Perl does not "support setuid". It is not perl's job to elevate permissions. Either the operating system gives scripts elevated permissions on exec or it doesn't, and on most systems --

Code:
$ cat ./setuid.pl
#!/usr/bin/perl

print $<, "\n"; # Print UID
print $>, "\n"; # Print EUID

$ ls -l ./setuid.pl

-rwsr-xr-x 1 root root 49 Sep  2 15:53 ./setuid.pl

$ ./setuid.pl
1004
1004

$

-- it doesn't. Same as any other script.

If you are looking for security, cramming unescaped anonymous scripts into eval is hardly the way to go anyway. That's kludgy, and you've only shifted the problem one further down -- now you have the same problem of stopping people from running filereader.pl...

So my suggestion is to rip out eval and just source the script properly. Put the script and the profile under different ownership to protect them, and only let sudo run it.

Last edited by Corona688; 09-02-2015 at 07:21 PM..
# 5  
Old 09-02-2015
Try
Code:
eval "$(cat /tmp/envfile.txt)"
echo "TESTPATH=$TESTPATH"
TESTPATH=/masterdir/subdir

as opposed to
Code:
eval $(cat /tmp/envfile.txt)
echo "TESTPATH=$TESTPATH"
TESTPATH=/subdir

# 6  
Old 09-02-2015
In addition to RudiC's suggestion, here are a couple more interesting tidbits...
If you change the contents of /tmp/envfile.txt from:
Code:
export BASEPATH=/masterdir
export TESTPATH=${BASEPATH}/subdir

to:
Code:
export BASEPATH=/masterdir;
export TESTPATH=${BASEPATH}/subdir

your code behaves as expected with both bash and ksh with or without the double quotes around the command substitution.

And, if you change the contents of /tmp/envfile.txt to:
Code:
export BASEPATH=/masterdir TESTPATH=${BASEPATH}/subdir

bash fails with or without quotes around the command substitution and ksh succeeds in either case.

In the case of:
Code:
export BASEPATH=/masterdir TESTPATH=${BASEPATH}/subdir

the standards say that operands should be processed in the given order (unless the DESCRIPTION or OPERANDS sections state otherwise), so it appears that bash (with or without the --posix option) does not conform to the standards in this case.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-03-2015
Hi Rudi, Don,

Thanks for your great Discoveries. Solution provided by Rudi is very easy to implement and it works even with the Perl file reader code I have. Solution provided by Don is also very helpful. However in my case since envfile.txt is being shared across multiple users, I would prefer to make local changes to my script by adding "" in the eval command as in
Code:
eval "`/tmp/filereader.pl /tmp/envfile.txt`"

Also Don I was curious how come in bash the ';' at the end of each line in /tmp/envfile.txt makes it work without "" in eval ? Each entry in my envfile.txt are anyway newline-separated or on a separate line. Isn't that good for bash to understand that each export command in the envfile.txt is a separate export command that i needs to process separately as it is separated by newline. What is the significance of ; as opposed to newline in bash ?

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sourcing file from parent directory bash

"Debian 9 64x - LXDE" I try to source a file from my parent directory: #!/bin/bash #source.bash . ../links.bash but i get "file not found". I tried . "../links.bash" and . '../links.bash'. I got on all methods the same result. If i use the absolute path it works, but i don't want to... (4 Replies)
Discussion started by: int3g3r
4 Replies

2. UNIX for Advanced & Expert Users

Dot sourcing differences in ksh, AIX vs Linux vs Solaris

Why does dot sourcing of ksh functions behave so differently between AIX, Solaris, and Linux? How can I make Linux behave the way I want in the test I show below? I have a library of interdependent functions I have developed and use in ksh in AIX. They also run in Solaris. Now I am migrating... (9 Replies)
Discussion started by: charles_n_may
9 Replies

3. Shell Programming and Scripting

Sourcing .cshrc (C shell) environment variables to bash

I have tried with the following: csh -c 'source ~/.cshrc; exec bash' # works perfectly (cat ~/.cshrc; echo exec bash) | csh # not working And, using sed, I successfully retrieved the environment variables from ~/.cshrc sed -rn 's/setenv\s+(\S+)\s+(.*)$/export \1=\2/p' ~/.cshrc but now... (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. Shell Programming and Scripting

Setting up env variable in ksh

I am facing a very strange issue. I have script in ksh with #!/bin/ksh as shebang. This script has function which sets the env variable before running other functions of the script. by set_up_env() { CONFIG_FILE="/opt/app/tools/deepmarking/latestVersion/script/UploadEnv" if then ... (7 Replies)
Discussion started by: Tuxidow
7 Replies

5. UNIX for Dummies Questions & Answers

[solved] Where & what bash env file, Mac OS?

Hi! I wanted to simplify my bash prompt, so I edited my etc/bashrc file. I thought this was the file that would override any other env files. When I opened it, I saw that the way it was setup was not what my prompt looked like, although I forget exactly what was there. But i edited it the way I... (1 Reply)
Discussion started by: sudon't
1 Replies

6. Shell Programming and Scripting

Eval Tricky Manipulation of Arry in KSH - Help

Hi, Could any one share the intelligence to track this problem. I have any array BT_META_36 and it prints properly with contents of array. # print "BT_META_36=${BT_META_36}" # BT_META_36=cab3,cab4:HDS:052,07A cab3,cab4:HDS:052,07A Now I have a BT_META_36 assigned to a variable.... (0 Replies)
Discussion started by: ajilesh
0 Replies

7. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

8. UNIX for Advanced & Expert Users

Ksh - Env. Variables ??

Hey all, I have been using Ksh and in that I am setting Environment variables. To set Env. Variables I have created my own file "BuildScript.sh" in which i have written : export CLASSPATH=/somedir/some other dir/file:. export PATH=/some dir/file:. But when i am calling this... (4 Replies)
Discussion started by: varungupta
4 Replies

9. Shell Programming and Scripting

eval in bash

hi everyone i've been reading learning the bash and there is somrthing i don;t understand what does eval do i know that it run a command or script twice but i don;t see in what for cases i can use this could somebody explain this to me (3 Replies)
Discussion started by: jetfreggel
3 Replies

10. UNIX for Dummies Questions & Answers

script sourcing problem (ksh)

I have a script "abc.sh" in /tmp which has exit 0 as its last line when I run this script from /tmp/xyz/def.sh script as . ../abc.sh then the script executes but the control doesn't return to def.sh script for subsequent commands in def.sh but if I invoke the abc.sh from inside the... (3 Replies)
Discussion started by: rakeshou
3 Replies
Login or Register to Ask a Question