Session Prices

Feel free to negotiate depending on your situation.

Scheduling

Hit me up to work out a scheduled time and date.
[email protected]
213-995-7078 text or voice
Introduction To Bash Syntax, Functions, And Commands

Bash is just another computer language, and in this lesson, we'll learn a few things that will benefit us in the future. Things like doing if statements, loops, using arrays, and utilizing command line arguments such as --deploy or --build.

 

Command Line Arguments

The first thing we'll look at is command line arguments. Create a new file in your project directory and call it arg_test. Then add the following code into it:

#!/bin/bash
echo "Hello $0"

In your terminal run:

chmod +x arg_test
./arg_test
Hello ./arg_test

What does $0 represent? This gives us the script name, so essentially, we're just saying hello to our script name.

What if we want to allow someone to say hello and use their name? Change the $0 to $1:

#!/bin/bash
echo "Hello $1"

Then run this in your terminal:

./arg_test Jeff
Hello Jeff

If I'd like to use my full name with spaces, I need to add quotes to the command:

./arg_test "Jeff Clemmer"
Hello Jeff Clemmer

What if you need multiple arguments? Just use $2, $3, etc... In this example, change arg_test to the following:

#!/bin/bash
echo "Hello $1 and $2"

Then run:

./arg_test "Jeff Clemmer" "John Smith"
Hello Jeff Clemmer and John Smith
 

Loops

We'll add to the previous example, by utilizing a loop to churn through command line arguments. This will allow a user to specify options to our command in any order.

Let's update our arg_test script to look like the following:

#!/bin/bash
for i in ${@}; do
	echo $i
done

This will essentially dump our command line options to the terminal:

./arg_test --build --deploy
--build
--deploy

${@} creates an array out of our command line options that we can then iterate through.

You can also create arrays like the following:

servers=(a.dsql.org b.dsql.org c.dsql.org d.dsql.org e.dsql.org f.dsql.org)
for i in ${servers[@]}; do
	echo $i
done

In the previous example, array elements are seperated by spaces.

 

If Statements

If statements allow us to make decisions based on certain conditions. For example, we can make desicions on our command line arguments.

Let's respond to our --build and --deploy options.

#!/bin/bash

# setup script variables
build="no"
deploy="no"

# loop through command line options
for i in ${@}; do
	if [[ $i == "--build" ]]; then
		build="yes"
	fi
	if [[ $i == "--deploy" ]]; then
		deploy="yes"
	fi
done

# decide what actions to take
# we add these options here so that we always do steps in the right order
if [[ $build == "yes" ]]; then
	echo "We shall build the codebase."
fi

if [[ $deploy == "yes" ]]; then
	echo "Soon, I will deploy the code to the remote server."
fi

If we run this script with various options, we'll get:

./arg_test --build
We shall build the codebase.
./arg_test --deploy
Soon, I will deploy the code to the remote server.
./arg_test --build --deploy
We shall build the codebase.
Soon, I will deploy the code to the remote server.

And if we reverse the options, we still get the same result:

./arg_test --deploy --build
We shall build the codebase.
Soon, I will deploy the code to the remote server.
 

Offering The User Some Help

We can detect if the user hasn't added any command line options by checking if ${#@} equals 0. ${#@} gives us the length of the array.

#!/bin/bash

# if the user doesn't specify any command line options, offer them some help
if [[ ${#@} == 0 ]]; then
	echo "To use this command, specify the following parameters:"
	echo "--build - builds the codebase"
	echo "--deploy - deploys this codebase to the production server"
	echo ""
	echo "./arg_test --build --deploy"
	exit
fi

# setup script variables
build="no"
deploy="no"

# loop through command line options
for i in ${@}; do
	if [[ $i == "--build" ]]; then
		build="yes"
	fi
	if [[ $i == "--deploy" ]]; then
		deploy="yes"
	fi
done

# decide what actions to take
# we add these options here so that we always do steps in the right order
if [[ $build == "yes" ]]; then
	echo "We shall build the codebase."
fi

if [[ $deploy == "yes" ]]; then
	echo "Soon, I will deploy the code to the remote server."
fi
 

A More Careful Look At Variables

Variables in Bash are a little different than in other languages.

Just a couple of rules to follow:

  • When assigning variables, don't use a dollar sign at the beginning.
  • When assigning variables, you can't use spaces between the equal sign.
  • When using variables, you must use a dollar sign.

When assigning variables, follow this example:

build="no"

Notice there is no dollar sign. Also notice there are no spaces between build and "no".

When using variables, use them like this:

echo $build
echo "shall we build? $build"
 

A Closer Look At If Statements

If statements can have more conditions, just like other languages, such as else.

if [[ $deploy == "yes" ]]; then
	echo "we are definitely deploying"
elif [[ $deploy == "maybe" ]]; then
	echo "maybe we'll deploy, maybe we won't"
else
	echo "we're definitely not deploying"
fi

The elif statement allows you to specify more if conditions if previous conditions were not met.

Comparing numbers needs a small change. We change square brackets [[]] to parentheses (()).

Consider this example:

build_number=10
if (($build_number >= 10)); then
	echo "Milestone accomplished!"
fi
 

Capturing Command Output Into A Bash Variable

Sometimes you need to run a system command and capture the output of the command into a Bash variable.

If you run date +%y.%m.%d from your terminal, you'll get the output:

date +%y.%m.%d
21.01.27

To capture output from a command you use $(). If you wrapped date +%y.%m.%d in $(), it would look like:

date=$(date +%y.%m.%d)
echo $date
 

Code Comments

Code comments start with #. Anything after that will be a comment.