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 (( c=1; c<=${COUNT}; c++ ))

do
   
sed -n '/module '${MODULE}'${c} /,/endmodule/p' prescan.v > temp${c}.v

sed -i '0,/o_func_clk /s/o_func_clk/o_func_clk, DFT_ClkEnScanIn1 ,scan_clock_en , DFT_ClkgenScanEnable  /' temp${c}.v
#sed -i '0,/i_UDR_DFT_free_running;/i_UDR_DFT_free_running;/i_UDR_DFT_free_running , DFT_ClkEnScanIn1 , DFT_ClkgenScanEnable ;/' temp${c}.v

sed -i 's/i_UDR_DFT_free_running;/i_UDR_DFT_free_running , DFT_ClkEnScanIn1 , DFT_ClkgenScanEnable ;/' temp${c}.v

sed -i '/\[0\:0\] i_func_clk_en/a output  scan_clock_en ;' temp${c}.v
sed -i '/scan_clock_en_reg/,+1d' temp${c}.v

sed -i '/endmodule/i M8E35B_SDFF2X1  scan_clock_en_reg (.q ( scan_clock_en ) , .d (pre_scan_clock_en ) , .te ( DFT_ClkgenScanEnable ) , .ti ( DFT_ClkEnScanIn1 ) , .phi ( i_scan_clk ));' temp${c}.v


done

This script should generate multiple temp files temp1.v temp2.v temp3.v temp4.v
after being processed with sed scripts.

The problem is with the first line

sed -n '/module '${MODULE}'${c} /,/endmodule/p' prescan.v > temp${c}.v

sed is unable to do variable substitution.

I tried with double quotes also substitution is not happening and getting empty temp files.

The $c should be incremented and the $MODULE1 $MODUL2 should be read depending upon the environmental variable set and the pattern should be searched and temp file should be created.

Please let me how to tackle this issue

sed never does variable substitution, that's bash's job.

BASH does not substitute variables inside single quotes ' '. Use double quotes " " when you want substitution.

Your quoting is not correct, that ${c} after module will not be evaluated. And, if I infer sort of correctly from what you say, you're trying "dynamic" variables. That does not work that way. As you seem to be using bash, try indirect expansion (see man bash):

c=1
TMP=MODULE$c
echo ${!TMP}

hi
Thats true I am looking for dynamic variables ...
Cant they be created inside bash for loop
I am debugging more on this

sed_arg="-n '/module $MODULE1/,/endmodule/p' modem5_2_top_aapd_wrapper.prescan.v > temp1.v"
eval sed "$sed_arg"

Here the temp1 is getting generated but
Suppose I am setting variable and trying like this

sed_arg="-n '/module "$MODULE$c"/,/endmodule/p' modem5_2_top_aapd_wrapper.prescan.v > temp${c}.v"
eval sed "$sed_arg"

Substitution is not at all happening and I am getting empty temp file ...

How to tackle this kind of scenario ...

$Module${c} and need to use it in the loop

You cannot put the redirect into the variable like that and in general you are in for a quoting exercise.

Why not use RudiC's suggestion or

eval mod=\$MODULE$c
echo "$mod"

And use the latter variable for your substitutions.. There is always a security concern with eval . In this case c is a numerical value, controlled by you..

Thanks a lot !

Its working now

###############################

for (( p=1; p<=${COUNT}; p++ ))

do

temp_1=MODULE$p
sed -n "/module ${!temp_1}/,/endmodule/p" prescan.v > temp${p}.v

done 

###################################