Results 1 to 6 of 6
I'm trying to write a quick script to copy a file then rename parts of it using a passed in argument.
so PREFIX is seg to KEVIN lets say.. then ...
- 11-05-2008 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 7
Script variable issue..
I'm trying to write a quick script to copy a file then rename parts of it using a passed in argument.
so PREFIX is seg to KEVIN lets say.. then I use this command..
now the copy works fine.. but when I try to use ${PREFIX} in the statement it ends up just using "process_" for the perl replace.Code:#FIX PROCESS V DEST_NAME=$CURR_DIR/process_{$PREFIX}.cpp cp $CURR_DIR/process_voice_to_text.cpp $DEST_NAME perl -pi -e 's/process_voice_to_text/process_${PREFIX}/g' DEST_NAME
doesnt resolve it.. I also tried to do this..
that doesnt work either.. the print comes out okCode:COMMAND="'s/merge_normal_documents/merge_normal_documents_${PREFIX}/g'" print $COMMAND perl -pi -e $COMMAND $DEST_NAME
but when it runs it.. it does nothing.. so I must be missing something.. anyone try to do that same thing with an outcome ?Code:(r) [rad-ri_PRD] /SMSCLN/prd/src/res> BuildNewV2t.sh KEVIN 's/merge_normal_documents/merge_normal_documents_KEVIN/g'
- 11-06-2008 #2Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Learn to use quotation marks properly. Compare
withCode:echo "$HOME"
Code:echo '$HOME'
- 11-06-2008 #3Just Joined!
- Join Date
- Mar 2007
- Posts
- 7
I looked at your advice and got no where.. so I tried a test..
I made a file
wrote a new script with lots of optionsCode:[rad-ri_PRD] /SMSCLN/prd/src/res> vi test.txt CHANGE1 CHANGE2 CHANGE3 CHANGE4
had an error on number two.. go figureCode:typeset PREFIX PREFIX="SOMETHING NEW" perl -pi -e 's/CHANGE1/CHANGED_TO_"${PREFIX}"/g' test.txt echo "1" perl -pi -e 's/CHANGE2/CHANGED_TO_'${PREFIX}'/g' test.txt echo "2" perl -pi -e 's/CHANGE3/CHANGED_TO_${PREFIX}/g' test.txt echo "3" perl -pi -e 's/CHANGE4/CHANGED_TO_$PREFIX/g' test.txt echo "4"
here is the end result of the text file..
so unless I'm missing something.. I have no clue how to format that perl command so it resolves the $PREFIX and will do the command correctly...Code:CHANGED_TO_"" CHANGE2 CHANGED_TO_ CHANGED_TO_
any ideas ?
- 11-06-2008 #4Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Single quotes inhibit parameter expansion, double quotes do not. Try this:
Use "set -x" to improve your understanding of what the shell does.Code:perl -pi -e "s/CHANGE1/CHANGED_TO_${PREFIX}/g" test.txt
- 11-06-2008 #5Just Joined!
- Join Date
- Mar 2007
- Posts
- 7
See I thought the perl script command would need the single quote to work.. I never did a perl script in my life.. and only wrote like 5 shell ones..
that worked.. thanks..
- 11-07-2008 #6Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
That is entirely irrelevant. We are talking about preventing the shell from doing parameter expansion (or other types of expansion, for that matter). For example:
prints the PID of your shell.Code:echo $$
also prints the PID of your shell because the shell replaces the string "$$" with its own PID before handing that string to perl. So what perl actually gets is "print 1234;" (or some other random number). On the other handCode:perl -e "print $$;"
will print the PID of the perl interpreter, because the single quotes prevent the shell from replacing "$$" with its own PID.Code:perl -e 'print $$;'


Reply With Quote
