如何在 Mac OS 的 Shell 脚本中编写 if else 语句?

How do you write if else statements within a Shellscript for Mac OS?

我的目标很简单。我正在尝试编写一个包含 if else 语句的 MacOs Shell 脚本。具体来说,如果我的桌面上存在名为“newFolder”的文件,我希望脚本创建一个名为“targets”的新文件。其次,如果桌面上存在名为“ifolder”的文件夹,我希望脚本创建一个名为“days”的新文件夹,如果不存在,则创建一个名为“reactProjects”的文件夹。

这是我试过的方法:

#!/bin/bash

cd ..
cd ..
cd desktop

if exist newFolder mkdir targets

if exist targets mkdir days else mkdir reactProjects

但是当我通过终端尝试 运行 时出现错误。错误状态:

ifExample.sh: line 10: syntax error: unexpected end of file

这是我的第一个脚本shell,所以我可能有一些语法错误,但如果你明白如何做到这一点,请告诉我。

谢谢!

你应该在 bash 中了解更多关于条件的知识,所以我鼓励你阅读 this article

但在此之前,您的解决方案应如下所示:

#!/bin/bash

cd ..
cd ..
cd desktop

if [ -f newFolder ]; then
   mkdir targets
fi

if [ -d ifolder ]; then
   mkdir days
else
   mkdir reactProjects
fi