Bash Scripting For Dockerfile and Bamboo
Bash Scripting For Dockerfile and Bamboo
● References:
○ https://www.freecodecamp.org/news/bash-scripting-tutorial-linux-shell-script-and-
command-line-for-beginners/
○ https://github.com/trinib/Linux-Bash-Commands?tab=readme-ov-file#-s
○ https://likegeeks.com/linux-bash-scripting-awesome-guide-part3/
○ https://likegeeks.com/regex-tutorial-linux/
○ https://github.com/trinib/Linux-Bash-Commands?tab=readme-ov-file#-s
○ https://tldp.org/LDP/abs/html/options.html
○ https://phoenixnap.com/kb/bash-commands
○ https://www.educative.io/blog/bash-shell-command-cheat-sheet
○ https://www.freecodecamp.org/news/linux-chmod-chown-change-file-
permissions/
○ https://phoenixnap.com/kb/bash-commands
○ https://ss64.com/bash/
○ https://www.hostinger.ph/tutorials/linux-commands
● In Linux, process automation relies heavily on shell scripting. This involves creating
a file containing a series of commands that can be executed together.
● A bash script is a file containing a sequence of commands that are executed by the
bash program line by line. It allows you to perform a series of actions, such as navigating
to a specific directory, creating a folder, and launching a process using the command
line.
● By saving these commands in a script, you can repeat the same sequence of steps
multiple times and execute them by running the script.
● Bash scripting is a powerful and versatile tool for automating system administration
tasks, managing system resources, and performing other routine tasks in Unix/Linux
systems. Some advantages of shell scripting are:
○ Automation: Shell scripts allow you to automate repetitive tasks and processes,
saving time and reducing the risk of errors that can occur with manual execution.
○ Portability: Shell scripts can be run on various platforms and operating systems,
including Unix, Linux, macOS, and even Windows through the use of emulators
or virtual machines.
○ Flexibility: Shell scripts are highly customizable and can be easily modified to
suit specific requirements. They can also be combined with other programming
languages or utilities to create more powerful scripts.
○ Accessibility: Shell scripts are easy to write and don't require any special tools
or software. They can be edited using any text editor, and most operating
systems have a built-in shell interpreter.
○ Integration: Shell scripts can be integrated with other tools and applications,
such as databases, web servers, and cloud services, allowing for more complex
automation and system management tasks.
○ Debugging: Shell scripts are easy to debug, and most shells have built-in
debugging and error-reporting tools that can help identify and fix issues quickly.
● The terms "shell" and "bash" are used interchangeably. But there is a subtle difference
between the two.
● "Shell" is a broad term that refers to any program that provides a command-line
interface, "Bash" is a specific type of shell that is widely used in Unix/Linux systems.
● By naming convention, bash scripts end with .sh. However, bash scripts can run
perfectly fine without the sh extension.
● Bash scripts start with a shebang. Shebang is a combination of bash # and bang !
followed by the bash shell path. This is the first line of the script. Shebang tells the shell
to execute it via bash shell. Shebang is simply an absolute path to the bash
interpreter.
● Our first script prompts the user to enter a path. In return, its contents will be listed.
● Create a file named run_all.sh using the vi command. You can use any editor of your
choice.
● Line #1: The shebang (#!/bin/bash) points toward the bash shell path.
● Line #2: The echo command is displaying the current date and time on the terminal.
Note that the date is in backticks.
● Line #5: The read command reads the input and stores it in the variable the_path.
● line #8: The ls command takes the variable with the stored path and displays the
current files and folders.
● To make the script executable, assign execution rights to your user using this command:
● +x adds the execution rights to the current user. This means that the user who is the
owner can now run the script.
● You can run the script using any of the mentioned methods:
○ sh run_all.sh
○ bash run_all.sh
○ ./run_all.sh
H. Standard Options
● When you start your Linux bash scripting, you are free to choose which letter is suitable
for your option.
● Sometimes you need data from the user while the bash scripting is running.
● The bash shell uses the read command for this purpose.
● The read command reads input from standard input (the keyboard) or a file
descriptor and stores it in a variable:
● We use the -n option to disable the newline so that you can type your text in the same
line.
● If you don’t specify variables for the read command, it will save all incoming inputs in the
REPLY variable.
Figure 2.2 - Read Command 5
● You can use the -t option to specify a timeout for input in seconds.
● In Linux bash scripting, sometimes, you don’t want the user input to be displayed on the
screen, like entering a password.
1. Reading the user input and storing it in a variable. We can read the user input using
the read command.
2. Reading from a file. This code reads each line from a file named input.txt and prints it
to the terminal. We'll study while loops later in this article.
Figure 2.7 - Read Command 8
3. Command line arguments. In a bash script or function, $1 denotes the initial argument
passed, $2 denotes the second argument passed, and so forth. This script takes a name
as a command-line argument and prints a personalized greeting.
2. Writing to a file:
○ This writes the text "This is some text." to a file named output.txt. Note that the >
operator overwrites a file if it already has some content.
3. Appending to a file:
○ This appends the text "More text." to the end of the file output.txt.
Figure 3.3 - Appending to a File
4. Redirecting output:
○ This lists the files in the current directory and writes the output to a file
named files.txt. You can redirect output of any command to a file this way.
● Variables let you store data. You can use variables to read, access, and manipulate data
throughout your script.
● There are no data types in Bash. In Bash, a variable is capable of storing numeric
values, individual characters, or strings of characters.
● In Bash, you can use and set the variable values in the following ways:
2. Assign the value based on the output obtained from a program or command, using
command substitution. Note that $ is required to access an existing variable's value.
K. Bash commands
● Expressions that produce a boolean result, either true or false, are called conditions.
There are several ways to evaluate conditions, including if, if-else, if-elif-else, and
nested conditionals.
Figure 3.8 - Conditional Statement Syntax
● We can use logical operators such as AND -a and OR -o to make comparisons that
have more significance.
● This statement checks if both conditions are true: a is greater than 60 AND b is less
than 100.
Figure 4.0 - If-elif-else Condition Sample
● While loops check for a condition and loop until the condition remains true. We need to
provide a counter statement that increments the counter to control loop execution.
● In the example below, (( i += 1 )) is the counter statement that increments the value of i.
The loop will run exactly 10 times.
Figure 4.1 - While Loop Example
● The for loop, just like the while loop, allows you to execute statements a specific number
of times. Each loop differs in its syntax and usage.
Figure 4.3 - For Loop Example
● In Bash, case statements are used to compare a given value against a list of patterns
and execute a block of code based on the first pattern that matches. The syntax for a
case statement in Bash is as follows:
Figure 4.5 - Case Statement Syntax
● Here, "expression" is the value that we want to compare, and "pattern1", "pattern2",
"pattern3", and so on are the patterns that we want to compare it against.
● The double semicolon ";;" separates each block of code to execute for each pattern.
The asterisk "*" represents the default case, which executes if none of the specified
patterns match the expression.
Figure 4.6 - Case Statement Example
N. Schedule Scripts using Cron
● Cron is a powerful utility for job scheduling that is available in Unix-like operating
systems. By configuring cron, you can set up automated jobs to run on a daily, weekly,
monthly, or specific time basis. The automation capabilities provided by cron play a
crucial role in Linux system administration.
● The crontab utility is used to add and edit the cron jobs.
● You can add and edit the cron through crontab -e.
● Reference: https://www.freecodecamp.org/news/cron-jobs-in-linux/
O. Debug and Troubleshoot Bash Scripts
● One of the most useful techniques for debugging Bash scripts is to set the set -x option
at the beginning of the script. This option enables debugging mode, which causes
Bash to print each command that it executes to the terminal, preceded by a + sign. This
can be incredibly helpful in identifying where errors are occurring in your script.
● When Bash encounters an error, it sets an exit code that indicates the nature of the
error. You can check the exit code of the most recent command using the $?
variable. A value of 0 indicates success, while any other value indicates an error.
● Another useful technique for debugging Bash scripts is to insert echo statements
throughout your code. This can help you identify where errors are occurring and what
values are being passed to variables.
● If you want your script to exit immediately when any command in the script fails,
you can use the set -e option. This option will cause Bash to exit with an error if any
command in the script fails, making it easier to identify and fix errors in your script.
● We can troubleshoot crons using the log files. Logs are maintained for all the
scheduled jobs. You can check and verify in logs if a specific job ran as intended or not.
For Ubuntu/Debian, you can find cron logs at:
P. Regex
● References:
○ https://www.freecodecamp.org/news/practical-regex-guide-with-real-life-
examples/
○ https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-
Command.html
○ https://likegeeks.com/regex-tutorial-linux/
○ https://www.softpost.org/basic-linux-tutorial/difference-between-grep-sed-awk-tr-
and-cut-commands
● The s command (as in substitute) is probably the most important in sed and has a lot of
different options. The syntax of the s command is ‘s/regex/replacement/flags’.