gpt4 book ai didi

Shell 脚本 - 如果目录不存在则创建目录

转载 作者:行者123 更新时间:2023-12-04 04:55:02 36 4
gpt4 key购买 nike

我想输入一个目录的名称并检查它是否存在。
如果它不存在,我想创建但我收到错误 mkdir: cannot create directory'./' File exists
我的代码说该文件存在,即使它不存在。我究竟做错了什么?

echo "Enter directory name"
read dirname

if [[ ! -d "$dirname" ]]
then
if [ -L $dirname]
then
echo "File doesn't exist. Creating now"
mkdir ./$dirname
echo "File created"
else
echo "File exists"
fi
fi

最佳答案

read -p "Enter Directory Name: " dirname
if [[ ! -d "$dirname" ]]
then
if [[ ! -L $dirname ]]
then
echo "Directory doesn't exist. Creating now"
mkdir $dirname
echo "Directory created"
else
echo "Directory exists"
fi
fi

关于Shell 脚本 - 如果目录不存在则创建目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911144/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com