How do I perform double substitution in bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I perform double substitution in bash?
# 1  
Old 04-09-2009
How do I perform double substitution in bash?

Code:
#!/bin/bash
#set -x
MAX=255
FILE=$1.dns_list
#declare -a d_arr

if [ -z "$1" ]
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})
        if [ $? = 0 ]
          then
             echo "$PARSE" " $1.$i" >> $FILE
        fi
done

cat $FILE | while read i
do
declare -a d_arr
ln=`echo $i | awk '{printf $5 "\t : \t" $6}' |sed 's/\.XYZ\.com\.//g'`
cnt=${#d_arr[@]}
let c=$cnt
d_arr[$c]=$ln       # <<---<<  I think I need double substitution for this stmt
echo ${d_arr[$c]}
done

# For some reason the array does not exist 
cnt1=${#d_arr[@]}
echo "The size of d_arr: $cnt1"
echo "The array ${d_arr[@]}"
exit

Hi,

How do I perform double substitution in bash?

I try to write a bash script to list all the DNS entry from a subnet e.g. 192.168.1.0/24 and store the valid DNS entry into an array ${d_arr[]},
The variable d_arr[$c] works fine in while read i loop, but once the loop finished, array ${d_arr[]} is empty, I have tried he following:

Code:
let ${d_arr[$c]}=$ln

but get a syntax error.

How do I get it store into an array for the duration when he program is still running?

Thanks,
-Fu

Last edited by rbatte1; 12-13-2016 at 09:44 AM.. Reason: Added CODE tags
# 2  
Old 04-09-2009
try declaring the array outside the while loop
and for double substitution, use eval statement
# 3  
Old 04-09-2009
patjones,

Thank you for your suggestion!
I have tried declare -a d_arr at the beginning of the program, but it doesn't change anything, for dynamic assign value for an array variable, I tried:

Code:
let ${d_arr[eval $c]}=$ln

but still getting syntax error.

Thanks,
-Fu

Last edited by rbatte1; 12-13-2016 at 09:44 AM.. Reason: Added code tags & ICODE tags for clarity
# 4  
Old 04-09-2009
Hi All,

Actually, the following statement:

Code:
d_arr[$c]=$ln

or
Code:
eval "d_arr[$c]=\${ln}"
b

both of the above statement work fine for double substitution, but if you look at the last couple statement of the script:

Code:
cnt1=${#d_arr[@]}
echo "The size of d_arr: $cnt1"
echo "The array ${d_arr[@]}"

outside of the loop all those variable does not get value at all. How do I keep the value within the array variable after the loop has finished?

Thanks,
-flee

Last edited by rbatte1; 12-13-2016 at 09:45 AM.. Reason: Added CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH script to read external file to perform text replacements?

Hi all, I have a moderate size (300 lines) BASH Shell script that performs various tasks on different source reports (CSV files). One of the tasks that it performs, is to use SED to replace 'non-conforming' titles with conformant ones. For example "How to format a RAW Report" needs to become... (3 Replies)
Discussion started by: richardsantink
3 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. UNIX for Dummies Questions & Answers

Double variable substitution

$ ABCel='abc@gmail.com' $ XYZel='xyz@gmail.com' $ MYel='my@yahoo.com' $ echo $ABCel abc@gmail.com $ echo $XYZel xyz@gmail.com $ echo $MYel my@yahoo.com I tried 3 variations, but not getting the expected result: $ for VAR in ABC XYZ; do echo -e "${VAR}el $MYel"; done ABCel... (1 Reply)
Discussion started by: bagnoble
1 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

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): # sourced from a configuration file, we have a lot of... (6 Replies)
Discussion started by: teresaejunior
6 Replies

6. Shell Programming and Scripting

Using echo to print double quotes along with variable substitution

Hi, I am generating html code using cshell, but i am having one problem while printing double quotes, I need to write following code in file. where $var contains list of web address <a href="$var">$var</a> So i am using echo "<a href="$var">$var</a>" > file.html But with this " in... (4 Replies)
Discussion started by: sarbjit
4 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

double substitution

Hello, I would like to dual subtitutes variables. I tried $serv="combo" combo_TYPE="mop" TYPENAME="$serv_TYPE" Now, I'd like to get "mop" from TYPENAME echo "${${TYPENAME}}" I have a bad substitution error :) (1 Reply)
Discussion started by: pppswing
1 Replies

9. Shell Programming and Scripting

Double Substitution variables in ksh

Hi I have a variable whose value is like this i=/test/test1/test2/myfile.cd.070505123457 i would like to have the value of myfile.cd stored into another variable my attempt is test=${i##*/} ;echo $test ##and i get myfile.cd.070505123457 since what i wnat is myfile.cd i try this... (19 Replies)
Discussion started by: xiamin
19 Replies

10. UNIX for Dummies Questions & Answers

double variable substitution

Hi, I want to substitute variable in sed using combination of name and var_$name: name=Tom var_$name=1 sed -n "${var_"$name"}"p < myfile but it doesn't work, do you know what's wrong ? Thanks (1 Reply)
Discussion started by: asal_email2
1 Replies
Login or Register to Ask a Question