Tuesday, May 22, 2018

Make your Shell Script interactive using read command,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 23/05/2018
#
echo "Usage Of Read Command in Linux"
rc=0 # return code
while [ $rc -eq 0 ]
do
read -p "Enter value or q to quit: " var
echo "var: $var"
if [ "$var" = "q" ] ; then
rc=1
fi
done
rc=0 # return code
while [ $rc -eq 0 ]
do
read -p "Password: " -s var
echo ""
# carriage return
echo "var: $var"
if [ "$var" = "q" ] ; then
rc=1
fi
done
echo "Press some keys and q to quit."
rc=0
# return code
while [ $rc -eq 0 ]
do
read -n 1 -s var # wait for 1 char, does not output it
echo $var # output it here
if [ "$var" = "q" ] ; then
rc=1
fi
done
exit $rc

Monday, May 21, 2018

Automatic backing up a file if any changes occur in a regular interval,Linux Teacher Sourav,Kolkata 09748184075



 sh ./autobackup.sh a1.txt usb/ 3

 autobackup.sh=script name

a1.txt=file to be monitored

3=interval of checking in seconds

 autobackup.sh

#!/bin/sh
#
# automatically creating a backup of a file if the any changes to the file occurs
# the script that automatically backs up everytime with an incremented number is needed
# Checks that /back exists
# Copies to specific USB directory# Checks if filename.bak exists on startup, copy if it doesn't
# 22/05/2018
if [ $# -ne 3 ] ; then
echo "Usage: autobackup filename USB-backup-dir delay"
exit 255
fi
# Create back directory if it does not exist
if [ ! -d back ] ; then
mkdir back
fi
FN=$1 # filename to monitor
USBdir=$2 # USB directory to copy to
DELAY=$3 # how often to check
if [ ! -f $FN ] ; then # if no filename abort
echo "File: $FN does not exist."
exit 5
fi
if [ ! -f $FN.bak ] ; then
cp $FN $FN.bak
fi
filechanged=0
while [ 1 ]
do
cmp $FN $FN.bak
rc=$?
if [ $rc -ne 0 ] ; then
cp $FN back
cp $FN $USBdir
cd back
sh ./createnumberedbackup.sh $FN
cd ..
cp $FN $FN.bak
filechanged=1
fi
sleep $DELAY
done
****************************************************
createnumberedbackup.sh


#!/bin/sh
#let me show you this script which is able to back up of files in the same directory and number
#the backup,in devops it is very common the save the project with revision number,this
#script is not that complex though


echo "Create Numbered Backup from files of the current directory given as argument"
if [ $# -eq 0 ] ; then
echo "Usage: sh ./createnumberedbackup.sh filename(s) "
echo " Will make a numbered backup of the files(s) given."
echo " Files must be in the current directory."
exit 255
fi
rc=0 # return code, default is no error
for fn in $*  # for each filename given as arguments

do
if [ ! -f $fn ] ; then # if not found
echo "File $fn not found."
rc=1 # one or more files were not found
else
cnt=1 # file counter
loop1=0 # loop flag
while [ $loop1 -eq 0 ]
do
tmp=bak-$cnt.$fn
if [ ! -f $tmp ] ; then
cp $fn $tmp
echo "File "$tmp" created."
loop1=1
# end the inner loop
else
let cnt++ # try the next one
fi
done
fi
done
exit $rc # exit with return code


#every time you run this script with arguments if it finds backup file created by itself

#it will create the next backup with incrementing numbers so the backup can

#more organized


Using TPUT command to change cursor position in a Shell Script,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
# 21/05/2018
# script4
# Subroutines Example
export LINES=$LINES
export COLUMNS=$COLUMNS
#echo $LINES
cls()
{
tput clear
return 0
}
home()
{
tput cup 0 0
return 0
}

bold()
{
tput smso
# no newline or else will scroll
}
unbold()
{
tput rmso
}
underline()
{
tput smul
}
normalline()
{
tput rmul
}
end()
{
#x="${COLUMNS}"-1
let x=$COLUMNS-1

let y=$LINES
#echo x is $x
#echo y is $y
tput cup $y $x
echo -n ""
}
# Code starts here
rc=0
# return code
if [ $# -ne 1 ] ; then
echo "Usage: script4 parameter"
echo "Where parameter can be: "
echo " home - put an X at the home position"
echo " cls - clear the terminal screen"
echo " end - put an X at the last screen position"
echo " bold - bold the following output"
echo " underline - underline the following output"
exit 255
fi
parm=$1
# main parameter 1
if [ "$parm" = "home" ] ; then
echo "Calling subroutine home."
home
echo -n "X"
elif [ "$parm" = "cls" ] ; then
cls
elif [ "$parm" = "end" ] ; then
echo "Calling subroutine end."
end
elif [ "$parm" = "bold" ] ; then
echo "Calling subroutine bold."
bold
echo "After calling subroutine bold."
unbold
echo "After calling subroutine unbold."
elif [ "$parm" = "underline" ] ; then
echo "Calling subroutine underline."
underline
echo "After subroutine underline."
normalline
echo "After subroutine normalline."
else
echo "Unknown parameter: $parm"
rc=1
fi
exit $rc

Saturday, May 19, 2018

Create Numbered backup files from the current working directory given as arguments,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
echo "Create Numbered Backup from files of the current directory given as argument"
if [ $# -eq 0 ] ; then
echo "Usage: sh ./createnumberedbackup.sh filename(s) "
echo " Will make a numbered backup of the files(s) given."
echo " Files must be in the current directory."
exit 255
fi
rc=0 # return code, default is no error
for fn in $*  # for each filename given as arguments

do
if [ ! -f $fn ] ; then # if not found
echo "File $fn not found."
rc=1 # one or more files were not found
else
cnt=1 # file counter
loop1=0 # loop flag
while [ $loop1 -eq 0 ]
do
tmp=bak-$cnt.$fn
if [ ! -f $tmp ] ; then
cp $fn $tmp
echo "File "$tmp" created."
loop1=1
# end the inner loop
else
let cnt++ # try the next one
fi
done
fi
done
exit $rc # exit with return code


Usage:


sh ./createnumberedbackup.sh a1.txt a2.txt

Friday, May 18, 2018

Shell script to watch a process and inform when it ends ,Linux Teacher Sourav,Kolkata 09748184075

#!/bin/sh
#
# 18/5/2018
#
echo "this shell script will watch when a process ends"
if [ $# -ne 1 ] ; then
echo "Usage: processwatcher process-directory"
echo " For example: processwatcher /proc/14856 where 14856 is the PID of the  target process"
exit 255
fi
FN=$1
# process directory i.e. /proc/14856
rc=1
while [ $rc -eq 1 ]
do
if [ ! -d $FN ] ; then
# if directory is not there
echo "Process $FN is not running or has been terminated."
let rc=0
else
sleep 1
fi
done
echo "End of processwatcher script"
exit 0

I have given the name of this script as processwatcher.sh,suppose another job or process is going on simultaneously and i want to know when it ends ,my script will serve that purpose when we run it with the Process ID of the target process or job as an argument to this shell script

For example we created this shell script to show the usage of break and continue in shell scripting ,it will run for a certain amount of time

#!/bin/sh
#
# 18/5/2018
#
echo "Break Continue Example"
FN1=/tmp/break.txt
FN2=/tmp/continue.txt
x=1
while [ $x -le 1000000 ]
do
echo "x:$x"
if [ -f $FN1 ] ; then
echo "Running the break command"
rm -f $FN1
break
fi
if [ -f $FN2 ] ; then
echo "Running the continue command"
rm -f $FN2
continue
fi
let x++
sleep 2
done
echo "x:$x"
echo "End"
exit 0


we first run it from the terminal

sh ./breakandcontunue.sh


Now to see what process ID it has while running we need to use the grep command


ps auxw | grep break* | grep -v grep

the last piped section was to filter the grep search which itself acts as a process

the output is like this

sourav   14856  0.0  0.0  12876  2996 pts/1    S+   20:05   0:00 sh ./breakandcontunue.sh

So the PID as we can see is 14856


so in the /proc/folder we should find a folder named 14856 and it will continue to exist while this script/process/job runs

our shell script processwatcher is basically checking the existence of this folder and thus if the process is running with an interval of 1 second with the help of sleep command