0

I am trying to replace on AIX 6.1 every occurrence of the string "FULL_RUN_SCRIPT_DIR" with a path like "/etc/rc.d/.."

Here is the code

        SEE_FILE_PATH="$INIT_DIR$SERVICE_NAME"
        FULL_RUN_SCRIPT_DIR="FULL_RUN_SCRIPT_DIR"
        SERVICE_FILE="$INIT_DIR$SERVICE_NAME"
        echo "String1:$FULL_RUN_SCRIPT_DIR"
        echo "String2:$SEE_FILE_PATH"
        echo "String3:$SERVICE_FILE"

        perl -p -i -e "s/$FULL_RUN_SCRIPT_DIR/$SEE_FILE_PATH/g" "$SERVICE_FILE"

However i get the error :

String1:FULL_RUN_SCRIPT_DIR
String2:/etc/rc.d/init.d/seelinkdeployer
String3:/etc/rc.d/init.d/seelinkdeployer
Bareword found where operator expected at -e line 1, near "s/FULL_RUN_SCRIPT_DIR//etc"
syntax error at -e line 1, near "s/FULL_RUN_SCRIPT_DIR//etc"
Execution of -e aborted due to compilation errors.

Any ideas why?

1

1 Answer 1

1

The first hint at something going wrong is when you got:

... near "s/FULL_RUN_SCRIPT_DIR//etc"

... when you expected the expression to be something like

... near "s/FULL_RUN_SCRIPT_DIR//etc/rc.d/init.d/seelinkdeployer/g"

... with the full value of $SEE_FILE_PATH.

The problem you've run into is that you're using forward-slashes / to separate the search text from the replacement text in perl's s// operator, and there are forward-slashes in -- at least -- your replacement text.

You have a couple options:

  1. Don't use forward-slashes as delimiters in s//; use something that does not exist in either your search text or your replacement text. For example, ~:

    perl -p -i -e "s~$FULL_RUN_SCRIPT_DIR~$SEE_FILE_PATH~g" "$SERVICE_FILE"
    
  2. Replace the slashes with escaped slashes in your search and/or replacement text (assuming a bash-like shell for the // parameter expansion):

    SEE_FILE_PATH=/etc/rc.d/init.d/seelinkdeployer
    SEE_FILE_PATH=${SEE_FILE_PATH//\//\\/}
    

Further reading:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .