gpt4 book ai didi

linux - 如何从 Whiptail 返回值 --yesno

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:08 25 4
gpt4 key购买 nike

我正在创建一个在我的树莓派上运行的 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)。

关于linux - 如何从 Whiptail 返回值 --yesno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46101010/

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