Problem while concatenating variables in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem while concatenating variables in shell script
# 8  
Old 02-28-2011
Thanks Folks.
You are correct. I copied the script from windows and edited in text editor.
I used Emacs and removed all ^M. Now, it is working fine.

Actually, I am trying to set all environment variables in one script (env.sh)
and re-use the variables in appln.sh (calling script).

Does Unix really supports this? If so, how can I achieve this?

appln.sh
--------
#! /bin /sh
sh ./env.sh

# This prints nothing
echo ${MY_VAR}


env.sh
---------
export MY_VAR="/opt/bea"
echo ${MY_VAR}

I'd like to re-use 'MY_VAR' in appln.sh. I don't want to add MY_VAR in /etc/profile.

Any help on this is highly appreciable

Thanks,
Adhil

Last edited by Adhil; 02-28-2011 at 04:59 PM..
# 9  
Old 02-28-2011
Quote:
Originally Posted by Adhil
Actually, I am trying to set all environment variables in one script (test2.sh)
and re-use the variables in test1.sh (calling script).

Does Unix really supports this? If so, how can I achieve this?
UNIX really supports this but you're doing it wrong. You're also putting extra spaces and punctuation marks all over the place -- if your script was as you posted it it'd be complaining about syntax errors and files not found.

Code:
#!/bin/sh
# Don't put extra spaces in the #!/bin/sh

# This does nothing, because test2.sh will run in its own separate process
./test2.sh
echo "${MY_VAR}"

# My best guess at what you actually did, which also does nothing for the same reason.
sh ./test2.sh
echo "${MY_VAR}"

# This will run it in the same shell.  Note the dot and the space.
. test2.sh
echo "${MY_VAR}"

Code:
# Some shells demand that the = and the export happen separately.
MY_VAR="/opt/bea"
export MY_VAR

# 10  
Old 02-28-2011
Folks,

I got it fixed by changing appln.sh

from
sh ./env.sh
to
. env.sh

What is the difference between these two?

Thanks,
-Adhil

---------- Post updated at 04:05 PM ---------- Previous update was at 04:00 PM ----------

Thanks a lot, Corona.

Sorry I edited my previous post before reading your reply. Could create confusion to the readers. My apologize.

Thanks,
Adhil
# 11  
Old 02-28-2011
I forgive you. Smilie I manage to do that to people with fair regularity
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenating two mutiline variables in a bash

Hi All, I am having a situation where am capturing results in two variables from an xml file. However, I am looking to print those two variables with pipe in between them and these variable are multi-line. This is how my 1st variable looks like: 20181225010 20190224010 20190224010... (8 Replies)
Discussion started by: svks1985
8 Replies

2. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. Shell Programming and Scripting

Problem in Global variables in shell script

hi, i have a shell script which calls another shell which in turn calls another shell script. Main_shell_script.sh echo "In Main_shell_script.sh" FILENAME="original.txt" # LINE 1 DST_FILENAME=$FILENAME # LINE 2 echo "FILENAME = {$FILENAME}" echo "DST_FILENAME =... (3 Replies)
Discussion started by: Little
3 Replies

5. Shell Programming and Scripting

Concatenating fixed length lines in shell script

I have a peculiar file with record format like given below. Each line is wrapped to next lines after certain number of characters. I want to concatenate all wrapped lines into 1. Input:(wrapped after 10 columns) This is li ne1 This is li ne2 and this line is too lo ng Shortline ... (8 Replies)
Discussion started by: kmanyam
8 Replies

6. Shell Programming and Scripting

bash -- concatenating values from variables

Hi This is a simple one but I got a lost in translation when doing. What I want to do, given both variables in the example below, to get one value at the time from both variables, for example: 1:a 2:b etc... I need to get this in bash scripting code: varas="1 2 3 4" varbs="a b c d"... (4 Replies)
Discussion started by: ranmanh
4 Replies

7. Red Hat

Concatenating variables

Hi all, I'm trying to do a very simple script, as you can see as follow: #!/bin/bash #Valorizzazione Token presenti nel file di properties var_path_weblogic="`cat weblogic.properties | grep "dir_wl" | /usr/xpg4/bin/awk '{print $3}'`" var_ip_address="`cat... (5 Replies)
Discussion started by: idro
5 Replies

8. Shell Programming and Scripting

problem accessing Multiple Variables from C Program to a Shell script

program name--test #!/bin/bash output1=`/home/user/a.c` output2=`/home/user/a.c` k=`$output1 + 1` m=`$output2 + 1` echo $k echo $m --------------------------------------------------------------------------- prgram name--a.c #include<stdio.h> int main() (1 Reply)
Discussion started by: sameworld1980
1 Replies

9. Shell Programming and Scripting

Concatenating Different # of Variables

Hi, I'm quite new at unix and was wondering if anyone could help me with this. I have 2 arrays: eg. STAT=online, STAT=offline, STAT=online WWN=xxxx1, WWN=xxxx2, WWN=xxxx3 I got these information from a script using fcinfo hba-port that runs through a loop. Now, I want to store... (2 Replies)
Discussion started by: jake_won
2 Replies

10. Shell Programming and Scripting

Concatenating Variables

FILE_DATE=$(date +"%Y%m%d_%H%M")_ FILE_PREFIX=${FILE_DATE} echo $FILE_PREFIX JS_LOG_DIR="E:\DecisionStream Jobs\Invoice Balance Fact Jobs" echo $JS_LOG_DIR --This is where the problem surfaces. The last line of this script does a rsh to an NT machine and one of the parameters is the... (2 Replies)
Discussion started by: photh
2 Replies
Login or Register to Ask a Question