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

Time:04-11

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!

CodePudding user response:

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
  • Related