Correct bash substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Correct bash substitution
# 1  
Old 08-28-2010
Correct bash substitution

Hello!

I'm writing a shell script using #!/bin/bash instead of #!/bin/sh because of the substitution: ${!variable}, which won't work with sh. My main problem is the following (just a summarized example, the script is much more complex):

Code:
# sourced from a configuration file, we have a lot of options
opt_1="My file"
opt_2="My option"
opt_3="My variable..."

# then we use a gui to request the new value, which will return:
form="New File Name|option 2|option 3"

x=1
printf "$form\n" | sed 's/|/\n/g' | while read option; do
    export opt_$x="$option" # we use export, which works with other scripts
    x=$((x+1))
done

for number in $(seq 1 3); do
    opt=opt_$number
    echo "$opt=${!opt}" # ${!opt} returns the old value, not the new exported!
done

Thank you!

---------- Post updated at 02:00 PM ---------- Previous update was at 12:32 PM ----------

At least someone to tell me the POSIX alternative for ${!variable}, please:

Code:
$ value=hello
$ greeting=value
$ echo $greeting
value
$ echo ${!greeting}
hello


Last edited by teresaejunior; 08-28-2010 at 02:12 PM.. Reason: You could paste this now in a terminal
# 2  
Old 08-28-2010
This, in my opinion, is one of the serious shortcomings of bash. When piping the output of a command to a while loop, the contents of the while are executed in a forked process and thus variables that are local to the script, and should logically be updated as a part of the loop, are not.

I'd run your script using ksh (#!/usr/bin/env ksh) provided it is installed on your host.

Small example to illustrate:
Code:
# count lines in output from ps command
var=1
ps -ef | while read x
do
        var=$(( $var + 1 ))
done
echo $var

Running the script (t22) with bash vs ksh:
Code:
$ ksh t22
126
$ bash t22
1

Ksh does it right, the variable var is updated in the while as expected. Ksh also supports the ${!name} construct you are looking for.

The kshell man page:
http://www2.research.att.com/sw/down.../man1/ksh.html

Last edited by agama; 08-28-2010 at 02:17 PM.. Reason: changed wording to make more clear.
This User Gave Thanks to agama For This Post:
# 3  
Old 08-28-2010
This is helpful, thanks! Though some parts of my script won't work in ksh. But, now I can redirect the loop output to a temp file and source it. Should be enough.Smilie

Just an addition, there could be some smarter way to say:

Code:
opt=opt_$number
echo "$opt=${!opt}"

Something like:
Code:
opt_$number=${!opt_$number} # which of course won't work

Thank you!
# 4  
Old 08-28-2010
Quote:
Originally Posted by teresaejunior
Though some parts of my script won't work in ksh.
I would be very interested in knowing what doesn't work under Ksh.

Quote:
Just an addition, there could be some smarter way to say:

Code:
opt=opt_$number
echo "$opt=${!opt}"

Something like:
Code:
opt_$number=${!opt_$number} # which of course won't work

Im not sure what your goal is with this, but this might work.
Code:
eval opt_$number=\${!opt_$number}

Causes a double evaluation of the statement. $number will be expanded, and then the ${...} will be expanded.
# 5  
Old 08-28-2010
Hi, agama!

It's a set of scripts which are going to be packaged together, they're almost ready, so I would have to check the functionality of them all under another interpreter. From this script:

Code:
exec 3> >(application)

In bash it runs the application, in ksh it redirects the output to the file ?Ê.

Last edited by teresaejunior; 08-28-2010 at 04:00 PM..
This User Gave Thanks to teresaejunior For This Post:
# 6  
Old 08-28-2010
Quote:
Originally Posted by teresaejunior
Hi, agama!

It's a set of scripts which are going to be packaged together, they're almost ready, so I would have to check the functionality of them all under another interpreter.
I certainly understand that -- glad you could work round the issue in bash.

Quote:
Code:
exec 3> >(application)

In bash it runs the application, in ksh it redirects the output to the file ?Ê.
Just for the future, that can be done in ksh, though it is a completely different syntax all together:

Code:
command |&

Then use the read/write commands to read/write from/to the application. It's also possible to map the pipe to stdin/out.

Best on your release.
# 7  
Old 08-28-2010
Works well, thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time. Here is a fragment of my windows10/cygwin64/bash script: export BUPLOG=$(BackupRecords --log "$src") robocopy $(BackupRecords -mrbd "$src" --path "$src") $(BackupRecords --appSwitches "$src") "$src" "$dst" $(BackupRecords --fileSwitches... (15 Replies)
Discussion started by: siegfried
15 Replies

2. Shell Programming and Scripting

How to Force command substitution evaluation in bash?

OK, I'm striving to abide by all the rules this time. Here is a fragment of my windows10/cygwin64/bash script: export BUPLOG=$(BackupRecords --log "$src") robocopy $(BackupRecords -mrbd "$src" --path "$src") $(BackupRecords --appSwitches "$src") "$src" "$dst" $(BackupRecords --fileSwitches... (0 Replies)
Discussion started by: siegfried
0 Replies

3. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

4. Shell Programming and Scripting

bash, command line substitution

I have one script calling another with a set of strings that includes white space. Script A calls Script B with these input strings: one two "th ree" Script B pulls apart the arguments correctly: arg0 = one, arg1 = two, arg2 = "th ree" if I call it from within Script A like so:... (10 Replies)
Discussion started by: skippyV
10 Replies

5. Shell Programming and Scripting

bash: correct quoting with find and exiv2?

Hi, I need to embed a metatag to image files which contain qrcodes, i usually do this with exiv -M "set Exif.Image.DocumentName `zbarimg -q -Sdisable -Sqrcode.enable --raw image.tif`" image.tif which works fine. However I need to do this recursivly for whole directory and subdiretory... (4 Replies)
Discussion started by: mcframe
4 Replies

6. Shell Programming and Scripting

bash: combine arrays with weird substitution/references

Hi all. I'm trying to finish a bash script with the following elements: ARRAY="blah $ITEM blah blah" ARRAY="blah blah $ITEM blah bluh" #ARRAY="...." # ...the ARRAY elements represent a variable but defined # syntax and they're all hard-coded in the script. #(...) ITEMS='1.0 2.3... (2 Replies)
Discussion started by: yomaya
2 Replies

7. Shell Programming and Scripting

/bin/bash - variable substitution.

Is it possible with a bash variable to perform multiple substitution strings to one variable? I have this variable: echo $clock TIMEZONE="US/Central" What I would like to do with bash only it pull out just the "US" part of the variable.. which could be any number of countries. this is... (6 Replies)
Discussion started by: trey85stang
6 Replies

8. Shell Programming and Scripting

How do I perform double substitution in bash?

#!/bin/bash #set -x MAX=255 FILE=$1.dns_list #declare -a d_arr if then echo "Usage: `basename $0` network" echo " e.g.`basename $0` 1.1.1" exit fi echo "Remove file $FILE..." rm $FILE for (( i = 1; i < $MAX; i++ )) do PARSE=$(host $1.${i}) ... (3 Replies)
Discussion started by: flee
3 Replies

9. UNIX for Dummies Questions & Answers

Bash: bad substitution problem...pls help!

I have this situation in my script (simplified): A=C C=10 I need to get number 10 using just A variable. I tried with : echo $`echo $A` - but i get $C string (i need number) Thanks very much for any help! (1 Reply)
Discussion started by: xfouxs
1 Replies
Login or Register to Ask a Question