Visit The New, Modern Unix Linux Community


Bash script having variable substitution problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script having variable substitution problems
# 1  
Wrench Bash script having variable substitution problems

Hi

I am setting the variables like this :
HTML Code:
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

################################################
HTML Code:
#!/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

Code:
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

Last edited by Scrutinizer; 05-09-2014 at 07:11 AM.. Reason: additional code tags
# 2  
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.
# 3  
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):
Code:
c=1
TMP=MODULE$c
echo ${!TMP}

# 4  
Wrench further debugging on variable substitution

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

Code:
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

Code:
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
# 5  
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
Code:
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..

Last edited by Scrutinizer; 05-09-2014 at 07:31 AM..
# 6  
Thanks a lot !

Its working now

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

do

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

done

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

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #281
Difficulty: Easy
A hypervisor or virtual machine monitor (VMM) is a computer software, firmware or hardware that creates and runs virtual private networks.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script problems int to binary

Hi, I am trying to do a bash script that convert a decimal number to a binary value, but it doesn't work... To begin, I am just trying to convert a positive number to 8 bits binary. read -p"Entrez un nombre entre -128 et 127 pour l'encoder en binaire: " number binaryValues=(128 64 32 16 8 4 2... (8 Replies)
Discussion started by: Zedki
8 Replies

2. Shell Programming and Scripting

sed problems - Bash Script

Hi I keep getting the following error sed: -e expression #1, char 32: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command whenever I use the following bash script #! /bin/bash... (2 Replies)
Discussion started by: spbr
2 Replies

3. Shell Programming and Scripting

Partial variable substitution in script

I have a script. filecreatenew () { touch /usr/src/$1_newfile.txt var=$1 echo $var touch /usr/src/$var_newfile_with_var.txt } filecreatenew myfile Its creating file /usr/src/myfile_newfile.txt as the variable $1 is correctly used. When $ is... (2 Replies)
Discussion started by: anil510
2 Replies

4. Shell Programming and Scripting

sed variable substitution in a script

Hi I am trying to do the following in a script find a string and add in a block of text two lines above on the command line this works fine #/usr/bin/cat /usr/local/etc/dhcpd.conf_subnet | /usr/xpg4/bin/sed -n -e '1h;1\!H;${;g;s/}.*#END of 10.42.33.0/#START of RANGE $dstart\:option... (3 Replies)
Discussion started by: eeisken
3 Replies

5. Shell Programming and Scripting

Execution problems with BASH Shell Script

Hi I need help with my coding , first time I'm working with bash . What i must do is check if there is 3 .txt files if there is not 3 of them i must give an error code , if al three is there i must first arrange them in alphabetical order and then take the last word in al 3 of the .txt files... (1 Reply)
Discussion started by: linux newb
1 Replies

6. Shell Programming and Scripting

Execution Problems with bash script

Hello, can someone please help me to fix this script, I have a 2 files, one file has hostname information and second file has console information of the hosts in each line, I have written a script which actually reads each line in hostname file and should grep in the console file and paste the... (8 Replies)
Discussion started by: bobby320
8 Replies

7. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

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

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. Shell Programming and Scripting

unzip via bash startup script problems

i have two lines in my rc.local file that are wget -O/<path>/<file>.zip url://domain.com unzip -o /<path>/<file>.zip the wget works fine, but the unzip won't work. when i copy/pase the unzip line to the prompt it works fine. i thought that maybe the unzip was running before the wget... (0 Replies)
Discussion started by: easysnowboards
0 Replies

Featured Tech Videos