Diamantopoulos on Press

if(!success){try++;}

Bash scripts

I present here some useful bash scripts I wrote mostly for the Operating System Lab exercises.

  • Backup Bash Script : A script that backs up all files in home directory modified within last 24 hours in a “tarball” (tarred or bzipped file).

#!/bin/bash
###################################################################################
#  Filename    : backup.sh
#  Purpose     : Backs up all files in home directory modified within last 24 hours
#                in a “tarball” (tarred or bzipped file).
#  Author      : Diamantopoulos Dionisis
#  License     : GNU GPL V3
#  Institution : Computer Engineering and Informatics Department
###################################################################################

# Script name
SCRIPTNAME=”Backup bash-script”
# Version
VERSION=1.04
# Copyright
COPYRIGHT=”(c) 2009 Diamantopoulos Dionisis (GNU GPL V.3)”
# Start time
STARTTIME=`date`
echo “#######################################################################”
echo “$SCRIPTNAME “
echo “Version $VERSION”
echo “$COPYRIGHT”
echo “Start Time: $STARTTIME”
echo “#######################################################################”

### Append date in form dd_mm_yyyy in backup filename.
BACKUPFILE=backup_of_$(date +%d-%m-%Y)

#  If no compression level specified on command-line,
#  it will default to 6
DCL=6 #default compression level

#argument syntax validation stage
if [[ $# == [0,1] ]]
then
#compression level validation stage
if [[ $1 == [1-9] ]]
then
COMPRESION_LEVEL=${1:-$DCL}
echo “The comprssion level \”$1\” is valid.”
#echo $COMPRESION_LEVEL –only for test
#echo $DEF_COM_LE –only for test
else
if [[ $# == 1 ]]
then
echo “The comprssion level \”$1\” is not valid. Using default \”$DCL\”.”
else
echo “No argument found. Using default comprssion level: \”$DCL\”.”
fi
fi

# Working dir is home (~) .
PREFIXDIR=~

# main command find allocates files(-type f) that are changed the last day(-mtime -1)
# except the archive file (-prune ! -name backup_of_*). The files from the previous list
# are tared (tar cvf) with a nice pipe from previous stdout(-print).
tar cvf – `find $PREFIXDIR -mtime -1 -type f -prune ! -name backup_of_* -print` > $BACKUPFILE.tar

############### checks if bzip2 command exists ####################################
BZIP2_BIN_PATH=/usr/bin/bzip2
if [[ -e $BZIP2_BIN_PATH ]]
then
bzip2 -f -$COMPRESION_LEVEL $BACKUPFILE.tar
echo ” <” ~ “> backed up in archive file \”$BACKUPFILE.tar.bz2\”.”
else
echo “command bzip2 not found!”
echo ” <” ~ “> backed up in archive file \”$BACKUPFILE.tar\”.”
fi
###################################################################################

######### checks if BACKUPDIR exists and if not create it and move backup #########
BACKUPDIR=$PREFIXDIR/Backup
if [ ! -d $BACKUPDIR ]
then
mkdir $BACKUPDIR
fi
mv $BACKUPFILE* $BACKUPDIR
###################################################################################

SCRIPTFINISHTIME=`date`
echo “#######################################################################”
echo “Finish Time: $SCRIPTFINISHTIME”
echo ” “

else
echo “ERROR! Too many arguments!!! “
echo “Usage: backup.sh [COMPRESION_LEVEL]  “
echo “Choose a value @{0-9} or leave it blank for Default = \”$DCL\”.”
fi # end of syntax checking – exit after

exit 0

# The following alternatives I wrote-found are also valid:
# I append them here them for the GPL spirit.
# ——————————————————————-
#   find . -mtime -1 -type f -print0 | xargs -0 tar rvf “$archive.tar”
#      using the GNU version of “find”.

#   find . -mtime -1 -type f -exec tar rvf “$archive.tar” ‘{}’ \;
#         portable to other UNIX flavors, but much slower.
# ——————————————————————-

………………………………………………………………………………………………………………………………………………………………………

  • File name Finder : Finds in current directory files that their name match a pattern and prints the sum of their lines for all of them

#************************************START************************************
#—————————–Filename : countsrc.sh————————–
#Operation : Finds in current directory files that their name match a pattren..
#*******and prints the sum of their lines for all of them**********************
#******************************************************************************

#First i find from current directory (.) theese files that their name match a
#pattern given by first argument (-name $1). In every path i find i put a null
#byte (-print0). Then i print the line count (wc -l) for every file. Theese #files are distinguished by the null-byte that i adde befire. So what i do is #piping the find results to ‘wc-l’ with arguments of null-bytes (xargs-0) in #order to distinguish the files. Finally i use awk to summarize the first #column ($1)  as this has the line numbers.

#——————————————————————————
find . -name $1 -print0 | xargs -0 wc -l | awk ‘ { sum+=$1} END {print “total lines: ” sum}’
#——————————————————————————

#easy now!
#************************************END****************************************

#!/bin/bash

#****************************************START********************************
#——————————Filename:findsrc.sh—————————-
#************Operation : Finds and prints patterns in all sub-dirs************
#*****************************************************************************

#-Create a new command named dirs that find all files (including sub-dirs)**
echo ‘find . -type f -print0 | xargs -0′ > dirs;
#————————————————————————–

for i in `sh dirs`  #for every file including current and sub-sub dirs
do

echo $i `grep -n $1 $i` | grep [0-9] #grep the pattern given as first argument

done     #easy now
#********************************END***********************************

#!/bin/bash

#***********************************START*************************************
#—————————-Filename : checkfriends.sh ———————-
#–Operation : Inform about last time my friends has logged in. Friend’s ….
#usernames sre keeped to a file named “.friends” on home directory.———-
#Known Bugs : Do nothing with “friends” listed in .friends with no unmae.
#*****************************************************************************

for user in `cat ~/.friends`    # for every name exsist in .friends
do
#——————————————————————————
#When i finger a user i view the pattern “On since” only when the user is online
#So , in order to find the online users i finger current user and from  the
#pipelined data i grep only the count lines that “On since” does exists. So the
#variable chech has value 0 for no-logged users , 1 for logged , 2 for logged in
# 2 virtual machines , etc…

check=`finger $user | grep -c “On since”`
#——————————————————————————

#——————————————————————————
if [ $check -gt 0 ]   #if current user is online (check > 0 )
then
echo “User $user is online since “

#Here i use last command with latest-most recent line(-1) so as to print the
#last time current user logged (whatsoever now or past). Fromn the data i #’catch’ with awk the columns for day-#day-month-time-virtual console.

last -1 $user | awk ‘ { print $3 “-” $5 “-” $4 ” at time ” $6 ” on ” $2 ” console”\n } ‘
#————————————————————————————

#————————————————————————————
else             #if current user is offline
echo “User $user is not logged in. Last seen on “

#same technique for last
last -1 $user | awk ‘ { print $3 “-” $5 “-” $4 ” at time ” $6 ” on ” $2 ” console\n” } ‘

fi
#————————————————————————————
done  #easy now!

#****************************************END**************************************

#!/bin/bash

#***************************************START***************************************
#—————————–Filename : problem4.txt——————————-
#———————Operation : Analyze some sub-exercises————————
#***********************************************************************************

#——-1. Find all currently connected users and sort them by login uniquely.—
#I use the command :

who | awk ‘ {print $1}’ | sort -u | less

#who command gives all currently connected users. From them i grab only first.
#..column which is logins (awk) . I sort these with sort command with parametre
#’-u’ to view them unique. Finally i use less command to view them in control
#mode (browsing with arrows and exit with ‘q’).
#——————————————————————————–

#———-2.Find all files that their name ends with character c or h.———–
# I use the command :

find . -type f -name “*[ch]“

#Finds in current and sub-directories (.) files (-type f) whose names (-name) ends
with c or h (“*[ch]). Here i used the wildcard *[ch]
#———————————————————————————

#————3.Find all active system processes , prints them to screen and save
them to file aliveprocs.txt—————————————————–
#I used the command :

ps -axl | grep R | tee aliveprocs.txt | less

#I use ps -axl to show all processes. Then i grep only the ones that have
character “R” (so are running). Finally i save the pipelined result to file
aliveprocs.txt with ‘tee’ and print it on screen in control mode (move with
arrows – quit with ‘q’ ) with less command. Less is more efficient than ‘more’
as it supports backward movement.
#——————————————————————————–

#——–4. Find all active system processes , prints them to screen and sort
them by size———————————————————————-
#I used the commnand :

ps -axl | grep R | sort -n +7 -8 | less

#Same as before but now i don’t save the result to a file. Also i sort the
processes by their size with command ’sort’ . I use parametre -n for numerical
sorting(not ASCII) and +7 -8 in order to sort by the 7th column which is SZ.
#——————————————————————————–

#*****************************************END**************************************

#!/bin/bash

#!/bin/bash
###################################################################################
#  Filename    : backup.sh
#  Purpose     : Backs up all files in home directory modified within last 24 hours
#                in a “tarball” (tarred or bzipped file).
#  Author      : Diamantopoulos Dionisis
#  License     : GNU GPL V3
#  Institution : Computer Engineering and Informatics Department
#################################################################################### Script name
SCRIPTNAME=”Backup bash-script”
# Version
VERSION=1.04
# Copyright
COPYRIGHT=”(c) 2009 Diamantopoulos Dionisis (GNU GPL V.3)”
# Start time
STARTTIME=`date`
echo “#######################################################################”
echo “$SCRIPTNAME “
echo “Version $VERSION”
echo “$COPYRIGHT”
echo “Start Time: $STARTTIME”
echo “#######################################################################”

### Append date in form dd_mm_yyyy in backup filename.
BACKUPFILE=backup_of_$(date +%d-%m-%Y)

#  If no compression level specified on command-line,
#  it will default to 6
DCL=6 #default compression level

#argument syntax validation stage
if [[ $# == [0,1] ]]
then
#compression level validation stage
if [[ $1 == [1-9] ]]
then
COMPRESION_LEVEL=${1:-$DCL}
echo “The comprssion level \”$1\” is valid.”
#echo $COMPRESION_LEVEL –only for test
#echo $DEF_COM_LE –only for test
else
if [[ $# == 1 ]]
then
echo “The comprssion level \”$1\” is not valid. Using default \”$DCL\”.”
else
echo “No argument found. Using default comprssion level: \”$DCL\”.”
fi
fi

# Working dir is home (~) .
PREFIXDIR=~

# main command find allocates files(-type f) that are changed the last day(-mtime -1)
# except the archive file (-prune ! -name backup_of_*). The files from the previous list
# are tared (tar cvf) with a nice pipe from previous stdout(-print).
tar cvf – `find $PREFIXDIR -mtime -1 -type f -prune ! -name backup_of_* -print` > $BACKUPFILE.tar

############### checks if bzip2 command exists ####################################
BZIP2_BIN_PATH=/usr/bin/bzip2
if [[ -e $BZIP2_BIN_PATH ]]
then
bzip2 -f -$COMPRESION_LEVEL $BACKUPFILE.tar
echo ” <” ~ “> backed up in archive file \”$BACKUPFILE.tar.bz2\”.”
else
echo “command bzip2 not found!”
echo ” <” ~ “> backed up in archive file \”$BACKUPFILE.tar\”.”
fi
###################################################################################

######### checks if BACKUPDIR exists and if not create it and move backup #########
BACKUPDIR=$PREFIXDIR/Backup
if [ ! -d $BACKUPDIR ]
then
mkdir $BACKUPDIR
fi
mv $BACKUPFILE* $BACKUPDIR
###################################################################################

SCRIPTFINISHTIME=`date`
echo “#######################################################################”
echo “Finish Time: $SCRIPTFINISHTIME”
echo ” “

else
echo “ERROR! Too many arguments!!! “
echo “Usage: backup.sh [COMPRESION_LEVEL]  “
echo “Choose a value @{0-9} or leave it blank for Default = \”$DCL\”.”
fi # end of syntax checking – exit after

exit 0

# The following alternatives I wrote-found are also valid:
# I append them here them for the GPL spirit.
# ——————————————————————-
#   find . -mtime -1 -type f -print0 | xargs -0 tar rvf “$archive.tar”
#      using the GNU version of “find”.

#   find . -mtime -1 -type f -exec tar rvf “$archive.tar” ‘{}’ \;
#         portable to other UNIX flavors, but much slower.
# ——————————————————————-

Υποβολή απάντησης

XHTML: Μπορείτε να χρησιμοποιήσετε αυτές τις ετικέτες: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>