Issue in shell script variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue in shell script variables
# 1  
Old 02-01-2016
Issue in shell script variables

Hi,

I have a file at $HOME/t.txt, below is file content
Code:
cat $HOME/t.txt
CUSTOMER_${REGION}.out

Am using this file content in one script $HOME/samp.sh, below is the script

Code:
#!/bin/bash
REGION=USA
x=`cat ${HOME}/t.txt`

echo $x

Am getting following output.. CUSTOMER_${REGION}.out

Expected output is CUSTOMER_USA.out, can you please help me to get this.

Thanks

Last edited by Don Cragun; 02-01-2016 at 12:59 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-01-2016
Indirection can't be done like that in shell. What do you need that construct for?

Use arrays, or, in bash, indirected variables (like ${!REGION}) might help. The use of eval for the assignment can be dangerous and is therefore deprecated.
# 3  
Old 02-01-2016
Hi,
Thanks for the reply.

REGION value vary from run to run, so we would like to pass it as mentioned.

Looks like arrays would not help here, REGION will get run time same needs to replace in file name.

Thanks
# 4  
Old 02-01-2016
OSX 10.7.5, default bash terminal...

Just an idea, and not sure if this might work for you, first $HOME/t.txt ...
Note the variable "x" is now in the "t.txt" file...
Code:
cat $HOME/t.txt
x=CUSTOMER_${REGION}.out

And the script:-
Code:
#!/bin/bash
REGION=USA
source "$HOME/t.txt"
echo "$x"

Results:-
Code:
Last login: Mon Feb  1 20:14:29 on ttys000
AMIGA:barrywalker~> ./sub.sh
cat $HOME/t.txt
x=CUSTOMER_${REGION}.out
CUSTOMER_USA.out
AMIGA:barrywalker~> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 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

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

5. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code: WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT) when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved. i tested from... (1 Reply)
Discussion started by: SkySmart
1 Replies

6. Shell Programming and Scripting

White spaces issue with shell variables

Hi all, I've a requirement as below Source file src.txt sample data: A<10 white spaces>B12<5 white spaces>C<17 white spaces> A1<5 white spaces>B22<5 white spaces>C13<17 white spaces> when I'm fetching a record from this file into a shell variable like below: vRec=`head -1 src.txt... (2 Replies)
Discussion started by: madhu_1126
2 Replies

7. HP-UX

Shell script variables

hi everyone, i'm writing shell script on hp-ux server that run by root user then (inside the script) su to database user and appl user..the reason for this script is to run some commands involve all users root and database and appl..anyway, variables when root in control is ok but when su, the... (1 Reply)
Discussion started by: neemoze
1 Replies

8. UNIX for Dummies Questions & Answers

Issue with variables via cronned script

I have script sourcing the profile of functional user like as below and my .profile for functional user has below entry now when i cron the script i am receiving the below error after execution, though i could see the output of variable when i do echo as below. can you tell me where... (11 Replies)
Discussion started by: Ariean
11 Replies

9. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

Hi All, I have a shell script called sample1.sh where I have 2 variables. Now I have another shell script called sample2.sh. I want the variables in sample1.sh to be available to sample2.sh. For example. In sample1.sh I am finding the sum of 2 numbers namely a and b. Now I want to access... (2 Replies)
Discussion started by: rsendhilmani
2 Replies

10. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

I have a variable $exe in a shell script file a.sh which I need to access in another shell script file b.sh. How can I do that? :rolleyes: Thanks!! (2 Replies)
Discussion started by: looza
2 Replies
Login or Register to Ask a Question