如果工作目录路径中有空间,我对第二个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}"
我需要一种方法在上面的代码中包含有空字符串的路径而不会引发错误。
if [ -z `ls -A "$workdir"` ]; then