用 bash 函数的结果替换部分模式 space
Replace part of pattern space with result of bash function
我读到按名称 (/dev/sdxy) 挂载设备不太安全,因为设备名称可能会在重新启动之间发生变化。所以,我想用 UUID 代替设备名称生成一个 fstab 文件。互联网上的教程建议我找到我想要的设备的 UUID,然后手动将它们复制粘贴到 /etc/fstab.
我认为必须有一种方法可以使它自动化,我正在尝试让 sed 从脚本中工作。这是我最接近的工作脚本:
#!/bin/bash
function GetUUID()
{
# Arg 1 is expected to be a device (such as /dev/sdc1)
# This function calls blkid on the device and prints to stdout UUID=[uuid], without quotes, so that it can be passed out to SED.
echo -n calling GetUUID on
FullString=$(blkid | tr -d '"')
UUID=$(echo ${FullString} | cut -d ' ' -f2)
echo $UUID
}
# Greps /etc/mtab and looks for user given disk (say, /dev/sdc)
# Changes the names of all mounted partitions of that disk (eg all /dev/sdcx) to their UUIDs, in compatible format with /etc/fstab
# Outputs results to stdout
if [[ $# -eq 0 ]]
then
# If no argument, I will not try to guess a default, that looks dangerous.
echo "Usage : RobustMtabCopy /dev/somedisk (will search mstab for all /dev/somediskX and replace the names by their UUIDS)"
exit 22
else
# Else, look for anything like /dev/somediskX, source the function that will make the output pretty, get the pretty form of UUID, and put it in place of the device name in the output.
grep /etc/mtab | sed -E "s|([[:digit:]])|$(GetUUID )|g"
fi
预期的输出是这样的:
UUID=SOME-UUID mount-point fs options 0 0
UUID=SOME-OTHER-UUID mount-point fs options 0 0
实际输出:
./script.sh /dev/sdc
mount-point fs options 0 0
mount-point fs options 0 0
一点点调试表明我用参数“1”调用了 GetUUID(因此 blkid 输出空字符串)。转义 \ 没有帮助。
有几个很好的建议并不完全符合我的要求:
- 我在使用 how-to-replace-a-value-with-the-output-of-a-command-in-a-text-file 时遇到了问题,因为我调用的函数使用了我在 sed 中匹配的模式,我不知道如何将它传递给我的命令;
- 我在 Whosebug 上读到一些关于使用“e”标志从 sed 中调用 shell 命令的内容;它对我来说不太适用,因为整个模式都被替换了,我只想替换匹配项(虽然我不能把手放在参考上)。
如有任何帮助,我们将不胜感激。
使用 GNU sed 作为替换命令的 e
修饰符:
grep "" /etc/mtab |
sed -E 's|(.*)('""')(.*)|printf "UUID=%s %s%s\n" "$(blkid -s UUID -o value "")" "" ""|e'
但请注意:您必须传递与完整设备名称完全匹配的正则表达式,不能多也不能少。示例:
$ ./script.sh '/dev/sdc[0-9]*'
UUID=4071fbd0-711a-477d-877a-ee4b6be261fc /tmp ext4 rw,relatime 0 0
UUID=a34227b0-bb5e-44fb-9207-dc48cf4be022 /home ext4 rw,relatime 0 0
UUID=bcfa9073-79ad-43ca-ba34-ceb8aecb23bf /var ext4 rw,relatime 0 0
如果像您的示例一样,您已经知道您拥有的设备类型 (/dev/sd[a-z][0-9]+
) 并且只想传递前导词干,您可以调整 sed 脚本:
grep "" /etc/mtab |
sed -E 's|(.*)('""'\S+)(.*)|printf "UUID=%s %s%s\n" "$(blkid -s UUID -o value "")" "" ""|e'
然后:
$ ./script.sh '/dev/sdc'
UUID=4071fbd0-711a-477d-877a-ee4b6be261fc /tmp ext4 rw,relatime 0 0
UUID=a34227b0-bb5e-44fb-9207-dc48cf4be022 /home ext4 rw,relatime 0 0
UUID=bcfa9073-79ad-43ca-ba34-ceb8aecb23bf /var ext4 rw,relatime 0 0
我读到按名称 (/dev/sdxy) 挂载设备不太安全,因为设备名称可能会在重新启动之间发生变化。所以,我想用 UUID 代替设备名称生成一个 fstab 文件。互联网上的教程建议我找到我想要的设备的 UUID,然后手动将它们复制粘贴到 /etc/fstab.
我认为必须有一种方法可以使它自动化,我正在尝试让 sed 从脚本中工作。这是我最接近的工作脚本:
#!/bin/bash
function GetUUID()
{
# Arg 1 is expected to be a device (such as /dev/sdc1)
# This function calls blkid on the device and prints to stdout UUID=[uuid], without quotes, so that it can be passed out to SED.
echo -n calling GetUUID on
FullString=$(blkid | tr -d '"')
UUID=$(echo ${FullString} | cut -d ' ' -f2)
echo $UUID
}
# Greps /etc/mtab and looks for user given disk (say, /dev/sdc)
# Changes the names of all mounted partitions of that disk (eg all /dev/sdcx) to their UUIDs, in compatible format with /etc/fstab
# Outputs results to stdout
if [[ $# -eq 0 ]]
then
# If no argument, I will not try to guess a default, that looks dangerous.
echo "Usage : RobustMtabCopy /dev/somedisk (will search mstab for all /dev/somediskX and replace the names by their UUIDS)"
exit 22
else
# Else, look for anything like /dev/somediskX, source the function that will make the output pretty, get the pretty form of UUID, and put it in place of the device name in the output.
grep /etc/mtab | sed -E "s|([[:digit:]])|$(GetUUID )|g"
fi
预期的输出是这样的:
UUID=SOME-UUID mount-point fs options 0 0
UUID=SOME-OTHER-UUID mount-point fs options 0 0
实际输出:
./script.sh /dev/sdc
mount-point fs options 0 0
mount-point fs options 0 0
一点点调试表明我用参数“1”调用了 GetUUID(因此 blkid 输出空字符串)。转义 \ 没有帮助。
有几个很好的建议并不完全符合我的要求:
- 我在使用 how-to-replace-a-value-with-the-output-of-a-command-in-a-text-file 时遇到了问题,因为我调用的函数使用了我在 sed 中匹配的模式,我不知道如何将它传递给我的命令;
- 我在 Whosebug 上读到一些关于使用“e”标志从 sed 中调用 shell 命令的内容;它对我来说不太适用,因为整个模式都被替换了,我只想替换匹配项(虽然我不能把手放在参考上)。
如有任何帮助,我们将不胜感激。
使用 GNU sed 作为替换命令的 e
修饰符:
grep "" /etc/mtab |
sed -E 's|(.*)('""')(.*)|printf "UUID=%s %s%s\n" "$(blkid -s UUID -o value "")" "" ""|e'
但请注意:您必须传递与完整设备名称完全匹配的正则表达式,不能多也不能少。示例:
$ ./script.sh '/dev/sdc[0-9]*'
UUID=4071fbd0-711a-477d-877a-ee4b6be261fc /tmp ext4 rw,relatime 0 0
UUID=a34227b0-bb5e-44fb-9207-dc48cf4be022 /home ext4 rw,relatime 0 0
UUID=bcfa9073-79ad-43ca-ba34-ceb8aecb23bf /var ext4 rw,relatime 0 0
如果像您的示例一样,您已经知道您拥有的设备类型 (/dev/sd[a-z][0-9]+
) 并且只想传递前导词干,您可以调整 sed 脚本:
grep "" /etc/mtab |
sed -E 's|(.*)('""'\S+)(.*)|printf "UUID=%s %s%s\n" "$(blkid -s UUID -o value "")" "" ""|e'
然后:
$ ./script.sh '/dev/sdc'
UUID=4071fbd0-711a-477d-877a-ee4b6be261fc /tmp ext4 rw,relatime 0 0
UUID=a34227b0-bb5e-44fb-9207-dc48cf4be022 /home ext4 rw,relatime 0 0
UUID=bcfa9073-79ad-43ca-ba34-ceb8aecb23bf /var ext4 rw,relatime 0 0