为 bash 文件的选择循环提供一个条目

为 bash 文件的选择循环提供一个条目

我怎样才能提供一个条目来提供结果,当我在终端中运行代码时,它工作得很好,代码是:

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 笔记本中运行它时,它不起作用,我尝试了(我也尝试了代码本身):

%%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 作为条目读取。

答案1

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

$ 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 :

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

相关内容