Variable not passed to the sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable not passed to the sed command
# 8  
Old 03-06-2015
Quote:
Originally Posted by Scrutinizer
Well then it DOES work, doesn't it (L1ITMBLT_trash.root)?
i want the output as following:
Code:
sed -i 's/L1ITMBLT.root/L1ITMBLT_009_0.root/g' run_DttfFromCombinedPrimitives_cfg.py

which is the value of Index taken from the awk command
# 9  
Old 03-06-2015
Yes but you will need to assign the value to the Index variable outside the here document. You cannot assign a value to a variable inside...

--
An alternative is to escape the $ sign inside the here document and let the generated script substitute the variable:
Code:
sed -i 's/L1ITMBLT.root/L1ITMBLT_\$Index.root/g' $CONFIGFILE

then your generated file would become:
Code:
#!/bin/sh                                                                                                                    
sed -i "s/InputFile[a-zA-Z0-9]*/InputFile_009_0.txt/g" run_DttfFromCombinedPrimitives_cfg.py
Index=_009_0
sed -i 's/L1ITMBLT.root/L1ITMBLT_$Index.root/g' run_DttfFromCombinedPrimitives_cfg.py


Last edited by Scrutinizer; 03-06-2015 at 11:22 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 03-06-2015
Yes, it fixed the problem..
Would like to know why the new value could not be assigned to the Index inside ?

thanks again,
emily
# 11  
Old 03-06-2015
I had edited the post in the mean time, have a look..
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 03-06-2015
My code setup seem to be reaching it completeness to perform the desired task..thanks a lot everyone for great help Smilie Smilie
I always find useful and timely help here..

Dear Scrutinizer,
I just have a small question, which is "why the new value could not be assigned to the Index inside ?"

thank you,
Emily
# 13  
Old 03-06-2015
Quote:
Originally Posted by emily
My code setup seem to be reaching it completeness to perform the desired task..thanks a lot everyone for great help Smilie Smilie
I always find useful and timely help here..

Dear Scrutinizer,
I just have a small question, which is "why the new value could not be assigned to the Index inside ?"

thank you,
Emily
You're welcome...

From man bash:
Quote:
Here Documents
This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.
So everything inside the here document is standard input to the cat command (not commands that are executed)

Since EOF is unquoted:
Quote:
If word is unquoted, all lines of the here-document are subjected to parameter
expansion, command substitution, and arithmetic expansion...
This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 03-06-2015
Quote:
Originally Posted by emily
I modified the script as following, did not help either
Code:
#!/bin/sh
Index=trash
[....]
<<EOF 
[....]
Index=$(echo $FILE | awk '{match($0,/\_.*\_/);print substr($0,RSTART,RLENGTH+1)}')
sed -i 's/L1ITMBLT.root/L1ITMBLT_$Index.root/g' $CONFIGFILE
[...]
EOF

the output is following
Code:
[...]
sed -i "s/InputFile[a-zA-Z0-9]*/InputFile_009_0.txt/g" run_DttfFromCombinedPrimitives_cfg.py
Index=_009_0
sed -i 's/L1ITMBLT.root/L1ITMBLT_trash.root/g' run_DttfFromCombinedPrimitives_cfg.py

I suppose the problem comes from a misunderstanding about what is done when in the shells evaluation process.

When you write a process substitution in a here-document:

Code:
while read line ; do
     echo $line
done <<EOF
$(some_command)
EOF

The following happens: a subshell is loaded and "some_command" is executed in this subshell. The output (to stdout) of this process is taken and the process substitution is replaced with this outcome. Lets say the outcome is "blahblah" then the shell would arrive at this:

Code:
while read line ; do
     echo $line
done <<EOF
blahblah
EOF

Only now this resulting here document is fed to the (compound)-command. I used a while-loop for the example but it could be any other single or complex command too. Notice, that "blahblah" is not treated as a command - it is treated as a string! This will perfectly work even though the shell wouldn't know what to make of a command "blahblah".

Now consider this:

Code:
while read line ; do
     echo $line
done <<EOF
x=blahblah
EOF

Yes, it looks like variable "x" would be assigned some value, but in fact this is not treated as a command either - it is just a string, the same way "blahblah" alone was just a string!

You had this string in the here-document and expected it to be executed - but this is not the case. The shell only evaluates the here-document, which means that process substitution gets done and variables are expanded:

Code:
var="blahblah"
while read line ; do
     echo $line
done <<EOF
x=$var
EOF

In this example the string "$var" willl be replaced by the content of the variable "var", which is "blahblah", therefore the resulting string passed to the while-loop will again be "x=blahblah", but this will still not be executed and a variable "x" will still not have the value "blahblah" at all.

What you can do to achieve what you obviously want is the following:

Code:
[...your script....] <<EOF
[...here-document....]
sed -i 's/L1ITMBLT.root/L1ITMBLT_$(echo $FILE | awk '{match($0,/\_.*\_/);print substr($0,RSTART,RLENGTH+1)}').root/g' $CONFIGFILE 
[...more here-document....]
EOF

but i admit this looks ugly and i suggest not to do it at all. My suggestion is based on ksh (don't know if you have that) and looks like this (only sketched out). Note that for-loops with an undefined number of elements is a bad idea in any shell because it can break if "*txt" evaluates to too many files. I used a while-loop therefore. I also removed the awk-scripts where they could be replaced by simple shell-expansion:

Code:
#! /bin/ksh
typeset CONFIGFILE=run_DttfFromCombinedPrimitives_cfg.py
typeset FILE=""
typeset Scriptname=""

ls *txt |\
while read FILE ; do
    echo $FILE
    ScriptName="${FILE%*\.txt}.sh"
    Index=$(echo $FILE | awk '{match($0,/\_.*\_/);print substr($0,RSTART,RLENGTH+1)}')

    exec 3>"$ScriptName"
    print -u3 - "#!/bin/sh"
    print -u3 - "sed -i 's/InputFile[a-zA-Z0-9]\*/${FILE}/g' $CONFIGFILE"
    print -u3 - "sed -i \"s/L1ITMBLT.root/L1ITMBLT_${Index}.root/g $CONFIGFILE\""
    print -u3 - "#cmsRun"
    print -u3 - $CONFIGFILE"
    exec 3>&-

done

I admit, i couldn't make out what the awk-orgy is supposed to do, so i let it in place. You might want to replace it with a shell expansion eventually.

I hope this helps.

bakunin

Last edited by bakunin; 03-06-2015 at 07:02 PM..
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable not being passed

In the bash below the variable date displays in the echo. However when I use it in the for loop it does not. Basically, the user inputs a date then that date is converted to the desired format of (month-day-year, no leading 0). That input is used in the for loop to return every file that matches... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Variable passed as argument

I have a script. #!/bin/sh cur_$1_modify_time=Hello echo "cur_$1_modify_time" When I run like sh /root/script1 jj I expect value "Hello" being assigned to variable "cur_jj_modify_time" and output being "Hello" ie echoing $cur_jj_modify_time But the output comes as # sh... (3 Replies)
Discussion started by: anil510
3 Replies

3. Shell Programming and Scripting

In the sh file variable is not being passed on.

I am having difficulties with the fllowing script: !/bin/sh voicemaildir=/var/spool/asterisk/voicemail/$1/$2/INBOX/ echo `date` ':' $voicemaildir >> /var/log/voicemail-notify.log for audiofile in `ls $voicemaildir/*.wav`; do transcriptfile=${audiofile/wav/transcript} ... (4 Replies)
Discussion started by: ghurty
4 Replies

4. UNIX for Advanced & Expert Users

Value of variable not getting passed in child script

Hi, I am facing a challenge in fixing an issue in my installation scripts.Here is a situation: There are 3 files which are invoked at a below given order: Installer.ksh----->Installer.xml(Ant script)------->common.ksh I am outputting a message from common.ksh at a terminal, after that trying to... (3 Replies)
Discussion started by: baig_1988
3 Replies

5. Shell Programming and Scripting

Why Perl Subroutine Passed In Variable is 1?

The following subroutine prints 1 instead of the content of the Equipment variable. Can someone tell me why? #!c:/perl/bin/perl.exe # use strict 'vars'; my $Equipments = "data/equips.txt"; unless (open(EQUIP_FH, "$Equipments")) { print "errors: $Equipments\n"; # This line prints... (1 Reply)
Discussion started by: tqlam
1 Replies

6. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

7. Shell Programming and Scripting

My variable cannot be passed through into my path

Hi, Can you please help. I am scripting in sh and I am trying to simply copy one directory to another but for some reason my variables are not recognised? echo "The latest version of the program is being found......." cd $SOFTWARE/src/$progname version=`ls $SOFTWARE/src/$progname | grep... (13 Replies)
Discussion started by: cyberfrog
13 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. UNIX for Dummies Questions & Answers

variable passed to awk

Does anybody know how to print a variable passed to awk command? awk -F"|" 'BEGIN {print $job,"\n","Question \n"} {print $1,$2$4," ",$3}' "job=$job1" file1 I am trying to pass job the variable job1. the output is blank. ?? (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Dummies Questions & Answers

variable passed to awk

Anybody know what's wrong with this syntax? awk -v job="$job" 'BEGIN { FS="|"} {print $1,$2," ",$4," ",$3\n,$5,"\n"}' list It's keeping give me this message: awk: syntax error near line 1 awk: bailing out near line 1 It seems awk has problem with my BEGIN command. Any... (8 Replies)
Discussion started by: whatisthis
8 Replies
Login or Register to Ask a Question