2/7/2019 0 Comments Best Bash Script For MacThis is the first of a series of articles that are designed to ease people into using bash and writing bash scripts. This article will give you a quick overview of some of the commands you will need to use to execute every bash script, and are commands that you should keep close to your heart for the future. If you haven’t already checked out our, it’s a good place to start before undertaking this tutorial. Requirements • The Mac OS X Terminal or a Command Prompt 1. The best answers are voted up and rise to the top. Unanswered; how can I have my bash script detect mac filtering. MAC - edit hosts file in bash script-1. Echoing a phrase Before we write a script we need to learn the command for printing text to the bash shell. In bash this is a very simple command called echo. To test this out you have to open up the terminal application found at Applications/Terminal.app and type in: echo 'Hello World' Now we want to greet our friends in the world multiple times and we don’t want to have to type this command each time we say hello. So we are going to write a script. Creating a new script Creating a bash script is easy. You just have to open up your favorite text editor (or TextEdit on every Mac), and create a new plain text file. If you are using TextEdit go into preferences and under the format options choose plain text. It’s less funky that way. Now you have to click save as (File->Save As) and save the file as hello.sh in your documents folder. Writing the code Now that we have created our script we can fill in the code we need to actually print out hello world in bash. To start we need one very important piece that all bash scripts start off with, the shebang. This specifies what shell we are going to be executing our script with. Since we are using bash, at the top of every script you write should be: #!/bin/bash If you are writing other scripts for other shells you could have: #!/bin/csh or #!/usr/bin/perl for the C and Perl shells respectively. Below this shebang comes the actual meat of our script. For today, the meat isn’t going to be as meaty as it will be later; future articles will explain how to write more detailed and complicated scripts. For now, your second line should read: echo 'Hello World' Now save your script and get ready to head back into Terminal. Running the Script To run the script we need to hop back into the terminal application (found at Applications/Terminal.app). Before we can run our pretty little script we need to compile it to make it executable. To do this we need to point Terminal to the folder holding the hello.sh file via a command called cd. It’s a simple command that goes like: cd /path/to/file If you saved the hello.sh file to your “documents” folder you would type: cd /Users/Your_user_name/Documents Next, we need to compile the script.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |