set a variable with a variable name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set a variable with a variable name
# 8  
Old 11-03-2009
Ooops, in my real script, I'm not using the file as it is but I grep it first.
What's the correct syntax to use then?
Code:
r29829:~# cat conf
# comment
A=1
B=2
# C is set to three
C=3
r29829:~# cat testconf.sh
#!/bin/bash
while IFS="=" read lvalue rvalue; do
        eval $lvalue=$rvalue
done < grep -v '^#' conf
echo "-$A-$B-$C-"
r29829:~# ./testconf.sh
./testconf.sh: line 4: syntax error near unexpected token `-v'
./testconf.sh: line 4: `done < grep -v '^#' conf'

# 9  
Old 11-03-2009
Hi Chebarbudo,
in bash (and only in bash) the correct syntax would be:
Code:
#!/bin/bash
while IFS="=" read lvalue rvalue; do
        eval $lvalue=$rvalue
done < <(grep -v '^#' conf)
echo "-$A-$B-$C-"

An alternative that also works in other shells would be:
Code:
grep -v '^#' conf|
{ while IFS="=" read lvalue rvalue; do
        eval $lvalue=$rvalue
  done
  echo "-$A-$B-$C-"
}

# 10  
Old 11-04-2009
Hi Scrutinizer,

Thanks for your help, the bash syntax works fine.
Although do you know where I can find documentation for this type of redirection? So far I only knew the following:
|
>
>>
<
<<
<<<


Santiago
# 11  
Old 11-04-2009
Hi Santiago,

The redirection is a simple < together with process substitution

S.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

do you have a better way to set this variable?

greetings, i have a variable $input that i want to use to set $output. $input is /dir/filename.mph and $input is passed to my script that i manipulate it as follows: input=`basename $input`i want the $output to be filename_solved.mph, basically stuffing "_solved" in the filename. here's how i... (2 Replies)
Discussion started by: crimso
2 Replies

2. Shell Programming and Scripting

How to know who and where a variable is set ?

hi, i'm not a root user and i want to know which user and in which file is loaded a variable seen in the "env" display ? I will use this variable but i want to be sure that it will be a permanent variable ! i don't see it in my files (.profile , kshrc...) and neither in /etc/profile. ... (3 Replies)
Discussion started by: Nicol
3 Replies

3. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

4. UNIX for Dummies Questions & Answers

How to set a variable with a count variable i.e. VARIABLE$COUNT

Hi All I've very nearly finished this script I'm working on but have hit another idiots problem, after googling I can't see a solution for this one. I have a while count loop that checks checks two consecutive values then increments the count by two. What the script has to do is then check... (5 Replies)
Discussion started by: Bashingaway
5 Replies

5. Shell Programming and Scripting

Set variable with variable

Okay, probably an easy solution, but I've given up. I want to set a variable with a variable and print it in PHP. I just can't get the syntax right. As an example: $place = "T"; $norm_t = array("4","7"); I want to use this in a for-loop. How do I echo the first number of the norm_t... (4 Replies)
Discussion started by: wxornot
4 Replies

6. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

7. Shell Programming and Scripting

global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code: function f_open_log { typeset -r... (5 Replies)
Discussion started by: robotball
5 Replies

8. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

9. Linux

How do i set environment variable

Hi, I am quite new to Linux. And I have doubt how to set new environment variable with value to a C executable. Let say I have a environment variable $Hack ; I would like to load a value for this variable; so that when the C executable is executed, the $Hack would set the variable value. ... (4 Replies)
Discussion started by: ahjiefreak
4 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question