Bash Scripts For Mac



If you are familiar with bash, but not familiar with the Mac OS X environment, then keep reading, this blog post is for you.

If you are using your Mac to do some serious software development, or if you would like to use your Mac to perform several IT administration tasks, you might want to automatise some of the tasks using scripts such as bash scripts.

Mac run shell script

Bash scripting is a popular option because of portability. Bash is available on all Unix and Linux Platforms and it was recently added to the Windows.

One thing that can be frustrating coming from Linux to a Mac is that there is no /proc pseudo-file system. This has broken a number of my Linux scripts, and it can be tricky to find suitable ways to get the same information you find in proc into your Mac bash script. The reason it doesn't work after downloading is the file permissions don't allow it. To enable execute permissions, open Terminal and type chmod 755 /path/to/script. Instead of typing the full path, you can drag the script onto the Terminal window from Finder. Then, to execute, just enter /path/to/script. Again, you can drag and drop the file.

This short text is not about scripting, it is about the script file, and the advise about where it could be stored, and any relevant changes to the environment that would be required for running the script.

Let’s say you have the following script stored in a myscript.sh file:

The file containing the script usually have the following:

which indicates the script has read and write permissions “rw” for the file owner, read “r” permission for users in the “staff” group user read permission for all “other” users.

As you already know you need first to make the file executable. Let’s give it executable permission for the owner of the script

Now running ./myscript.sh will run the script:

But the question is now, where do we put the script and what do we have to change to be able to run the scripts from anywhere.

Option 1: /usr/local/bin/

The first option is to move your scripts to the /usr/local/bin/ folder that might already contain other scripts and executables. Using this option will allow other users on the device to access the script as well depending on their permissions. Since the path /usr/local/bin is usually part of the $PATH variable that is loaded in the terminal environment, you will be able to run the script from everywhere as long as the path is present in the current environment $PATH variable

For

To check whether the $PATH already contains the path use the following command and check the output for the path

Option 2: $HOME/bin

In this option, we move the scripts to a special folder in the user’s own home folder $HOME/bin:

Bash Scripts For Mac

In this case, only you will have access to the script, and all your scripts will be isolated in one location, which is a good option for backup and deployment of the scripts.

Bash Script Machine Name

The folder $HOME/bin/ does not usually exist, and thus it is not usually already in $PATH variable. Therefore the folder must be created and the path must be added to the $PATH variable of your environment.

To check whether the $PATH already contains the path use the following command and check the output for the path

If the path is not already included in $PATH open the $HOME/.bash_profile for editing. At the end of the file add the following line:

Bash Script Machine Learning

Store the file. Close the terminal and re-open it again to reload the environment variables that automatically runs the $HOME/.bash_profile. Alternatively, you could reload the $HOME/.bash_profile by running:

Bash Script Mac If

That’s it basically.





Comments are closed.