Sponsored Content
Top Forums Shell Programming and Scripting How do I perform double substitution in bash? Post 302305709 by flee on Thursday 9th of April 2009 02:26:12 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
RBASH(1)						      General Commands Manual							  RBASH(1)

NAME
rbash - restricted bash, see bash(1) RESTRICTED SHELL
If bash is started with the name rbash, or the -r option is supplied at invocation, the shell becomes restricted. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the follow- ing are disallowed or not performed: o changing directories with cd o setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV o specifying command names containing / o specifying a file name containing a / as an argument to the . builtin command o specifying a filename containing a slash as an argument to the -p option to the hash builtin command o importing function definitions from the shell environment at startup o parsing the value of SHELLOPTS from the shell environment at startup o redirecting output using the >, >|, <>, >&, &>, and >> redirection operators o using the exec builtin command to replace the shell with another command o adding or deleting builtin commands with the -f and -d options to the enable builtin command o using the enable builtin command to enable disabled shell builtins o specifying the -p option to the command builtin command o turning off restricted mode with set +r or set +o restricted. These restrictions are enforced after any startup files are read. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell spawned to execute the script. SEE ALSO
bash(1) GNU Bash-4.0 2004 Apr 20 RBASH(1)
All times are GMT -4. The time now is 01:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy