bash scripting
bash scripting

Bash Scripting Linux

Linux is a powerful operating system that is widely used in both personal and professional settings. One of the key features of Linux is the ability to use bash scripting, which are essentially files containing a series of commands that can be executed automatically. In this article, we will explore the basics of scripts on Linux, including how to create and run them.

To create a script on Linux, you will need a text editor. There are several text editors available for Linux, including vi, emacs, and nano. For the purposes of this article, we will use nano as it is a simple and easy-to-use text editor.

To create a new script using nano, open a terminal window and type the following command :

# nano script.sh

This will open a new file called « script.sh » in the nano text editor. You can then type in the commands that you want to include in your script. It is important to note that each command should be on a separate line.

Once you have entered all of the commands that you want to include in your script, press « CTRL + X » to exit the text editor. You will be prompted to save your changes, so press « Y » to save the file and then hit « Enter » to confirm the filename.

To run your script, you will need to make it executable. To do this, use the « chmod » command to set the executable permission for the script file :

# chmod +x script.sh

Now, you can run the script by typing the following command :

#./script.sh

This will execute all of the commands in the script, one after the other.

There are many ways to customize and enhance your scripts, including adding variables, loops, and conditional statements. You can also use various shell built-in commands and external programs to further automate tasks and processes.

Scripts are a powerful tool on Linux, and can save you a lot of time and effort by automating routine tasks and processes. Whether you are a beginner or an experienced Linux user, learning how to create and run scripts can greatly improve your productivity and efficiency. So, it is a good idea to learn about scripts and how to use them on Linux.

Simples exemples of Bash Scripting :

1- A simple script to display a message :

#!/bin/bash


echo "Hello, world!"

2- A script to display the contents of a directory :

#!/bin/bash

echo "Contents of the current directory:"
ls

3- A script to display the current date and time :

#!/bin/bash

echo "Current date and time:"
date

4- A script to perform a simple arithmetic calculation :

#!/bin/bash

result=$((2 + 2))
echo "2 + 2 = $result"

5- A script to display a message based on a command-line argument:

#!/bin/bash

if [ "$1" == "hello" ]
then
    echo "Hello, world!"
else
    echo "Invalid argument"
fi

Complex exemples of Bash Scripting :

1- Here is a bash script that you can use to free space from swap memory on Linux :

#!/bin/bash

# This script frees space from swap memory on Linux

# Check if swap is currently being used
swap_used=$(free -m | grep Swap | awk '{print $3}')
if [ "$swap_used" -gt 0 ]
then
    echo "Swap is currently in use."
else
    echo "Swap is not currently in use."
    exit
fi

# Free all used swap space
sudo swapoff -a

# Enable swap
sudo swapon -a

# Display the new swap usage
echo "Swap usage after freeing space:"
free -m | grep Swap

This script first checks if swap is currently being used by checking the output of the « free » command. If swap is not in use, the script exits. If swap is in use, the script disables and then re-enables swap using the « swapoff » and « swapon » commands, respectively. Finally, the script displays the new swap usage using the « free » command.

To use this script, save it to a file and make it executable using the « chmod » command, as described in the previous examples. Then, you can run the script by typing « ./script.sh », where « script.sh » is the name of the file containing the script.

2- Here is a bash script that you can use to find which services are taking up space in swap memory on Linux :

#!/bin/bash

# This script finds which services are taking up space in swap memory on Linux

# Get the PIDs of all processes using swap
pids=$(for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | grep [0-9])

# Loop through the PIDs and display the process name and swap usage for each
while read -r pid rest; do
    process=$(ps -o comm --no-headers -p $pid)
    swap=$(grep VmSwap $rest | awk '{ print $2 }')
    echo "$process (PID $pid) is using $swap bytes of swap."
done <<< "$pids"

This script first retrieves the PIDs of all processes that are using swap space by parsing the « status » files in the « /proc » directory and using the « awk » command to extract the relevant information. It then loops through the PIDs and uses the « ps » and « grep » commands to display the process name and swap usage for each process.

To use this script, save it to a file and make it executable using the « chmod » command, as described in the previous examples. Then, you can run the script by typing « ./script.sh », where « script.sh » is the name of the file containing the script.

Keep in mind that this script only displays the services that are currently using swap space. If you want to see the total amount of swap space used by all services, you can use the « free » command, as in the following example :

# free -m | grep Swap | awk '{print $3}'

Books to learn bash script :

Here are a few books that can help you learn Bash scripting :

1- « Learning the bash Shell » by Cameron Newham and Bill Rosenblatt: This is a classic tutorial that covers the basics of Bash scripting and is suitable for beginners. It covers topics such as shell syntax, control structures, and functions, as well as advanced topics such as debugging and performance optimization.

2- « bash Cookbook » by Carl Albing and JP Vossen: This book provides a large collection of recipes for solving common tasks in Bash, such as manipulating files and directories, working with strings and numbers, and interacting with other programs. It also covers advanced topics such as security and system administration.

3- « bash Pocket Reference » by Arnold Robbins: This is a concise reference guide that covers the core elements of Bash, including shell syntax, built-in commands, and control structures. It also includes a quick reference to regular expressions and advanced shell features.

These books should provide a good foundation for learning Bash scripting.