且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何让用户从数组中选择一个值并将其传递给KSH中的变量

更新时间:2022-12-12 14:15:37

如果您不介意列表中的完整文件名,则可以使用.

If you don't mind the full filenames in the list then just this will work.

select JAR in *.jar; do
        export NEW_PATCHID=$JAR
        echo $NEW_PATCHID
        REPLY=''
        break
done

如果您确实介意(示例表明您可能)并且想要删除.jar(我假设这就是cut的含义,尽管cut -d. -f1似乎比awk -F . '{print $1}'更为简单)尽管时间更长),但以下内容可以安全地工作.

If you do mind (and the example indicates you might) and you want to drop .jar (I'm assuming that's what that cut is for though cut -d. -f1 would seem simpler for that as would awk -F . '{print $1}' though that is longer) the following works safely.

jars=(*.jar)
select JAR in "${jars[@]%.jar}"; do
        export NEW_PATCHID=$JAR
        echo $NEW_PATCHID
        REPLY=''
        break
done