簡體   English   中英

如何在bash腳本的select語句中調用腳本

[英]How to call a script in a select statement in bash script

聽起來像是一個令人費解的問題,但實際上並非如此。 我正在忙於在bash腳本中構建菜單,作為case語句的一部分,我試圖調用我不久前創建的另一個腳本,該腳本用於編輯文件,但該腳本正在中斷。 這是一個例子:

#!/bin/bash

PS3="Please choose an option: "
options=("Do this" "Do nothing")
select opt in "${options[@]}"
do 
   case $opt in
      "Do this")
         read -p "Please enter the file name:`echo $'\nE9> '`" FILENAME
         getscript $FILENAME
         break
         ;;
      "Do nothing")
         break
         ;;
   esac
done

我從中得到的錯誤如下: getscript: command not found

為了說明這一點,我從正常的命令提示符下運行了getscript,但它仍然有效:

$ getscript randomfilename.txt
done

更多細節,以防萬一...如果有問題, getscript.sh腳本位於/usr/local/bin ,我在當前用戶目錄的.bashrc中為此創建了一個別名,它使我可以調用僅getscript

請幫忙?

Biffen是正確的,別名似乎在腳本中不起作用。 所以我要做的就是最后添加.sh,它可以正常工作...為了說明,以防萬一將來有人需要它:

PS3="Please choose an option: "
options=("Do this" "Do nothing")
select opt in "${options[@]}"
do 
   case $opt in
      "Do this")
         read -p "Please enter the file name:`echo $'\nE9> '`" FILENAME
         getscript.sh $FILENAME
         break
         ;;
      "Do nothing")
         break
         ;;
   esac
done

奇跡般有效 :)。 多謝你們

將這些行添加到~/.bashrc末尾

function getscript() {
   /usr/local/bin/getscript.sh "$@"

}
export -f getscript

在這之后

$ source ~/.bashrc

這樣可以解決問題。 如果您沒有source軟件包,則需要安裝它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM