我正在创建一个在我的树莓派上运行的 shell 脚本。在此过程中,我试图简化一些重复的代码,并且我想为 whiptail 函数制作一个包装器,如下所示。
function yesno() {
answer=$(whiptail --yesno "$1" 0 0 3>&1 1>&2 2>&3)
echo $answer
}
此函数不回显任何内容,我想知道我的语法是否有误。当在我的终端中输入 yesno "Your question"
时,该函数会触发。
同样回显 $?
也无济于事,因为在函数中不使用 return
时它似乎没有捕获任何数据。
当尝试使用 return
时,我收到一条错误消息,提示我需要返回一个整数。
我已经成功地在 if
语句中运行了这个函数,并且确信它确实返回了 0 或 1。
if whiptail --yesno "Are you sure you want to clear all SSH keys from the list?" 0 0; then
# Do stuff
fi
有人知道如何将我展示的 --yesno whiptail 函数的输出倒入变量吗?
也许你的意思是这样的
#!/bin/bash
function yesno() {
answer=$(whiptail --yesno "$1" 0 0 3>&1 1>&2 2>&3; echo $?)
echo "Answer <$answer>"
}
yesno "Hello world"
在子 shell 中打印 $?
(0=yes,1=no)。
我是一名优秀的程序员,十分优秀!