gpt4 book ai didi

linux - 带有空格参数的 Bash 脚本字符串不起作用

转载 作者:行者123 更新时间:2023-12-04 19:05:08 25 4
gpt4 key购买 nike

带有空格的 Bash 参数不起作用,即使我在参数中使用引号

update_config_entry() {
echo
echo -e "${RED}CONFIG-UPDATE${NC} Ensure $1 is updated in $2"

if grep -Fx $1 $2; then
echo "$1 already exist in file $2"
else
echo $1 >> $2
fi

policystatus=$?

if [[ "$policystatus" -eq 0 ]]; then
echo -e "${GREEN}Remediated:${NC} Ensure $1 is updated in $2"
else
echo -e "${RED}UnableToRemediate:${NC} Ensure $1 is updated in $2"
fi

}

update_config_entry "install usb-storage /bin/true" /etc/modprobe.d/CIS.conf
错误:
grep: usb-storage: No such file or directory
grep: /bin/true": No such file or directory
“安装 usb-storage/bin/true”应该是单个参数,但它将 install usb-storage/bin/true 作为单独的参数传递。我做错了什么,
其他地方推荐用引号括起来,我已经在做

Link
bash 脚本
GNU bash,版本 4.4.20(1)-release (x86_64-pc-linux-gnu)

最佳答案

您忘记引用 grep 的参数.它应该是

grep -Fx "$1" "$2"
否则 grep 收到
grep -Fx install usb-storage /bin/true /etc/modprobe.d/CIS.conf 
这意味着它被要求搜索文件usb-storage。

关于linux - 带有空格参数的 Bash 脚本字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72023407/

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