Examples of Bash scripts

There are quite a number of shells, such as sh, zsh, ksh, and others. But we will focus on Bash, the most popular shell among Linux. Now even Microsoft has added support for Bash.

This article is intended for those who are at least somewhat familiar with the scripting language Bash. Let’s look at some examples of Bash scripts that may be useful in your daily work.

Examples of Bash scripts in Linux

To see which shell you use, type the command:

echo $SHELL

As you can see I have installed it a Bash.

1. The first program. Modification of the command ‘rm’

#!/bin/bash
dir="$HOME/.archive/" # directory for deleted files
if [ -d $dir ]; then # check the directory .archive/
file="$1"
null=""
else mkdir $dir | chmod 700 $dir # if there is no create
fi
if [ $file == $null ]; then # error, if not specified file #
echo -e "/! No file.. Usage: $0 filename ? | archive directory $dir /! "
exit 1
fi
mv $file $dir$(date "+%H.%d.%m").$file # move file to .archive/

We all know what makes the rm command, it removes the file. This program creates a folder ~/.archive. Further checks whether the specified argument. Without an argument, the program will display an error message and stop working. When passed a file path, it puts it into the directory ~/.archive and adds the date(hour,day,month) in the beginning of the file name, so we can understand when deleted the file.

Next, I will give you some more programs, but without explanation. These programs can be modified to improve anything. Try to write something of my own.

2. Check the program path in the PATH variable

#!/usr/bin/bash
in_path() {
cmd=$1 ourpath=$2 result=1
oldIFS=$IFS IFS=":"
for directory in "$ourpath"
do
if [ -x $directory/$cmd ]; then
result=0
fi
done
IFS=$oldFS
return $result
}
##########
4ck() {
var=$1
if [ "$var" != "" ]; then
if [ "${var:0:1}" = "/" ]; then
if [ ! -x $var ];then
return 1
fi
elif ! in_path $var "$PATH" ; then
return 2
fi
fi
}
##############################
if [ $# -ne 1 ]; then
echo "Usage: $0 command" >&2
exit 0
fi
4ck "$1"
case $? in
0 ) echo "[$1] found in PATH" ;;
1 ) echo "[$1] not found or not executable" ;;
2 ) echo "[$1] not found in PATH" ;;
esac
exit 0
#############################

3. The normalization of the output date

#!/bin/bash
monthto() {
case $1 in
1 ) month="Jan" ;; 7 ) month="Jul" ;;
2 ) month="Jul" ;; 8 ) month="Aug" ;;
3 ) month="Mar" ;; 9 ) month="Sep" ;;
4 ) month="Apr" ;; 10 ) month="Oct" ;;
5 ) month="May" ;; 11 ) month="Nov" ;;
6 ) month="Jun" ;; 12 ) month="Dec" ;;
* ) echo "$0: Unknown month value $1" >&2 exit 1
esac
return 0
}
#################
if [ $# -ne 3 ] ; then
echo "Usage: $0 month day year" >&2
echo "Formats are August 3 1962 and 8 3 1962" >&2
exit 1
fi
if [ $3 -le 99 ]; then
echo "$A: expected a 4-digit year value." >&2
exit 1
fi
if [ -z $(echo $1|sed 's/[[:digit:]]//g') ]; then
monthto $1
else
month="$(echo $1 | cut-c1 | tr '[:lower:]' '[:upper:]')"
month="$month$(echo $1 | cut-c2-3 | tr '[:upper:]' '[:lower:]')"
fi
echo $month $2 $3
exit 0

4. Creating script libraries

Libraries are scripts which you can include in other scripts.

colors()
{
esc="33" ;
# text
blackf="${esc}[30m"; redf="${esc}[31m"; greenf="${esc}[32m";
yellowf="${esc}[33m"; bluef="${esc}[34m"; purplef="${esc}[35m"; cyanf="${esc}[36m"; whitef="${esc}[37m" ;
# backg
blackb="${esc}[40m"; redb="${esc}[41m"; greenb="${esc}[42m" ;
yellowb="${esc}[43m"; blueb="${esc}[44m"; purpleb="${esc}[45m" ; cyanb="${esc}[46m"; whiteboard="${esc}[47m" ;
# bold, italic, etc...
boldon="${esc}[1m"; boldoff="${esc}[22m";
italicson="${esc}[3m"; italicsoff="${esc}[23m" ;
ulon="${esc}[4m"; uloff="${esc}[24m" ;
invon="${esc}[7m"; invoff="${esc}[27m" ;
reset="${esc}[0m" ;
}

Such scenario can be easily switched to another file, command source and calling the desired function of demand.

Conclusion

Today we discussed a few examples of Bash scripts. Learned how to use them as a library of scripts. Very much useful, you can write in Bash, the limit is that only your imagination. But in fact, the theme of the Bash scripts are great and there are entire books dedicated to this topic. I hope the article was useful. About the basics of working with Bash you can read here.

Source: losst.ru

(Visited 40 times, 1 visits today)