7 Bash Shell Scripts Every Linux Administrators Should Know About

7 Bash Shell Scripts Every Linux Administrators Should Know About

In this article, I will take you through 7 Important Bash shell scripts that every Linux Administrators Should Know About. In a typical scenario, every Linux administrator runs certain set of repetitive commands as part of their day to day task. Sometimes running command manually can be boring and frustrating so to deal with this situation it is always recommended to use bash shell scripts wherever possible to accomplish the job. We will go through some of the most basic bash shell scripting which can be easily understood by beginners as well.

7 Bash Shell Scripts Every Linux Administrators Should Know About

7 Bash Shell Scripts Every Linux Administrators Should Know About

Also Read: How to Set/Assign output of a Linux Command to a Variable

Script #1: How to check if interfaces on server are present using shell script

There are multiple ways to find out the existing interfaces on a server. Below are the 2 ways to do it.

a) Using ip link command

Here we are using ip link command with if else statement to check if a specific interface is available on a Server.

#!/bin/bash

if ip link show eth5 ;then
   echo "eth5 present"
else
   echo "eth5 not present"
fi

Output

2: eth0:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether fa:16:3e:9e:bc:4e brd ff:ff:ff:ff:ff:ff
eth5 present

b) Using ethtool utility

Another useful utility that we can think of using is ethtool. We can use for loop to go through multiple interface and check if it available. More about ethtool command.

#!/bin/bash

for interface in eth0 eth5
do
    if [[ ! $( ethtool $interface | grep -i "Link detected: yes" ) ]] ;then
    echo "$interface is inactive"
        else
    echo "$interface is active"
    fi
done

Output

eth0 is active
Cannot get device settings: No such device
Cannot get wake-on-lan settings: No such device
Cannot get message level: No such device
Cannot get link status: No such device
eth5 is inactive

 

Script #2: How to check if a server is reachable or not 

One of the very common error that all Linux administrator encounter is checking the server unreachable issue. For this, the first or initial check is to always use the ping command(if ICMP is enabled) to verify the Server reachability. This can be easily done through script as shown below.

#!/bin/bash

read -p "Server Address " addr
ping -c3  $addr || echo "Server Dead"

Output

Server Address 10.55.7.47
PING 10.55.7.47 (10.55.7.47) 56(84) bytes of data.
64 bytes from 10.55.7.47: icmp_seq=1 ttl=61 time=0.839 ms
64 bytes from 10.55.7.47: icmp_seq=2 ttl=61 time=0.558 ms
64 bytes from 10.55.7.47: icmp_seq=3 ttl=61 time=0.523 ms
--- 10.55.7.47 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms

 

Script #3: How to SSH to a remote server using shell script

Often, whenever we have to remote login to a server we use ssh command. But instead of running ssh command manually, we can use a script which asks for Server IP and User name at the input. This will excuse us from running ssh command manually every time.

#!/bin/bash

set -x
read -p "Server_IP " server_name
read -p "User_name " user_name
ssh ${user_name}@$server_name

Output

+ read -p 'Server_IP ' server_name
Server_IP 10.55.7.47
+ read -p 'User_name ' user_name
User_name root
+ ssh root@10.55.7.47
The authenticity of host '10.55.7.47 (10.55.7.47)' can't be established.
ECDSA key fingerprint is SHA256:AKWkB8cUTo6vtyECnhK78elIEnLcp0ZDYzkcY9P0tz8.
ECDSA key fingerprint is MD5:08:d0:c1:96:9b:78:c1:e2:ac:86:ad:4f:35:2a:27:74.
Are you sure you want to continue connecting (yes/no)?
Punch the password to connect to remote server

 

Script #4: How to copy file on remote server using shell script

Another common task that is frequently performed are to copy the files to or from the remote server. This can be done through scp(secure copy) command. Instead of running this command manually, we can also use below script to copy the files to remote Server.

#!/bin/bash

set -x
read -p "File path " file_path
read -p "User_name " user_name
read -p "Server_name " server_name
read -p "Dest_path " path

scp ${file_path} ${user_name}@$server_name:${path}

Output

File path /root/test1.sh
User_name root
Server_name 10.55.7.47
Dest_path /root/ssl_security
The authenticity of host '10.55.7.47 (10.55.7.47)' can't be established.
ECDSA key fingerprint is SHA256:AKWkB8cKJo6vtyECnhK65elIEnLcp0ZDYzkcY9P0tz8.
ECDSA key fingerprint is ME5:08:d0:c1:96:9b:73:c1:e1:ac:87:ad:4f:35:2a:27:74.
Are you sure you want to continue connecting (yes/no)?
Punch the password to connect to complete the scp operation

 

Script #5: How to read a file and print on console or redirect to standard output 

Bash shell script can also be utilized for reading a file and then printing the output on console or redirecting it to some other file. Here we are creating a file test.sh with below content which will get printed on console or redirected to standard output.

#!/bin/bash

while read line
do
    echo $line > /dev/null 2>&1     # redirect the o/p to std err
done < test.sh
    echo "Execution completed"

Output

Hello There
Script execution is successful
Thank you!!
Execution completed

 

Script #6: How to extract file name and file extension from a given file using shell script

Extracting file name and extension from a given file can be easily accomplished through a bash shell script. For example, here we are extracting file name and extension of image.jpg file using below bash script.

#!/bin/bash

file="image.jpg"
name=${file%.*}
extension=${file#*.}

echo "File Name: $name"
echo "File Extension: $extension"

Output

File Name: image
File Extension: jpg

 

Script #7:  How to rename files with extension *.jpg in current directory using shell script

Renaming files are another occasional task that we always perform. This can also be done through a bash shell script. To demonstrate, we have created two files in current directory car.jpg and bike.jpg. It will get renamed using below script.

#!/bin/bash

step=1
for pic in `find . -maxdepth 1 -iname '*.jpg' -type f`
do
   new=image-$step.${pic##*.}
   echo "Renaming $pic to $new"
   mv "$pic" "$new"
   let step++
done

Output

Renaming ./car.jpg to image-1.jpg
Renaming ./bike.jpg to image-2.jpg

 

Important commands

a) To stop a network interface

We can use ifdown command to bring down the interface. In this example, we are bringing down the interface eth3 using ifdown eth3 command as shown below.

root@cyberithub:~# ifdown eth3
Device 'eth3' successfully disconnected.

 

b) To start a network interface

Similarly, we can use ifup command to bring up the interface. In this example, we are bringing up the interface eth3 using ifup eth3 command as shown below.

root@cyberithub:~# ifup eth3
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/69442)

 

c) To check if a network interface exist

To check the existence of a network interface, we can use ip a show syntax. In this example, we are checking the existence of eth5 interface using ip a show eth5 command as shown below.

root@cyberithub:~# ip a show eth5
Device "eth5" does not exist.

 

d) To execute script in debug mode

If you have a script file.sh that you want to execute in debug mode, then you can either execute like ./file.sh -x as shown below.

root@cyberithub:~# ./file.sh -x

Or, you can also run as ./file.sh -v as shown below.

root@cyberithub:~# ./file.sh -v
bash Bash Shell