linux - 在 shell 脚本中使用 sed 尝试替换包含正斜杠的文本时出错

标签 linux bash shell raspberry-pi3

<分区>

我在 rpi 3 上运行我的 shell 脚本。但是我得到了以下错误。

script.sh: 51: script.sh: Syntax error: "(" unexpected

我的脚本行如下。

51: lines_to_be_removed=("allow-hotplug wlan0" "iface wlan0 inet manual" "wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf")

52: remove_lines(){
53:    for i in "${lines_to_be_removed[@]}"
54:    do
55:        sudo sed -i "/$i/,1 d" /etc/network/interfaces
56:    done
57: }

最佳答案

不确定这是否会解决问题,但它可能有助于简化和精简解决方案。尝试调用 sed 一次:

remove_lines() {
    sed -i '
        /allow-hotplug wlan0/d
        /iface wlan0 inet manual/d
        /wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf/d
    ' /etc/network/interfaces
}

关于linux - 在 shell 脚本中使用 sed 尝试替换包含正斜杠的文本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47741687/

相关文章:

bash - 如何设置 Airflow 的电子邮件配置以发送有关错误的电子邮件?

linux - 解压目录中的所有文件

linux - 如何可靠地跟踪 POSIX 系统上的子/孙进程?

linux - 有没有办法使用 uniq 只比较前 n 个字符?

android - 为android编译c程序

mysql - bash/mysql 更新行计数中的字符串提取

linux - 使用 Unix Shell 验证日期的一致性

json - shell脚本从一个文件中读取值并将其替换为另一个文件

python - 在 Python 中将 RAR 文件的内容读入内存

linux - 如何提高 Postgresql 'truncate' 性能?