sed command not accepting sub-string with '.&#

2019-09-19 10:21发布

问题:

I am writing a ksh script which processes a path variable containing @sys substring to its corresponding OS name:

If build_type is is of 64bit version we need to append .64 at the end of osver.

e.g. if opsys=amd64_linux26_rh5(Red Hat Enterprise Linux 5) and build_type=dbg64, osver should be amd64_linux26_rh5.64.

Now, I want to replace @sys present in file_path variable with its corresponding OS version as a substring i.e. amd64_linux26_rh5.64.

But it looks like sed is ignoring . somehow and the only amd64_linux26_rh5 is replacing @sys omitting the .64 part.

#!/bin/ksh
file_path="/xxx/xxx/xxx/@sys/xxx"
sys="@sys"
bld_type="dbg64"
opsys="amd64_linux26_rh5"
bitver=".64"
if [[ ${bld_type} = @(*64) ]]
then
    osver="${opsys}${bitver}"
fi
echo "osver: $osver"
echo "old file_path is : $file_path"
file_path=$(echo "$file_path" | sed "s/$sys/${osver}/")
echo "new file_path is : $file_path"

The corrosponding output:

osver: amd64_linux26_rh5.64
old file_path is : /xxx/xxx/xxx/amd64_linux26_rh5/xxx
new file_path is : /xxx/xxx/xxx/amd64_linux26_rh5/xxx

We are trying to change file_path to /xxx/xxx/xxx/amd64_linux26_rh5.64/xxx. Please suggest an appropriate way use sed in this case.

回答1:

When the output shows

old file_path is : /xxx/xxx/xxx/amd64_linux26_rh5/xxx

then something went wrong in copy-pasting the code.
In your code the file_path is set to

file_path="/xxx/xxx/xxx/@sys/xxx"

and is never changed before the echo. So your code seems to be different from what you show. When you start after the

 echo "old file_path is : $file_path"

the output makes sense.
In your sed command

file_path=$(echo "$file_path" | sed "s/$sys/${osver}/")

sed will not find @sys in the string /xxx/xxx/xxx/amd64_linux26_rh5/xxx, and the old_string remains unchanged.



标签: linux sed ksh