如何在帶有選單對話框的 shell 腳本中執行 shell 腳本?

如何在帶有選單對話框的 shell 腳本中執行 shell 腳本?

我想這是我未完成的腳本,我的目標是使它列出我的腳本,並且我希望能夠運行列出的選定的 .sh 檔案。我在這裡先向您的幫助表示感謝。我嘗試過製作( optionone= 1 ),因為標籤“1”可能會被識別為列出的 .sh 檔案的名稱?所以在我的結果區域我嘗試了 result=$(./${optionone})

我不想輸入文件名來運行它,我試圖讓它變得簡單,就像一個控制面板,我只需在文件名上單擊輸入,它就會為我運行。

`#!/bin/bash
let i=0 # define counting variable
W=() # define working array
while read -r line; do # process file by file
    let i=$i+1
    W+=($i "$line")
done < <( ls -1 /home/peder/Desktop/scripts )
FILE=$(dialog --title "List of scripts" --menu "Chose one" 24 80 17 "${W[@]}" 3>&2 2>&1 1>&3) # show dialog and store output
clear
optionone= 1
 case $FILE in
    0 )
      clear
      echo "Program terminated."
      ;;
    1 )
      result=$(./${optionone})
      display_result "Scripts"
      ;;
    2 )
      result=$(C^)

      ;;
  esac

  done

` 圖一:

我的程式碼

圖二:

執行的樣子

答案1

介紹

當所有文件都位於自己的目錄中時,以下一組文件對我有用。您可能想讓您的選單系統更加通用。

選單

#!/bin/bash

# this simplified version works with files in its own directory

i=0 # define counting variable
wa=() # define working array

while read -r line; do # process file by file
    let i=$i+1
    wa+=($i "$line")
done < scripts

result=$(dialog --title "List of scripts" --menu "Choose a script from the list" 24 80 17 "${wa[@]}" \
 3>&2 2>&1 1>&3)      # show dialog menu

#clear

if [ "$result" == "" ]
then
 echo "Quit"
else
 item=$(($result*2-1))
 #  test output (to be removed later on)
 echo "$item"
 echo "${wa[$item]}"
 read -p "Press Enter to continue or ctrl C to quit"
 # end of test output
 "${wa[$item]}"       # execute selected item
fi

腳本

./test0
./test1

測試0

#!/bin/bash

echo $0 start
echo $0 end

測試1

#!/bin/bash

echo $0 start
echo $0 end

相關內容