shell脚本中的参数扩展,包括带有空字符串的路径[重复]

j0pj023g  于 2023-04-07  发布在  Shell
关注(0)|答案(1)|浏览(93)

此问题已在此处有答案

Test whether a glob has any matches in Bash(22答案)
2天前关闭。
如果workdir路径中有空间,我在第二个if语句[ -z "$(ls -A $workdir)" ]的条件下遇到问题。我试图用“$workdir”将其括起来,但不起作用。感谢您的帮助。

#!/bin/bash

workdir=${1:-$(pwd)}

if [ -d "$workdir" ]; then
    if [ -z "$(ls -A $workdir)" ]; then
        echo "Working directory is set to: $workdir"
    else
        echo "Working directory is not empty!"
        read -p "Do you want to continue? (y/n) " choice
        if [[ "$choice" == [yY] ]]; then
            echo "Continuing..."
        elif [[ "$choice" == [nN] ]]; then
            echo "Exiting."
            exit 0
        else
            echo "Invalid choice. Please enter y or n."
            exit 1
        fi
    fi
else
    echo "Creating and setting as working directory: $workdir"
    mkdir -p "$workdir"
fi

mkdir -p "${workdir}"/{code,data,docs,logs,outs,temp}
mkdir -p "${workdir}/code/utils"
mkdir -p "${workdir}/data/{raw,processed}"

我需要一种方法来包括上述代码中包含空字符串的路径,而不会引发错误。

相关问题