9f24c807f4 | ||
---|---|---|
.github/workflows | ||
hooks | ||
src | ||
LICENSE | ||
README.md |
README.md
Bash-scripts
A collection of utility Bash scripts for automating common mundane tasks.
Table of Contents
About Bash
- Scripting languages originated as extensions of command interpreters in operating systems.
- Bourne shell (sh) was the first significant shell. Bash, today's most used Unix shell, is a GNU/FSF enhancement on the Bourne shell.
- Other shells include: C shell (csh), TC shell (tcsh), Dash (dash), Korn shell (ksh), Z shell (zsh).
What's the purpose of shell scripts?
- When working on programming projects or doing administrative tasks on servers, usually several command sequences are regularly repeated. This is especially true when working with files and directories, processing text, or configuring the network. Numerous times, those command sequences exist in many forms and must be adjusted with user input, necessitating the usage of scripts.
- Scripting languages such as Bash improve our processes by including variables, if statements, loops, arrays, and functions, allowing for more sophisticated program flow.
- The actual power of shell script comes from all of the accessible Unix commands.
- Eventually, scripts get too complicated for basic languages such as Bash. At this stage, you should consider utilizing more powerful programming languages, such as Python.
- Shell scripts may also be used to "glue" together more complex Python scripts.
When should you not use Bash?
- Complex applications.
- GUI.
- Cross-platform portability.
- Calculations.
- Network programming.
Hello world
This is a simple example of a Bash script. It prints the string "Hello World" to the standard output (stdout).
#!/usr/bin/env bash
echo "Hello world"
Executing a script
To run this script, open the terminal in the directory where the script is located and type the following command:
chmod u+x filename.sh
./filename.sh
The shebang
In the first line of a script, the shebang (#!) is used to specify the interpreter to be used when the script is executed. To use the bash interpreter, the first line of a script file must specify the absolute path to the bash executable:
#!/usr/bin/env bash
The bash path in the shebang is resolved and utilized only when a script is launched directly from a terminal. If the script is launched from a shell script, the interpreter is not resolved and the script is executed using the shell interpreter.
./filename.sh
When a bash interpreter is explicitly specified to execute a script, the shebang is ignored:
bash ./filename.sh
Scripts may also be created in a variety of different "scripting languages," thus a Perl script might begin with #!/usr/bin/env perl and one in Python with #!/usr/bin/env python3.
Variables
- Assign the value: var="Test".
- Retrive the value:
x</i> or <i>
{x}. - Variables can be defined explicitly as int or array:
declare -i var # var is an int
declare -a arr # arr in an array
declare -r var2=5 # var2 is read only
- Variables can store the value of executed command:
var=$(whoami)
Command line arguments
- First argument: $1
- All command line arguments as array: $@
- Number of command line arguments: $#
- The exit status of the last executed command: $?
If statements
If statements are used to execute a block of code if a certain condition is met. Comparison of strings and ints differs. Assume that all values are strings, unless proven otherwise.
if [ $i -eq 10 ]; then echo True; fi # int comparison
if [ "$name" == "10" ]; then echo True; fi # string comparison
Integer comparison:
Operator | Description |
---|---|
-eq | equal |
-ne | not equal |
-gt | greater than |
-ge | greater than or equal to |
-lt | less than |
-le | less than or equal to |
String comparison:
Operator | Description |
---|---|
== | equal |
!= | not equal |
> | greater than |
< | less than |
-n | string is not null |
-z | string is null |
Single [] are condition tests that are compatible with the posix shell.
Bash and other shells (e.g. zsh, ksh) allow double as an enhancement to the usual []. They expand the standard possix operations with other operations. For example, instead of -o, it is possible to use || and do regex matching with =~.
If you need to perform word splitting or filename expansion, you'd use single square brackets. Assuming there is just one csv file named 'file.csv' in the current directory, the following line of code will not print True:
if [[ -f *.csv ]]; then echo True; fi
The reason for this is that the test condition checks for a file with the name '*.txt' and no globbing is performed. This line of code, on the other hand, will print True:
if [ -f *.csv ]; then echo True; fi
For loops
A for loop repeats a sequence of steps a number of times.
for number in {1..10}
do
echo "$number "
done
Arrays
An array is a variable that holds an ordered list of values. The values are separated by spaces. The following example creates an array named array and assigns the values 1, 2, 3, 4, 5 to it:
array=(1 2 3 4 5)
It is possible to create an array with specified element indices:
array=([3]='elem_a' [4]='elem_b')
To insert an elementat (e.g. 'abc') at a given index (e.g. 2) in the array, use the following syntax:
array=("${array[@]:0:2}" 'new' "${array[@]:2}")
To iterate over the elements of an array, use the following syntax:
items=('item_1' 'item_2' 'item_3' 'item_4')
for item in "${items[@]}"; do
echo "$item"
done
# => item_1
# => item_2
# => item_3
# => item_4
It is often useful to print the elements of an array on a single line. The following code will print the elements of the array on a single line:
echo "${array[*]}"
Functions
Functions are used to group a sequence of commands into a single unit. They are used to perform repetitive tasks. Functions can be called from anywhere in the script. The following example creates a function named hello_world that prints the string Hello World to the standard output (stdout):
hello_world()
{
echo "Hello World!"
}
To call the function, use the following syntax:
hello_world
The above function does not take any arguments and does not explicitly return a value. It is possible to pass any number of arguments to the function. It is also possible to return a value from the function, but only an integer from range [0,255] is allowed.
Here is a complete example of a script that defines and uses a function to sum two numbers:
#!/usr/bin/env bash
sum_two()
{
return $(($1 + $2))
}
sum_two 5 3
echo $?
Pipes
The pipe is used to pass the output of one command as input to the next:
ps -x | grep chromium
Redirections
But what if you'd want to save the results to a file? Bash has a redirect operator > that may be used to control where the output is delivered.
some_command > out.log # Redirect stdout to out.log
some_command 2> err.log # Redirect stderr to file err.log
some_command 2>&1 # Redirect stderr to stdout
some_command 1>/dev/null 2>&1 # Silence both stdout and stderr
Complete summary:
Syntax | StdOut visibility | StdErr visibility | StdOut in file | StdErr in file | existing file |
---|---|---|---|---|---|
> | no | yes | yes | no | overwrite |
>> | no | yes | yes | no | append |
2> | yes | no | no | yes | overwrite |
2>> | yes | no | no | yes | append |
&> | no | no | yes | yes | overwrite |
&>> | no | no | yes | yes | append |
tee | yes | yes | yes | no | overwrite |
tee -a | yes | yes | yes | no | append |
n.e. (*) | yes | yes | no | yes | overwrite |
n.e. (*) | yes | yes | no | yes | append |
|& tee | yes | yes | yes | yes | overwrite |
|& tee -a | yes | yes | yes | yes | append |
Formatting and linting
It is important to keep the formatting of your script as consistent as possible. Beautysh is an amazing tool that helps you to format your script. To use it, just run the following command in a directory where your scripts are located:
beautysh **/*.sh
Additionally we advise to use shellcheck for code inspection.
shellcheck **/*.sh
Available scripts
Intro
# | Description | Code |
---|---|---|
1 | Hello world in Bash. | Bash |
2 | Using if statements, determine whether a condition is true. | Bash |
3 | Example of a while loop. | Bash |
4 | Example of a for loop. | Bash |
5 | A simple script for displaying the digits of a number. | Bash |
6 | Print all of the numbers in a given interval. | Bash |
7 | Print a christmas tree. | Bash |
8 | Prompt the user for a response to a given question. | Bash |
Math
# | Description | Code |
---|---|---|
1 | Arithmetic operations. | Bash |
2 | Sum the parameters passed to the script. | Bash |
3 | Convert a number from the decimal system to binary form. | Bash |
4 | Calculate the factorial of an integer. | Bash |
5 | Is it a prime number? | Bash |
6 | Calculate the square root of a number. | Bash |
Strings
# | Description | Code |
---|---|---|
1 | Count the number of occurrences of a given character in a string. | Bash |
2 | Convert all uppercase letters in a text string to lowercase. | Bash |
3 | Convert all lowercase letters in a text string to uppercase. | Bash |
4 | Check if a string is a palindrome. | Bash |
5 | Check if two strings are anagrams. | Bash |
6 | Calculate the Hamming Distance between two strings. | Bash |
7 | Sort a given string alphabetically. | Bash |
Arrays
# | Description | Code |
---|---|---|
1 | Calculate the arithmetic mean of the given n numbers. | Bash |
2 | Find the maximum value in an array. | Bash |
3 | Find the minimum value in an array. | Bash |
4 | Remove duplicates in a given array. | Bash |
Files
# | Description | Code |
---|---|---|
1 | Count the number of files in a directory. | Bash |
2 | Create a directory. | Bash |
3 | Count the number of lines in a text file. | Bash |
4 | Get the middle line. | Bash |
5 | Remove duplicate lines from a file. | Bash |
6 | Replace left slashes with right slashes and vice versa. | Bash |
7 | Add the text to the beginning of a specified file. | Bash |
8 | Remove all lines in a specified file that contain just whitespaces. | Bash |
9 | Rename all files in a directory with a particular extension to a new extension. | Bash |
10 | Strip digits from every string found in a given file. | Bash |
System administration
# | Description | Code |
---|---|---|
1 | Basic system info. | Bash |
2 | Check the operating system. | Bash |
3 | Check if root. | Bash |
4 | Check if the apt command is available. | Bash |
5 | Check the RAM size. | Bash |
6 | Get the CPU temperature. | Bash |
7 | Get the total CPU usage. | Bash |
8 | On the local machine, block certain websites from being visited. | Bash |
9 | Create a backup of your system. Compress the files and encrypt the resulting archive. | Bash |
10 | Display processes which might be orphans. | Bash |
11 | Display processes which might be zombies. | Bash |
Programming workflow
# | Description | Code |
---|---|---|
1 | Remove carriage return from the given files. | Bash |
2 | Replace all diacritical characters in the given files. | Bash |
3 | Change all spaces in file names to underscores and convert them to lowercase. | Bash |
4 | Remove trailing whitespaces from every file in a given directory. | Bash |
5 | Beautify and format every shell script found in the current repository. | Bash |
6 | Find unused functions and classes in a Python project. | Bash |
Git
# | Description | Code |
---|---|---|
1 | Reset local repository to match the origin. | Bash |
2 | Remove the specified branch, both locally and remotely. | Bash |
3 | Count the number of lines of code in a git repository. | Bash |
4 | Squash n last commits. | Bash |
5 | Remove n last commits. | Bash |
6 | Change the date of the last commit. | Bash |
7 | Download all of a user's public github repositories. | Bash |
Utility
# | Description | Code |
---|---|---|
1 | Find your IP address. | Bash |
2 | Empty trash. | Bash |
3 | Block websites from being visited. | Bash |
4 | Extract files based on extension. | Bash |
5 | Check which programs are running on a specific port. | Bash |
6 | Convert month names to numbers and vice versa. | Bash |
7 | Alias all the scripts from a given directory. | Bash |
8 | Get a random integer number from the given range. | Bash |
9 | Generate a random password of the specified length. | Bash |
10 | Time execution of a program with the parameters supplied. | Bash |
11 | Download audio from a YouTube video or playlist. | Bash |
Refrences
- https://www.gnu.org/software/bash/manual/bash.html
- http://mywiki.wooledge.org/BashGuide
- https://wiki.bash-hackers.org/
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.