linux - Bash:IF 和 ELSE,比较和逻辑或门?

标签 linux bash scripting

我目前正在从外部 HDD 运行 Archlinux,因此我正在尝试在 bash 中制作一个脚本,这将允许我更改视频驱动程序,而无需每次从另一台计算机启动时浏览配置文件。

我在台式电脑上使用 nvidianouveau,但在我女朋友的笔记本电脑上使用 intel。因此,我一直在用 SED 制作一个 bash 脚本,它将当前使用的驱动程序输出到一个名为 $VCARD 的变量,而我想要更改的那个将是脚本的第一个参数($1,然后)。

我的问题是,我试图确保它安全,这样我就不会不小心将包含 nvidia、nouveau 或 intel 的字符串换成这三个字符串以外的任何字符串。我认为实现这一点的最佳方法是进行一系列 if-else 比较,如下所示。

if [ "$1" = "$VCARD" ]
then
    echo "Already using $VCARD driver."
else
    if [ "$1" = "nouveau" or "intel" or "nvidia" ]  #This is the line that concerns me.
    then
        sed -i -e "7s#\(nouveau\|intel\|nvidia\)#$1#" /etc/X11/xorg.conf.d/10-monitor.conf
        echo "New driver is now $1."
    else
        echo "You don't want to put that string there. (try nouveau, intel or nvidia)"
    fi
fi

当您想在中间放置一个逻辑或门时,条件如何工作?

PD:是的,我使用“#”作为 sed 命令的分隔符,我希望这不会对任何人造成影响。

最佳答案

还有:

if [ "$1" = "nouveau"] || [ "$1" = "intel" ] || ["$1" == "nvidia" ] 

关于linux - Bash:IF 和 ELSE,比较和逻辑或门?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997023/

相关文章:

python - 设置python调用bash脚本的环境变量

bash - 向所有进程发送 SIGTERM

linux - 我无法在终端上使用 sed 正确替换字符串

c - 如何等待子进程在时间限制内完成?

linux - 是否可以将文件从本地 windows 目录复制到远程 linux 目录?

C Sendto 重复执行时不起作用

xml - 如何在 shell 脚本中解析 rss-feeds/xml

linux - screen 无法打开您的终端 '/dev/pts/0' - 请检查

linux - 错误的端口号-- sftp ://11. 111.10.3

linux - 脚本不会自动从 root 用户切换到普通用户