Shell 用于检查 dir 目录是否存在然后更改路径的脚本,如果不存在则使用该名称创建 dir 并检查文件名是否不存在

Shell script to check dir directory if it exists then change the path, if not then create dir with that name and also check for file name not exists

如何编写 Shell 脚本来检查目录,如果存在则更改路径,如果不存在则创建具有该名称的目录?(使用 nano 编辑器)

要检查目录是否存在,您可以使用以下测试:

[ ! -d "$DIRNAME" ]

完整脚本:

if [ ! -d "${DIRNAME}" ]; then
   mkdir ${DIRNAME}
fi
cd ${DIRNAME}

可以创建另一个解决方案,但是带有 -p 选项的目录如果存在则不会 return 错误:

mkdir -p ${DIRNAME}
cd ${DIRNAME}