Unable to read Environment Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to read Environment Variable
# 1  
Old 09-06-2012
Unable to read Environment Variable

Hi I have created the following shell script file with the following content.

Code:
#!/bin/csh
set VAR1="abcxyz" << EOF
EOF
echo "---------------------"
echo "VAR1 = $VAR1"
echo "---------------------"

i am not able to echo the previously set VAR1.

Can any one suggested what could be wrong?

Thanks,
Srinivas

Last edited by zaxxon; 09-07-2012 at 10:27 AM.. Reason: code tags, not quote tags
# 2  
Old 09-06-2012
Leave the << EOF and EOF out.
# 3  
Old 09-06-2012
In addition to @zaxxon , please post what you typed to run the script and what exact output you saw. Even with the spurious <<EOF and EOF, the script still works on my system.

Please also post what Operating System and version you are running.

Note: It is quite unusual for anybody to use C-Shell nowadays.
# 4  
Old 09-07-2012
Hi All, Thanks for quick reply.

I resolved the issue some how as mentioning below.

I just knocked off <<EOF and EOF statements and ran the following

Code:
#!/bin/csh
set VAR1="abcxyz"
echo "---------------------"
echo "VAR1 = $VAR1"
echo "---------------------"


First i ran with the following shell command without adding +x bit on sh file.

Code:
> sh test1.sh

the out put i got as

---------------------
Code:
VAR1 =

---------------------

Then i tried adding +x bit on the sh file and ran the following

Code:
> ./test1.sh

output came as expected.

---------------------
Code:
VAR1 = abcxyz

---------------------

I don't clearly got the point here. what is the difference between the previous ran command
and the later one?

Thanks,
Srinivas


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 09-07-2012 at 10:26 AM.. Reason: code tags
# 5  
Old 09-07-2012
so far you have not been setting an environmental variable, only a C-shell variable. You need to use "setenv" to set an environmental variable in the C-shell.
# 6  
Old 09-07-2012
When you invoked the script with sh test1.sh the Shebang line is ignored and the script was run with Bourne Shell. The syntax was incorrect for Bourne Shell. It would of course have worked with csh test1.sh.

When you made the script executable and ran it as ./test1.sh the Shebang line was executed and the script ran with C Shell.

Unless you have a very good reason for using C Shell, you will find learning Bourne Shell much more useful in the modern unix and Linux world. All system scripts are Bourne Shell or some enhance form of Bourne Shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

2. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

3. Solaris

Environment Variable

Hi All, I am new to SUN Solaris.I have some questions regarding environment variable.:confused: Q1. Where the environment variables available in Solaris. Q2. What command used here. Q3. Can a user change this .Suppose i want to change the bash to ksh can it be possible here. Or i need to... (2 Replies)
Discussion started by: jdash.ps
2 Replies

4. Shell Programming and Scripting

Environment Variables in text file and read command

I cannot get the following substitution ($ORACLE_SID) to work: The variable ORACLE_SID is set to wardin my environment. It has been exported. I have a text file called test.dat: /u07/oradata/${ORACLE_SID}/extab/finmart/summit/ps_voucher_line_crnt_ex.dbf... (2 Replies)
Discussion started by: bradyd
2 Replies

5. UNIX for Dummies Questions & Answers

use AWK to read an Environment variable

Dear Unix gurus, Perhaps I'm missing something,.....I'm having the most infernal difficulty getting AWK to read in an environment variable. For example, value=5 awk '{print ""$value""}' should return the number "5". This is not the case. Can somebody help?:confused: cheers (7 Replies)
Discussion started by: tintin72
7 Replies

6. Shell Programming and Scripting

Interpreting Logicals/Environment Variables using the read command

Hi All I have something that from the outset seems really trivial but in practice is not quite working. I have the following code sample in my shell script which illustrates the problem echo "enter home directory" read home mkdir $home/newdir The user then enters a logical $HOME... (3 Replies)
Discussion started by: kingpin2502
3 Replies

7. UNIX for Advanced & Expert Users

How to read pdf file in UNIX environment?

Hi, I had a PDF file in Windows, I ftp'ed it to UNIX environment. Now, I couldnot read the same file in the UNIX environment. Is there any possible way to read this file? I need to give this file as input Thanks, Geetha (4 Replies)
Discussion started by: iamgeethuj
4 Replies

8. Shell Programming and Scripting

about environment variable

i am not clear at some places i saw that assigning a env var with colon ':' like export PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:.:/usr/platform/sun4u/sbin here you can find : and a . and again : could some one explain how it works... see all is starting from root directory, how : is used and... (6 Replies)
Discussion started by: Arunprasad
6 Replies

9. Shell Programming and Scripting

Environment Variable

First of all I am using C shell. I have a variable destDirectory that holds a path. the path includes an environment variable($user) when I try to execute a command within the script, the $destDirectory gets replaced with the path, but the environment variable is not replaced. I end up... (2 Replies)
Discussion started by: karyn1617
2 Replies

10. UNIX for Advanced & Expert Users

help on environment variable

what is the environment variable used for getting the <machine name> . hello $LOGNAME,welcome to <??> I want to print the machine name in the place of <??> Please help. (6 Replies)
Discussion started by: mehuldoshi
6 Replies
Login or Register to Ask a Question