如果Mac OS的ShellScript中其他语句,您如何编写?

发布于 2025-01-20 18:12:19 字数 541 浏览 3 评论 0原文

我的目标很简单。我正在尝试编写一个包含 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,因此我可能有一些语法错误,但如果您了解如何执行此操作,请告诉我。

谢谢!

My goal is a simple one. I am trying to write a MacOs Shell Script containing if else statements. Specifically, I want the script to create a new file called "targets" if a file named "newFolder" exists on my desktop. Secondly I want the script to create a new folder called "days" if a folder named "ifolder" exists on the desktop and if it does not exist create a folder called "reactProjects".

Here is what I've tried:

#!/bin/bash

cd ..
cd ..
cd desktop

if exist newFolder mkdir targets

if exist targets mkdir days else mkdir reactProjects

But I am having an error occur when I try run it through the terminal. The error states:

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

This is my first script shell, so I may have some syntax mistakes, but please let me know if you understand how to do this.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-01-27 18:12:19

您应该了解有关 bash 中的条件的更多信息,因此我鼓励您阅读这篇文章

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

#!/bin/bash

cd ..
cd ..
cd desktop

if [ -f newFolder ]; then
   mkdir targets
fi

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

You should know more about conditionals in bash so I encourage you to read this article.

But before that your solution should look like this:

#!/bin/bash

cd ..
cd ..
cd desktop

if [ -f newFolder ]; then
   mkdir targets
fi

if [ -d ifolder ]; then
   mkdir days
else
   mkdir reactProjects
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文