LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-21-2012, 11:34 PM   #1
linux098
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Rep: Reputation: Disabled
Sourcing Environment File in Shell Script


Hi Team,

I'm trying to source a file into my shell script. My Environment file has all the required variable name value parameters. But when I run my script I'm getting the ERROR: Cannot read env file. Please find below my script

File to be sourced: /tmp/myenv.file
#######################################

#!/bin/sh
#set -x
ENVFILE=$("/tmp/myenv.file")
[ ! -r "$ENVFILE' ] && echo "ERROR: Cannot read env file $ENVFILE" && exit8
. $ENVFILE
echo $ENVFILE

###################################

Please suggest me on how to source a property file or env file in the shell script.
Any advice is greatly appreciated.

Thanks
 
Old 10-21-2012, 11:40 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,358

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Code:
ENVFILE=$("/tmp/myenv.file")
That tells the script to execute the file, and store the output in the ENVFILE variable. If you just want ENVFILE to contain the name of the file, leave off the $()

Also, all-caps is generally reserved for environment variables. Local variables inside a script should be either lower case or mixed upper/lower case to prevent confusion. You also need a space in your "exit8", and you mixed single and double quotes in your -r test.

Last edited by suicidaleggroll; 10-22-2012 at 12:01 AM.
 
Old 10-21-2012, 11:59 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
First, the way you've defined the ENVFILE variable is wrong. And where are you sourcing /tmp/myenv.file in your script? Following code is working:
Code:
#!/bin/sh
#set -x
ENVFILE="/tmp/myenv.file"
[ ! -f $ENVFILE ] && echo "ERROR: Cannot read env file $ENVFILE" && exit 8
. $ENVFILE                          ## No use of this line, remove it.
echo "`cat $ENVFILE`\r"             ## Either use this to display output
#OR
cat $ENVFILE;                       ## Or use this line to display output 
source /tmp/myenv.file              ## Sourcing the /tmp/myenv.file file
The file /tmp/myenv.file should exist for successful execution. So make a try on this.

Last edited by shivaa; 10-22-2012 at 12:03 AM.
 
Old 10-22-2012, 12:03 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,358

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
Quote:
Originally Posted by meninvenus View Post
First, the way you've defined the ENVFILE variable is wrong. Following code is working:
Code:
#!/bin/sh
#set -x
ENVFILE="/tmp/myenv.file"
[ ! -f $ENVFILE ] && echo "ERROR: Cannot read env file $ENVFILE" && exit 8
. $ENVFILE                          ## No use of this line, remove it.
echo "`cat $ENVFILE`\r"             ## Either use this to display output
#OR
cat $ENVFILE;                       ## Or use this line to display output
So make a try on this.
What's with the semicolon on the cat, or the "echo cat"? "\r" won't work in echo either without the -e flag set.
 
Old 10-22-2012, 01:23 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by suicidaleggroll View Post
What's with the semicolon on the cat, or the "echo cat"? "\r" won't work in echo either without the -e flag set.
Yes, \r won't work without -e if we don't use " " with echo.
 
Old 10-22-2012, 02:10 AM   #6
linux098
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi suicidaleggroll/ meninvenus

Appreciate your response.

suicidaleggroll,

Made the changes you pointed out.

Here is what i'm trying to achieve


myenv.file file has the below content


admin_user=adminusr
admin_uid=2456
java_home=/opt/ibm/java



So in my script if I need to use the variables from myenv.file how can I source them without having to include the entire name value pair

Included the below lines in my script but it is not showing the right values from myenv.file

echo $admin_user
echo $admin_uid
echo $java_home

#################################

meninvenus,

I thought the below line would source the content of myenv.file

. $ENVFILE ## No use of this line, remove it.### IS this wrong?
---------------------------

myenv.file //content

admin_user=adminusr
admin_uid=2456
java_home=/opt/ibm/java

-----------------------------
Executed your script and it is working fine but If I included the below echo lines


#!/bin/sh
#set -x
ENVFILE="/tmp/myenv.file"
[ ! -f $ENVFILE ] && echo "ERROR: Cannot read env file $ENVFILE" && exit 8
echo "`cat $ENVFILE`\r"

echo $admin_user ///it is picking the corresponding value from myen.file - adminusr
echo $admin_uid ///it is picking the corresponding value from myen.file - 2456
echo $java_home //////it is picking the corresponding value from myen.file - /opt/ibm/java


####I'm getting an empty response for the above three echo commands

Your input is greatly appreciated
 
Old 10-22-2012, 03:38 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Multiple questions are arising here, in your question.
First of all, following code will work:
Code:
#!/bin/bash
#set -x
ENVFILE="/tmp/myenv.file"
[ ! -f $ENVFILE ] && echo "ERROR: Cannot read env file $ENVFILE" && exit 8
##. $ENVFILE                   ## No use of this line, remove it.
##echo "`cat $ENVFILE`\r"      ## Either use this to display output
#OR
##cat $ENVFILE;                ## Or use this line to display output 
source $ENVFILE                ## Sourcing the /tmp/myenv.file file
1) Change the script execution path to #!/bin/bash from #!/bin/sh, because source will not be available when you will execute it with /bin/sh.
2) echo "`cat $ENVFILE`\r" and cat $ENVFILE; are in fact of NO USE here. These lines will do nothing here, but will display the content of /tmp/myenv.file file, which I strongly feel that of no use in this script. Your script should be as simple as like this:
Code:
#!/bin/bash
#set -x
ENVFILE="/tmp/myenv.file"
[ ! -f $ENVFILE ] && echo "ERROR: Cannot read env file $ENVFILE" && exit 8 
source $ENVFILE
3) After invoking the script you can check value of any of your variables defined in /tmp/myenv.file file, using:
# echo $<variable>
E.g. echo $admin_user
adminusr

4) I don't understand one thing, that why are you doing this using a shell script?
If you will do this is such a way, then everytime you'll need to execute your script after login into the system to source your own defined variables.
It would be better if you simply add these variables at the end of your .bashrc or .login or any shell initialization file according to the shell you're using. Then you'll not need to execute the script all the time, because these initialization files get automatically sourced when you login. So your variables will also come into existance.
To add them into .bashrc, just at the end, add following lines:
Code:
#### This is end of my .bashrc file, I am adding my defined variables here ###
admin_user=adminusr
admin_uid=2456
java_home=/opt/ibm/java
export admin_user admin_uid java_home
And that's it! It will simply solve your purpose!

Last edited by shivaa; 10-22-2012 at 03:45 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to access environment variables in shell script jayasekar Programming 4 07-30-2011 12:30 AM
Shell Script - entering the chroot environment solo9300 Programming 1 07-19-2010 10:32 AM
Passing Environment variables to a shell-script kregec05 Programming 6 08-18-2009 09:27 AM
Need help setting environment variables via shell script srosburg Linux - Newbie 2 12-08-2005 08:58 PM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 09:50 PM


All times are GMT -5. The time now is 01:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration