我想这是我未完成的脚本,我的目标是让它列出我的脚本,并且我希望能够运行列出的选定 .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