![变量作为命令](https://linux22.com/image/110326/%E5%8F%98%E9%87%8F%E4%BD%9C%E4%B8%BA%E5%91%BD%E4%BB%A4.png)
我想使用终端名称设置一个变量,并使用该变量以全屏方式打开终端。它会是这样的:
if [ "$DESKTOP" = "gnome" ]; then
if command_exists gnome-terminal; then
terminal=$(gnome-terminal)
fi
elif [ "$DESKTOP" = "mate" ]; then
if command_exists mate-terminal; then
terminal=$(mate-terminal)
fi
fi
$terminal --working-directory="$HOME/code/" --window --full-screen &
我该如何进行这项工作?
答案1
简单地替换$( )
为任何东西。
if [ "$DESKTOP" = "gnome" ]; then
if command_exists gnome-terminal; then
terminal=gnome-terminal
fi
elif [ "$DESKTOP" = "mate" ]; then
if command_exists mate-terminal; then
terminal=mate-terminal
fi
fi
$terminal --working-directory="$HOME/code/" --window --full-screen &
$( )
用于运行命令并将输出粘贴到变量或命令行中。
如果 foo 终端未定义,上面的代码可能无法运行,我建议
noterminal=true
if [ "$DESKTOP" = "gnome" ]; then
if command_exists gnome-terminal; then
gnome-terminal --working-directory="$HOME/code/" --window --full-screen &
noterminal=false
fi
elif [ "$DESKTOP" = "mate" ]; then
if command_exists mate-terminal; then
mate-terminal --working-directory="$HOME/code/" --window --full-screen &
noterminal=false
fi
fi
if $noterminal
then
echo unable to find terminal
## or other GUI alert system.
## or xterm as per mmmint sugestion
fi
答案2
据我所知,xterm
大多数基于 Linux 的操作系统都可用。
无论如何,所使用的终端都可以在$TERM
变量中找到。