Being a developer often requires that you dip into the command line. Whether you are setting up configuration files, starting/stopping servers, or running build tools, you’ll find you repeat yourself, time and time again. Shell scripting is a way to automate command-line tasks, saving you time to pet your favorite cat or play ping pong. To automate repetitive tasks, it’s essential to learn basic shell scripting.
Recently, I installed Nginx on top of Apache to serve up local web servers spun up by some of our build tools. Nginx now sits on top of port 80 and acts as a proxy to various other servers, including Apache (running on another port). This requires adding configuration settings in multiple locations. This repetitive task drove me to write this shell script.
Writing the shell script reminded me how useful they can be, and I learned some new tricks along the way. I wanted to share the basics to get started with shell scripting and tools you can use to make a powerful task automator.
Getting Started
Starting off, you’ll want to include a shebang (#!
), followed by the path to the interpreter, like bash (/bin/bash
).
#!/bin/bash
The shebang plus interpreter instructs your script to be run with the specified shell, /bin/bash in this case. Once you’re set up, you’re ready to get automating. There are lots of common commands you’ll want to utilize.
Printing messages
echo "Luke, I am your father..."
Print without a newline:
printf "Enter your name:"
Variables
Variables must be declared without a dollar sign.
Declaring variables, no dollar sign:
FOO="bar"
MYDIR=/Users/patrick
After the variable is declared, you extract the value from the variable by preceding the variable with a dollar sign.
echo $FOO
echo $MYDIR
Notes:
Variables are case sensitive.
Prefix your variables, and be careful not to name your variables names like PATH. This will overwrite the system variable PATH for your script, and you may see errors. You’ll want to prefix your variables with something unique (e.g.
APP_
, orFOO_
) to prevent this.Avoid using “export” in your scripts. This can cause harmful system-wide changes.
Accepting Input
printf "Enter your name:"
read name
echo $name
If/Else Statement
if [ $foo = $bar ]; then
echo "Foo is equal to bar!"
else
echo "Foo is not bar!"
fi
Notes:
Shell if ends with
fi
(backwards spelling of if).If you put “then” on the same line as an “if,” then you need to put a ; (semi-colon) after the condition.
Use
if [ -e $FILE ]
to test if a FILE existsUse
if [ -d $FILE ]
to test if a FILE exists and is a directoryUse
if [ -h $FILE ]
to test if a FILE exists and is a symbolic link
Including
I find it helpful to separate configurations into other files.
source "inc/config.conf"
This will include the shell script or config file, etc., as if it were written at that point in your shell script.
Functions
Functions provide a way to group commands into a simple package of repetitiveness. Just like in any programming language, you can create shell script functions. One rule about functions is they must be declared (written) before they are used.
Shell functions are written as:
function func_name() {
#code block
}
The keyword, function, is optional. In the code block, you can put in any shell commands.
Arguments in functions are not declared in the function definition. You can simply pass arguments in as need be. Each argument passed in will be defined numerically as $1, $2, etc., within the function scope.
func_name() {
echo $1
echo $2
}
func_name "Hello" "World"
This will work and print “Hello World”.
End
Shell scripting gives you the ability to automate commands and repetitive tasks. Armed with the knowledge of shell scripting, you’ll be hunting down repetitive tasks in your life and scripting them out of existence. Learning to script your shell is the way to make it happen—don’t be afraid to give it a shot. There’s a lot more information that we could cover here, but here are some decent tutorials you can follow up on: