Bash script default start

#!/bin/bash
# Description of the script

#DEBUG=yes      # Comment out. Anything but 'yes' will do not do debugging.
#RUNONCE=1      # 0 or commented all, 1 will close this scripts, 2 closes others

#The standard {{{
#Standard parametters for debugging{{{
L_C="?" # The line character e.g. =
L_L="?" # Left side in a line e.g. >
L_R="?" # Right side in a line e.g. <
#The three above will make a line like  ===> text here <======
LINE_=$(printf "%$(tput cols)s"|sed "s/ /${L_C}/g")
# Colors{{{
txtund=$(tput sgr 0 1)    # Underline
txtbld=$(tput bold)       # Bold
txtblk=$(tput setaf 0)    # Black
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtyel=$(tput setaf 3)    # Yellow
txtblu=$(tput setaf 4)    # Blue
txtpur=$(tput setaf 5)    # Purple
txtcyn=$(tput setaf 6)    # Cyan
txtwht=$(tput setaf 7)    # White
txtrev=$(tput rev)        # Reverse text
txtoff=$(tput sgr0)       # Text reset
# Trap and temp file
TMP=$(mktemp)
trap "rm $TMP* -rf" 0 1 2 3 5 6 9 14 15
# }}}
#}}}
# Go back to the original deskto {{{
GETBACK ()
{
if [ -f "$(which xwininfo)" ] && [ -f "$(which wmctrl)" ]
then
        WINID="$(xwininfo -id $WINDOWID|grep ^xwininfo:|awk '{print $4}')"
        if [ "$WINID" != "" ]
        then 
                wmctrl -ia $WINID
        fi
fi              
}       
# }}}   
# Exit and clean {{{
EXIT () {
        GETBACK
        exit $1
}       
# }}}   
# Identify an error {{{
ERROR () {      
        test -t $DISPLAY || GETBACK
        LINE "${txtred}Something went wrong${txtoff}"
        if [ -n "$1" ] 
        then
                echo "It went wrong on line $@"
                EXIT 1
        fi
}
# }}}
# Make a heading line {{{
LINE ()
{
        printf "$LINE_\r" 
        if [ "$1" != "" ] 
        then
                tput cuf 3
                printf "${L_L} $@ ${L_R}\n"
        else
                printf "\n"
        fi
} #}}}
# Check if it needs to run once {{{
PID_NO=$(pidof -x "$(basename $0)")
PID_CT=$(wc -w <<< $PID_NO)
if [ "$PID_CT" -gt 1 ] && [ "$RUNONCE" == "2" ]
then
        LINE "${txtyel}Killed all the other instances${txtoff}"
        kill -9 $(echo "$PID_NO"| sed "s/$$//g")
fi
if [ "$PID_CT" -gt 1 ] && [ "$RUNONCE" == "1" ]
then    
        LINE "Closing this script $(basename $0)"
        echo "An other instance is already running. 
You can only run on at a time"
        EXIT 2
fi              
# }}}   
#Check debugging {{{
if [ "$DEBUG" == "yes" ] 
then            
        SPACES=$(printf "%$(tput cols)s")
PS4='+${txtbld}\
${txtgrn}${SPACES:0:$((5-${#LINENO}))}$LINENO \
${txtblu}${FUNCNAME:-${SPACES:0:12}}${FUNCNAME:+${SPACES:0:$((12 - ${#FUNCNAME}))}}\
${txtoff}'
        set -xe
fi
#}}} 
# }}}  
# Start of the script
#######################################################################

#Sample for RUNONCE
clear

echo "
Uncomment DEBUG and run the script with yes and no

"
echo "
To test RUNONCE. Start a second terminal with  the same script
Try it with 0, 1 and 2 and see what happens.

The script will go on in 15 seconds."
sleep 15

echo "
Showing a line"
LINE

echo "

"

LINE "Showing a line with comment"

echo "
The temp file. Write time and showing it
"
date > $TMP
cat $TMP

echo "
Testing the error. This will happen in 5 seconds.

Go to a different desktop if possible."

sleep 5

false || ERROR $LINENO. Minor explanation

echo "
The script will not get here
"



#######################################################################
#The exit
EXIT 0

Explanation

I use this as a default to start all my bash scripts. I use vim and folding, so what I see actually when I start the script is the following:

#!/bin/bash
# Description of the script

#DEBUG=yes      # Comment out. Anything but 'yes' will do not do debugging.
#RUNONCE=1      # 0 or commented all, 1 will close this scripts, 2 closes others

+-- 95 lines: The standard -----------------------------------------------------------------------------------
+-- 50 lines: Start of the script ----------------------------------------------------------------------------
#The exit
EXIT 0

So I open the fold of “Start of the script”. By default this would be empty and I start writing my script. So here is what it all does. Run it see see what goes on.

The parameters

On the first line we have the shebang. The second line I add a small description of what the script does.

The next one is DEBUG=yes Un-comment this if you want debugging on. More on that on this page. After that is RUNONCE=1 Un-comment it and when it is set to 0, it will do nothing. When it is set to 1, it will keep the last script that runs running, but closes all others. If set to 2, it will keep the first version running and no second version is possible.

The standard

We have the making of lines. You can edit the characters, so the line will look differently. After that I have the colors I sometimes use. Easier to remember and look up.

After that I have the TMP that will be a TMP file if I need one. The trap sees that it is removed no matter what.

Back to the desktop

If you run on GUI, it might be that you are going to a different desktop. GETBACK can be called when you want user feedback or just inform the user. When you have a script running for a long time and then needs input while you are browsing on another desktop. xwinfo and wmctrl need to be installed.

Exit and clean

It just exits and draws you back. Additional cleaning can be placed here. There is a difference between the EXIT and the trap.

Identify an error

Use it after a pipe. You either add just || ERROR after a command. The || means it did not work out. You can also do || ERROR $LINENO as well as || ERROR $LINENO. Small remark To show the line number ore also add a small remark.

Make a heading line

This will just show a line, or a line with text.

Running once and debugging

On verifies if the script is allowed to be run only one or not and if this or the other script(s) need to. The other does set the debugging parameters with pretty colors.

Starting of the script.

All between the lines of ### is just the normal script. You can use the things before (besides DEBUG and RUNONCE) as normal.

Just delete everything there and start writing your own.

EXIT

Instead of using exit or exit 1, you use EXIT or EXIT 1.

Conclusion

Having a default to start scripts makes it easy to do repetitive things. My needs will be different from yours. So make your own. With or without mine as a basis.