Useful Bash Shell Scripts

Coşqun Həsənov
2 min readMar 13, 2023

--

Useful Bash Shell Scripts

Nested if else elif check file(s) and folder(s):

ubuntu:~/Bash_scripts# vim nested_if.sh
1 #!/bin/bash
2 if [ $# -eq 1 ]; then
3 if [ -f $1 ]; then
4 echo "Displaying arguments:"
5 sleep 1
6 cat $1
7 elif [ -d $1 ]; then
8 echo "Displayed arguments:"
9 sleep 1
10 ls -l $1
11 else
12 echo "Invalid input!! ($1)"
13 fi
14 else
15 echo "No argument given"
16 fi

ubuntu:~/Bash_scripts# bash nested_if.sh /root/table.txt
Displaying arguments:
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| Operator | Description |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| num1 -eq num2 | Check if numbers are equal |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| num1 -ne num2 | Check if numbers are not equal |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| num1 -lt num2 | Checking if num1 is lower than num2 |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| num1 -le num2 | Lower or equal than num2 |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| num1 -gt num2 | Greater than num2 |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| num1 -ge num2 | Greater or equal than num2 |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|

ubuntu:~/Bash_scripts# bash nested_if.sh /root
Displayed arguments:
total 32
drwxr-xr-x 2 root root 4096 Mar 9 00:00 Bash_scripts
- - - - - 1 root chsnv 20 Mar 6 00:00 file1.txt
- - - - - 1 root root 20 Mar 9 00:00 file2.txt
-rw-r - r - 1 root root 65 Mar 9 00:00 find_txt.sh
-rw-r - r - 1 root root 119 Mar 9 00:00 for.sh
-rw-r - r - 1 root root 252 Mar 10 00:00 nested_if.sh
drwxr-xr-x 2 root root 4096 Mar 6 00:00 newdir
-rw-r - r - 1 root root 0 Mar 6 00:00 old.txt
-rw-r - r - 1 root root 870 Mar 5 00:00 table.txt
- - - - - 1 root root 0 Mar 6 00:00 test.sh

if else check internet drop example:

ubuntu:~/Bash_scripts# vim ip.sh
1 #!/bin/bash
2 ch="$(ping -c 2 $1)"
3 #echo $ch
4 if [[ "$ch" == *"100% packet loss"* ]]; then
5 echo "$1 connection down"
6 else
7 echo "$1 connetion up"
8 fi

ubuntu:~/Bash_scripts# bash ip.sh 8.8.8.99
8.8.8.99 connection down

ubuntu:~/Bash_scripts# bash ip.sh 8.8.8.8
8.8.8.8 connetion up

cat deny_ip.txt and ip_block.sh script to block them:

ubuntu:~/Bash_scripts# cat deny_ip.txt
8.8.8.8
192.168.1.1
192.168.1.228

ubuntu:~/Bash_scripts# vim ip_block.sh
1 #!/bin/bash
2 for ip in $(cat deny_ip.txt); do
3 echo "Blocked IP: $ip"
4 iptables -I INPUT -s $ip -j DROP
5 done

ubuntu:~/Bash_scripts# bash ip_block.sh
Blocked IP: 8.8.8.8
Blocked IP: 192.168.1.1
Blocked IP: 192.168.1.228

ubuntu:~/Bash_scripts# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
- - 192.168.1.1 ping statistics - -
3 packets transmitted, 0 received, 100% packet loss, time 2052ms

ubuntu:~/Bash_scripts# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
- - 8.8.8.8 ping statistics - -
3 packets transmitted, 0 received, 100% packet loss, time 2051ms

Conclusion

That is all for now. See you in the following one, and remember to stay in touch. I hope you have a wonderful day.

--

--

Coşqun Həsənov
Coşqun Həsənov

No responses yet