Shell脚本读取文件路径直到文件存在

标签 shell loops user-input file-exists case-statement

用户输入文件路径,shell脚本必须检查文件是否存在,如果文件不存在,则提示输入正确的路径,直到提供正确的路径。这是我的尝试,但我不喜欢它

if [[ -z $filepath || ! -f $filepath  ]]; then
    printf "\nError: File does not exist. Try again\n"

    while true :
    do
        read -p "Please provide file path: " filepath
        if [[ -z $filepath || ! -f $filepath  ]]; then
            continue
        else 
            break
        fi 
    done
fi

这是另一个由于语法错误而失败的尝试

if [[ -z $filepath || ! -f $filepath  ]]; then 
    printf "\nError: Bad file path. Try again\n" 
    while true : 
    do 
        read -p "Please enter file path: " filepath
        case $filepath in
                "")
                    echo "Bad file path. Try again"
                    continue ;;
                ! -f)
                    echo "Bad file path. Try again"
                    continue ;; 
                *)
                    break ;;
        esac
    done

最佳答案

怎么样

until [[ -n $filepath && -f $filepath ]]; do
    read -p "Please provide file path: " filepath
done

关于Shell脚本读取文件路径直到文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769456/

相关文章:

linux - Bash:如何远程运行脚本

git - 如何以编程方式确定 Git checkout 是否是标签,如果是,标签名称是什么

php - 添加到 PHP 中的循环而不覆盖

javascript - 如何使用 Javascript 简单地循环遍历数组?

Java - 使用方法indexOf作为输入

php - 如何从网页在服务器上执行脚本?

shell - 如何增加文件中的数字

postgresql - 在 Postgres 9.0+ 中使用 PL/pgSQL 循环表

c++ - 给定两个值时,输入循环循环两次

Android:带有 X 和 Y 位置的自定义 View onClickEvent