0% found this document useful (0 votes)
22 views32 pages

Bash Scripting For Dockerfile and Bamboo

The document provides a comprehensive guide on Bash scripting, detailing its definition, advantages, and how to create and execute scripts. It covers essential commands, input/output handling, variables, conditional statements, loops, and debugging techniques. Additionally, it includes references for further reading and insights into using cron for scheduling scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views32 pages

Bash Scripting For Dockerfile and Bamboo

The document provides a comprehensive guide on Bash scripting, detailing its definition, advantages, and how to create and execute scripts. It covers essential commands, input/output handling, variables, conditional statements, loops, and debugging techniques. Additionally, it includes references for further reading and insights into using cron for scheduling scripts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

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. Definition of Bash scripting

● 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.

B. Advantages of Bash scripting

● 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.

C. Overview of Bash Shell and Command Line Interface

● 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.

D. How to Get Started with Bash Scripting

● Generally, commands follow this syntax:

Figure 1.0 - Bash Command Syntax


● You can always refer to a commands manual with the man command.

● Ex. echo - Prints a string of text, or value of a variable to the terminal.

Figure 1.1 - Echo Command

E. How to Create and Execute Bash scripts

E.1. Script Naming Conventions

● By naming convention, bash scripts end with .sh. However, bash scripts can run
perfectly fine without the sh extension.

E.2. Adding the Shebang

● 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.

Figure 1.2 - Shebang Statement


● You can find your bash shell path (which may vary from the above) using the command:

Figure 1.3 - Own Bash Shell Path

F. Creating our first bash script

● 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.

Figure 1.4 - Vi Command


Figure 1.5 - Script to print contents of a user supplied directory

● 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 #4: We want the user to enter a valid path.

● 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.

G. Executing the Bash Script

● To make the script executable, assign execution rights to your user using this command:

Figure 1.6 - Chmod Command

● chmod modifies the ownership of a file for the current user: u.

● +x adds the execution rights to the current user. This means that the user who is the
owner can now run the script.

● run_all.sh is the file we wish to run.

● 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.

● However, some letters are commonly used in Linux programs.

● And here is the list of the common options:

Figure 1.7 - Common Options 1


Figure 1.8 - Common Options 2
I. Input and output in Bash scripts

● 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:

Figure 1.9 - Read Command

● We use the -n option to disable the newline so that you can type your text in the same
line.

Figure 1.10- Read Command 2


● You can specify multiple inputs like this:

Figure 2.0 - Read Command 3

Figure 2.1 - Read Command 4

● 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.

Figure 2.3 - Read Command 6


I.1. Reading Password

● In Linux bash scripting, sometimes, you don’t want the user input to be displayed on the
screen, like entering a password.

● The -s option suppresses the output from appearing on the screen.

Figure 2.4 - Suppress Output

Figure 2.5 - Suppress Output 2


I.2. Gathering Input

1. Reading the user input and storing it in a variable. We can read the user input using
the read command.

Figure 2.6 - Read Command 7

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.

Figure 2.8 - $1 Initial Argument

Figure 2.9 - Code Script for greetings.sh

Figure 3.0 - Output


I.3. Displaying Output

1. Printing to the terminal:

Figure 3.1 - Print to Terminal

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.

Figure 3.2 - Writing to a File

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.

Figure 3.4 - Redirecting Output


J. Variables and data types in Bash

● 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:

1. Assign the value directly.

Figure 3.5 - Variable in Bash

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.

Figure 3.6 - Value of Country to the New Variable same_country

3. To access the variable value, append $ to the variable name.


Figure 3.7 - Assigning and Printing Variable Values

K. Bash commands

1. cd: Change the directory to a different location.

2. ls: List the contents of the current directory.

3. mkdir: Create a new directory.


4. touch: Create a new file.

5. rm: Remove a file or directory.

6. cp: Copy a file or directory.

7. mv: Move or rename a file or directory.

8. echo: Print text to the terminal.

9. cat: Concatenate and print the contents of a file.

10. grep: Search for a pattern in a file.

11. chmod: Change the permissions of a file or directory.

12. sudo: Run a command with administrative privileges.

13. df: Display the amount of disk space available.

14. history: Show a list of previously executed commands.

15. ps: Display information about running processes.

L. Conditional Statements (if/else)

● 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.

Figure 3.9 - AND Logical Operator

● 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

M. Looping and Branching

M.1. While loop

● 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

Figure 4.2 - While Loop Example Output

M.2. For loop

● 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

Figure 4.4 - For Loop Example Output

M.3. Case Statements

● 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.

Figure 4.7 - Cron Syntax

● The *s represent minute(s) hour(s) day(s) month(s) weekday(s), respectively.


Figure 4.8 - Cron Jobs Scheduling Example

N.1. Using Crontab

● The crontab utility is used to add and edit the cron jobs.

● crontab -l lists the already scheduled scripts for a particular user.

● 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

O.1. Set the set -x option

● 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.

Figure 4.8 - Set -x Option


O.2. Check the exit code

● 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.

Figure 4.9 - Exit Code

O.3. Use echo statements

● 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.

Figure 5.0 - Echo Statement


O.4. Use the set -e option

● 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.

Figure 5.1 - Set -e Option

O.5. Crons by verifying logs

● 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:

Figure 5.2 - Cron Logs Directory


Figure 5.3 - Cron Log Example

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

P.1. The S Command

● 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’.

You might also like