首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为bash文件的select循环提供一个条目

为bash文件的select循环提供一个条目
EN

Unix & Linux用户
提问于 2020-09-18 21:45:27
回答 1查看 73关注 0票数 0

如何为选择提供一个条目来给出结果,当我在终端中运行代码时,它工作得很完美,代码是:

代码语言:javascript
复制
PS3="Enter the space shuttle to get quick information : "
 
# set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
    case $shuttle in
        columbia)
            echo "--------------"
            echo "Space Shuttle Columbia was the first spaceworthy space shuttle in NASA's orbital fleet."
            echo "--------------"
            ;;
        endeavour)
            echo "--------------"       
            echo "Space Shuttle Endeavour is one of three currently operational orbiters in the Space Shuttle." 
            echo "--------------"       
            ;;
        challenger) 
            echo "--------------"               
            echo "Space Shuttle Challenger was NASA's second Space Shuttle orbiter to be put into service."
            echo "--------------"                   
            ;;      
        discovery) 
            echo "--------------"       
            echo "Discovery became the third operational orbiter, and is now the oldest one in service."
            echo "--------------"                           
            ;;      
        atlantis)
            echo "--------------"       
            echo "Atlantis was the fourth operational shuttle built."
            echo "--------------"                           
            ;;
        enterprise)
            echo "--------------"       
            echo "Space Shuttle Enterprise was the first Space Shuttle orbiter."
            echo "--------------"                           
            ;;      
        pathfinder)
            echo "--------------"       
            echo "Space Shuttle Orbiter Pathfinder is a Space Shuttle simulator made of steel and wood."
            echo "--------------"                           
            ;;
        *)      
            echo "Error: Please try again (select 1..7)!"
            ;;      
    esac
done

但是,当我试图在jupyter笔记本上运行它时,它不起作用,我尝试了(我也尝试了代码本身):

代码语言:javascript
复制
%%bash
cd /shellfilepath
bash file.sh

cd /shellfilepath
bash file.sh | 1

%%bash
cd /shellfilepath
bash file.sh | echo "1"

%%bash
cd /shellfilepath
if bash file.sh; then echo "1"

输出停止进入选择,假设为1,shell文件应该向我显示选择1的输出,我想要的是shell文件读取1作为一个条目。

EN

回答 1

Unix & Linux用户

发布于 2020-09-27 14:49:33

您的脚本从标准输入中读取。如果希望其他进程的输出成为脚本的标准输入,则需要使用管道。

代码语言:javascript
复制
$ echo 3 | bash file.sh
1) columbia    3) challenger  5) atlantis    7) pathfinder
2) endeavour   4) discovery   6) enterprise
Enter the space shuttle to get quick information : --------------
Space Shuttle Challenger was NASA's second Space Shuttle orbiter to be put into service.
--------------
Enter the space shuttle to get quick information :

请注意,在这种情况下,您的脚本不知道输入来自文件,因此它仍然打印菜单等,就好像它正在与用户交互一样。

票数 0
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/610145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档