7.2. BASH Shell

A shell keeps everything that is inside safe and secure.

../../_images/seashell.jpg

Photo Credit: George Girnas on Unsplash

In a similar sense, in Unix/Linux terminology, a Shell is an interface to the Operating System (OS). It keeps the core implementaiton of the OS and underlying system abstracted.

Shell scripting is a very much powerful for many repetitive tasks on your computer. But on the other hand, it can also be used for completing an in-promptu/unplanned task very efficiently.

7.2.1. Find Command

Find and list MD5 hashes of all files in a parent directory.

find .. -name "*.rst" -exec md5 "{}" ";"

# Can be run as below as well:
#
#   find .. -name "*.rst" -exec md5 "{}" \;
#   find .. -name "*.rst" -exec md5 {} \;
MD5 (../index.rst) = 2ab129a3b7a2d0cd35a4a60a4bdad91a
MD5 (../bash-shell.rst) = e6fcc35111fe02e1e5b23064c2dd0656
MD5 (../dos-batch-files.rst) = 603de7f4236e418c7b336193c1647c7a
MD5 (../power-shell.rst) = 3f030c5ef093f120eeb46800c6c98cbb
MD5 (../sed.rst) = 7aba3cac88de6754ce76f079de3784c1
MD5 (../awk.rst) = 5e2ae60e1ab46c6ae785da0422b73a01

Similar to previous, but find and list SHA1 hashes of all files in a parent directory.

find .. -name "*.rst" -exec sha1sum "{}" ";"

# Can be run as below as well:
#
#   find .. -name "*.rst" -exec sha1sum "{}" \;
#   find .. -name "*.rst" -exec sha1sum {} \;
5e4cc89170d0e8c807ec67a02c2dcca4ebf649be  ../index.rst
603956559d1b67996c9bb4c53cc07dc1933ef4dc  ../bash-shell.rst
c8e88d7578604b17cf93d48b375c73d8dbdcdb73  ../dos-batch-files.rst
8610842ff20212a915f66b37bb4145c65efdccbf  ../power-shell.rst
2c368e46120cc30d144622111fddd07edf7d4d11  ../sed.rst
70ef832723ce3a466554c912f2ad2cd582027b49  ../awk.rst

7.2.2. For Command

Print few numbers in a loop.

for num in 1 2 3 4 5;
do
    echo num=$num
done
num=1
num=2
num=3
num=4
num=5

Similar to previous, but print some values from a list.

for name in alpha beta gamma;
do
    echo name=$name
done
name=alpha
name=beta
name=gamma

Print numbers, but from a range and step.

for num_incr in {1..4};
do
    echo num_incr=$num_incr
done
num_incr=1
num_incr=2
num_incr=3
num_incr=4

Print numbers, from a range, and step by a larger number.

# Print from 10 to 30, but step of 4.
for num_step in {10..30..4};
do
    echo num_step=$num_step
done
num_step=10
num_step=14
num_step=18
num_step=22
num_step=26
num_step=30

7.2.3. IF Command

Check if a file or folder/directory exists.

if [[ -e all.sh ]]; then
    echo "Yes. all.sh exists."
else
    echo "No. all.sh is missing."
fi

if [[ -e something_missing ]]; then
    echo "Yes. something_missing exists."
else
    echo "No. something_missing is missing."
fi
Yes. all.sh exists.
No. something_missing is missing.

Check if a given entry is a folder/directory.

if [[ -d ../sample_bash ]]; then
    echo "Yes. ../sample_bash is a directory."
else
    echo "No. ../sample_bash is not a directory."
fi

if [[ -d all.sh ]]; then
    echo "Yes. all.sh is a directory."
else
    echo "No. all.sh is not a directory."
fi
Yes. ../sample_bash is a directory.
No. all.sh is not a directory.