gpt4 book ai didi

linux - sed 命令不接受 ksh 中带有 '.' 的子字符串

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:42 24 4
gpt4 key购买 nike

我正在编写一个 ksh 脚本,该脚本将包含 @sys 子字符串的路径变量处理为其相应的操作系统名称:

如果build_type是64位版本,我们需要在osver末尾附加.64

例如如果 opsys=amd64_linux26_rh5(Red Hat Enterprise Linux 5) 和 build_type=dbg64osver 应为 amd64_linux26_rh5.64

现在,我想将 file_path 变量中存在的 @sys 替换为其相应的操作系统版本作为子字符串,即 amd64_linux26_rh5.64

但看起来 sed 正在忽略 .不知何故,唯一的 amd64_linux26_rh5 正在替换 @sys 并省略 .64 部分。

#!/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"

对应的输出:

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

我们正在尝试将file_path更改为/xxx/xxx/xxx/amd64_linux26_rh5.64/xxx。请建议在这种情况下使用 sed 的适当方法。

最佳答案

当输出显示时

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

然后在复制粘贴代码时出了问题。
在您的代码中,file_path 设置为

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

并且在echo之前从未改变。所以你的代码似乎与你显示的不同。当您在

之后开始时
 echo "old file_path is : $file_path"

输出是有道理的。
在你的 sed 命令中

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

sed 不会在字符串 /xxx/xxx/xxx/amd64_linux26_rh5/xxx 中找到 @sys,并且 old_string 保持不变。

关于linux - sed 命令不接受 ksh 中带有 '.' 的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com