Skip to content
Snippets Groups Projects
Commit d97e37ae authored by zmap's avatar zmap
Browse files

shell script functions

parent 596a2569
No related branches found
No related tags found
No related merge requests found
#!/bin/echo DOT SCRIPT please source
# A few absolute requirements for non-failure
if [ "x$SCRIPT_NAME" == "x" ]; then
SCRIPT_NAME=$(basename $0)
fi
if [ "x$ECHO" == "x" ]; then
ECHO=/bin/echo
fi
# message functions
# Usage: zmap_message_out Your Message
function zmap_message_out
{
now=$(date +%H:%M:%S)
$ECHO "[$SCRIPT_NAME ($now)] $*"
}
# Usage: zmap_message_err Your Message
function zmap_message_err
{
now=$(date +%H:%M:%S)
$ECHO "[$SCRIPT_NAME ($now)] $*" >&2
}
# Usage: zmap_message_exit Your Message
function zmap_message_exit
{
zmap_message_err $*
exit 1;
}
function zmap_message_file
{
if [ "x" != "x$1" ]; then
local file=$1
shift
$ECHO "[$SCRIPT_NAME ($now)] $*" >> $file
fi
}
function zmap_create_file
{
if [ "x" != "x$1" ]; then
$ECHO -n "" > $1
fi
}
# last absolute requirements
if [ "x$BASE_DIR" == "x" ]; then
zmap_message_exit "$0 needs to set BASE_DIR!"
fi
function zmap_check
{
$ECHO > /dev/null
}
# common variables
ZMAP_TRUE=yes
ZMAP_FALSE=no
# default assignments...
zmap_check ${RM:=rm}
zmap_check ${MKDIR:=mkdir}
# common functions
# Usage: zmap_cd <dir>
function zmap_cd
{
if [ "x$SILENT_CD" != "x$ZMAP_TRUE" ]; then
zmap_message_out "cd $1"
fi
cd $1 || zmap_message_exit "Failed to cd to $1"
}
function zmap_mkdir
{
$MKDIR -p $1 || zmap_message_exit "Failed to mkdir $1"
}
function zmap_clean_dir
{
if [ -d $1 ]; then
save_dir=$(pwd)
zmap_cd $1
zmap_message_out "Running $RM -rf ./*"
$RM -rf ./* || zmap_message_exit "Failed to remove files in $1"
zmap_cd $save_dir
fi
}
# Usage: zmap_trap_handle
function zmap_trap_handle
{
zmap_message_exit "Signal caught!";
}
trap 'zmap_trap_handle;' INT
trap 'zmap_trap_handle;' TERM
trap 'zmap_trap_handle;' QUIT
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment