我试图将目录中的文件用作bash脚本中的选项.用户应该能够选择一个,然后将所选文件的名称传递给变量.到目前为止,我可以获取文件列表,但经过几个小时的尝试后,我无法弄清楚如何将它们显示为选项.
#!/bin/bash
prompt="Please select a file:"
options=( $(find -maxdepth 1 -print0 | xargs -0) )
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
for i in "${options[@]}"
do
$i' ) echo "You picked $opt which is file $REPLY";;'
done
$(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢.谢谢!
cho*_*oba 25
我不认为case这里适合:
#!/bin/bash
prompt="Please select a file:"
options=( $(find -maxdepth 1 -print0 | xargs -0) )
PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
exit
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
echo "You picked $opt which is file $REPLY"
break
else
echo "Invalid option. Try another one."
fi
done
ls -ld $opt
Run Code Online (Sandbox Code Playgroud)