gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:45:03 26 4
gpt4 key购买 nike

我目前正在从外部 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/

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