Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    7

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

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

    doesnt resolve it.. I also tried to do this..

    Code:
    COMMAND="'s/merge_normal_documents/merge_normal_documents_${PREFIX}/g'"
    print $COMMAND
    
    perl -pi -e $COMMAND $DEST_NAME
    that doesnt work either.. the print comes out ok

    Code:
    (r) [rad-ri_PRD] /SMSCLN/prd/src/res>  BuildNewV2t.sh KEVIN
    
    's/merge_normal_documents/merge_normal_documents_KEVIN/g'
    but when it runs it.. it does nothing.. so I must be missing something.. anyone try to do that same thing with an outcome ?

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Learn to use quotation marks properly. Compare

    Code:
    echo "$HOME"
    with

    Code:
    echo '$HOME'

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

    Code:
    [rad-ri_PRD] /SMSCLN/prd/src/res> vi test.txt
    
    CHANGE1
    CHANGE2
    CHANGE3
    CHANGE4
    wrote a new script with lots of options

    Code:
    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"
    had an error on number two.. go figure

    here is the end result of the text file..

    Code:
    CHANGED_TO_""
    CHANGE2
    CHANGED_TO_
    CHANGED_TO_
    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...

    any ideas ?

  4. #4
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Single quotes inhibit parameter expansion, double quotes do not. Try this:

    Code:
    perl -pi -e "s/CHANGE1/CHANGED_TO_${PREFIX}/g" test.txt
    Use "set -x" to improve your understanding of what the shell does.

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

  6. #6
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by ksauerwald View Post
    I never did a perl script in my life..
    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:

    Code:
    echo $$
    prints the PID of your shell.

    Code:
    perl -e "print $$;"
    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 hand

    Code:
    perl -e 'print $$;'
    will print the PID of the perl interpreter, because the single quotes prevent the shell from replacing "$$" with its own PID.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...