linux - 当我尝试传递 2 个变量时,Sed 未知选项 `s

标签 linux bash sed

我的 sh 脚本中的 sed 命令有问题。 我已经尝试过不使用 -i,使用 > 和 cp 将我的文件替换为备份,删除简单引号并尝试在 s/和/g 之后使用双引号。

echo '=> Enabling Cryptodisk in GRUB'
sed -i 's/#GRUB_ENABLE_CRYPTODISK=y/GRUB_ENABLE_CRYPTODISK=y/g' /etc/default/grub #Enabling Cryptodisk in GRUB

echo '=> Adding Preload_modules in GRUB'
sed -i 's/GRUB_PRELOAD_MODULES="part_gpt part_msdos"/GRUB_PRELOAD_MODULES="part_gpt part_msdos luks cryptodisk"/g' /etc/default/grub #Adding Preload_modules in GRUB

echo '=> Adding Linux CMDLINE in GRUB'
GUIDMAPPER=$(blkid | grep ^/dev/mapper | awk -F "\"" '{print $2}') #Get mapper GUID
GUIDSDA3=$(blkid | grep ^/dev/sda3 | awk -F "\"" '{print $2}') #Get sda3 GUID
sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cryptdevice=${GUIDMAPPER}=${GUIDSDA3}:cryptroot root=/dev/mapper/cryptroot crypto=whirlpool:aes-xts-plain64:512:0:"/g' /etc/default/grub #Adding Linux CMDLINE in GRUB

最后一次 sed 调用会触发错误,其他一切正常。

sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cryptdevice=${GUIDMAPPER}=${GUIDSDA3}:cryptroot root=/dev/mapper/cryptroot crypto=whirlpool:aes-xts-plain64:512:0:"/g' /etc/default/grub

到目前为止已测试...:

sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cryptdevice='"$GUIDMAPPER"'='"$GUIDSDA3"':cryptroot root=/dev/mapper/cryptroot crypto=whirlpool:aes-xts-plain64:512:0:"/g' /etc/default/grub #Adding Linux CMDLINE in GRUB

Result

谢谢(抱歉我的英语不好)

最佳答案

编辑: 它的工作原理是在替换中转义“\”。

sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cryptdevice='"$GUIDMAPPER"'='"$GUIDSDA3"':cryptroot root=\/dev\/mapper\/cryptroot crypto=whirlpool:aes-xts-plain64:512:0:"/g' /etc/default/grub #Adding Linux CMDLINE in GRUB

感谢 Wiktor Stribiżew

关于linux - 当我尝试传递 2 个变量时,Sed 未知选项 `s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60203588/

相关文章:

regex - 如何 grep 具有特定格式的行

linux - 将信息从我的程序传递到调用脚本(退出时)的正确方法是什么

linux - 将 bash 中的文本字符串转换为数组

bash - AWS上的Docker-环境变量未从主机继承到容器

linux - 使用 tee 和 sed 重定向

xml - 根据搜索父叶字符串从 xml 中查找并打印特定字符串

python - 启用 PyROOT Ubuntu 14.04

bash - EC2 用户数据,启动时未执行的特定命令,手动工作正常

sed 在括号内插入一个单词

linux - 尝试使用 grep、sed 等时如何处理颜色代码