tutorial

So... you liked EasyBashGUI  :-)

You think this library could be useful in your project,

but have no idea how to use it.

No problem. EasyBashGUI is... "Easy" !

... follow me and I will show you .  ;-)

1) Hello world !

Let's start from the beginning...

open a terminal and type ( assuming version is 4.0.0, otherwise use proper version number ) :

>tar -xzvf EasyBashGUI_4.0.0.tar.gz

>cd ./EasyBashGUI_4.0.0

>su

[password]

#sudo make install && exit

>touch hello_world.sh

>chmod 755 hello_world.sh

then, open newly created file in your prefered editor ( e.g. kwrite, gedit, gvim, etc.etc. )

and write all rows, one at time, one below the other, that you see in "text" gadget...

first row...

Hello world ! 1

#!/usr/bin/env bash

this is used to inform operating system that it's a Bash program...

let's continue to second row...

Hello world ! 2

#!/usr/bin/env bash

source easybashgui

this is the actual sourcing of my library... now all functions are loaded in your program, and are ready to go !  :-)

third row...

Hello world ! 3

#!/usr/bin/env bash

source easybashgui

ok_message "Hello world !"

Congratulations! :) your first EasyBashGUI function in your script !  :-)

there are three types of messages: message (neutral), ok_message (for successes), alert_message (for errors)...

...this is the end ( "my only friend, the end" - The Doors )

Hello world ! 4

#!/usr/bin/env bash

source easybashgui

ok_message "Hello world ! \n ...and peace to you..."

clean_temp

we added a second row to our message text, and used the "clean_temp" function that you have always to use when finish a script ( used to remove the two EasyBashGUI temp files ).

* P.S.: Since 6.0.0 version, IF 'easybashlib' library is successfully loaded (if not you will have an alert message) it's NOT mandatory anymore using clean_temp() at the end of your scripts to remove temp files, since easybashlib does it automatically.

Wow! Your first program with my ultimate, universal, widget library !!!  phiewww!!!... ;-P

2) Entering in it...

Ooookay...  :-)

Let's try now to write something a little more useful... ;-) . This program you can use to enter via ssh in different computers/servers without need to remember every time wich IP address they have.

Open the terminal, and type:

>touch enter_in.sh

>chmod 755 enter_in.sh

then, open newly created file in your prefered editor ( e.g. kwrite, gedit, gvim, etc.etc. )

Enter in... 1

#!/usr/bin/env bash

#

source easybashgui

#

###############

first rows are the same of "Hello world !" example, but now let's use "#" character ( used in scripts to comment ) to space and improuve readability...

Enter in... 2

#!/usr/bin/env bash

#

source easybashgui

#

###############

#

menu "enter in Server1" "enter in Server2" "enter in Server3" "enter in Server4"

Wow! we're using a menu !...  :-D

as you see, menu options are simply functions arguments: you can add options at your menu how many you want... ( don't forget quoting!! )

Yes... but, where does user's choice go?

Let's see...

Enter in... 3

#!/usr/bin/env bash

#

source easybashgui

#

###############

#

menu "enter in Server1" "enter in Server2" "enter in Server3" "enter in Server4"

choice="$(0< "${dir_tmp}/${file_tmp}" )"

All user choices (but "question" function choice) go into the EasyBashGUI temp file"${dir_tmp}/${file_tmp}". Remember it...

So, now you can put file content in the "choice" variable, with bash command substitution "$( ... )" ( that is equivalent to `...` but more readable ).

(Note: you could also use "choice=$(cat ${dir_tmp}/${file_tmp} )" form, but in this way you launch an other process, instead in this way "choice=$(0< ${dir_tmp}/${file_tmp} )" you say directly to the shell to read file content, without any external process... )

So we have now a variable with menu option user's choice, but now you have to discriminate it...

( let's pretend that your servers have these IP addresses "192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4" )

Enter in... 4

#!/usr/bin/env bash

#

source easybashgui

#

###############

#

menu "enter in Server1" "enter in Server2" "enter in Server3" "enter in Server4"

choice="$(0< "${dir_tmp}/${file_tmp}" )"

#

if [ "${choice}" = "enter in Server1" ]

    then

    #

    xterm -geometry 145x50 -bg gray -fg black -e "ssh root@192.168.1.1 -Y"

    #

elif [ "${choice}" = "enter in Server2" ]

    then

    #

    xterm -geometry 145x50 -bg lightblue -fg black -e "ssh root@192.168.1.2 -Y"

    #

elif [ "${choice}" = "enter in Server3" ]

    then

    #

    xterm -geometry 145x50 -bg pink -fg black -e "ssh root@192.168.1.3 -Y"

    #

elif [ "${choice}" = "enter in Server4" ]

    then

    #

    xterm -geometry 145x50 -bg cyan -fg black -e "ssh root@192.168.1.4 -Y"

    #

fi

It seems complete... but it still misses two important steps...

First, you have always to check if user doesn't choose anything... it means he doesn't care that menu in that moment, and wants probably exit... so...

Enter in... 5

#!/usr/bin/env bash

#

source easybashgui

#

###############

#

menu "enter in Server1" "enter in Server2" "enter in Server3" "enter in Server4"

choice="$(0< "${dir_tmp}/${file_tmp}" )"

#

if_arg_is_an_empty_variable_then_exit "choice" #bye, bye, user... :)

#

if [ "${choice}" = "enter in Server1" ]

    then

    #

    xterm -geometry 145x50 -bg gray -fg black -e "ssh root@192.168.1.1 -Y"

    #

elif [ "${choice}" = "enter in Server2" ]

    then

    #

    xterm -geometry 145x50 -bg lightblue -fg black -e "ssh root@192.168.1.2 -Y"

    #

elif [ "${choice}" = "enter in Server3" ]

    then

    #

    xterm -geometry 145x50 -bg pink -fg black -e "ssh root@192.168.1.3 -Y"

    #

elif [ "${choice}" = "enter in Server4" ]

    then

    #

    xterm -geometry 145x50 -bg cyan -fg black -e "ssh root@192.168.1.4 -Y"

    #

fi

( obviously you can check variable in any other preferred way... )

And the second step that is still missing? Don't you remember anything? ...

Enter in... 6

#!/usr/bin/env bash

#

source easybashgui

#

###############

#

menu "enter in Server1" "enter in Server2" "enter in Server3" "enter in Server4"

choice="$(0< "${dir_tmp}/${file_tmp}" )"

#

if_arg_is_an_empty_variable_then_exit "choice" #bye, bye, user... :)

#

if [ "${choice}" = "enter in Server1" ]

    then

    #

    xterm -geometry 145x50 -bg gray -fg black -e "ssh root@192.168.1.1 -Y"

    #

elif [ "${choice}" = "enter in Server2" ]

    then

    #

    xterm -geometry 145x50 -bg lightblue -fg black -e "ssh root@192.168.1.2 -Y"

    #

elif [ "${choice}" = "enter in Server3" ]

    then

    #

    xterm -geometry 145x50 -bg pink -fg black -e "ssh root@192.168.1.3 -Y"

    #

elif [ "${choice}" = "enter in Server4" ]

    then

    #

    xterm -geometry 145x50 -bg cyan -fg black -e "ssh root@192.168.1.4 -Y"

    #

fi

clean_temp

Ahhh... now you definitively got it... ;-)

* P.S.: Since 6.0.0 version, IF 'easybashlib' library is successfully loaded (if not you will have an alert message) it's NOT mandatory anymore using clean_temp() at the end of your scripts to remove tem files, since easybashlib does it automatically.

3) ...And now the *real* world : copying to a remote host with EasyBashGUI

I use this script "in real life" to copy a file/directory in a remote host ( I often use it, as multiple servers sysadmin...  ;-)  )

Open the terminal, and type:

>touch cp_to_remote_host.sh

>chmod 755 cp_to_remote_host.sh

then, open newly created file in your preferred editor ( e.g. kwrite, gedit, gvim, etc.etc. )

Cp to remote host 1

#!/usr/bin/env bash

#

#

source easybashgui

#

#########################

# Initial checks...

#

if [ -n "${TERM}" ]

    then

    if [ $(type -a "${TERM}" 2>/dev/null | wc -c) -gt 0 ]

        then

        :

    elif [ $(type -a rxvt 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="rxvt"

    elif [ $(type -a xterm 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="xterm"

    elif [ $(type -a gnome-terminal 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="gnome-terminal"

    elif [ $(type -a konsole 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="konsole"

    else

        alert_message "Err.: \n no terminal in this system ?"

        exit 1

    fi

fi

#

########

for fd in 0 1 2

    do

    if [ ! -t ${fd} ]

        then

        #

        ${TERM} -e ${0} && exit

        #

    fi

done

########

Ok. let's do two checks: first, if ${TERM} variable is already set ( and if not, let's set it ); second, since this script needs a terminal window to write password in, let's check if terminal file descriptors 0, 1, 2 (namely StdIn, StdOut, and StdErr ) are set ( if not, program relaunches recursively itself inside a terminal window and exits ).

Cp to remote host 2

#!/usr/bin/env bash

#

#

source easybashgui

#

#########################

# Initial checks...

#

if [ -n "${TERM}" ]

    then

    if [ $(type -a "${TERM}" 2>/dev/null | wc -c) -gt 0 ]

        then

        :

    elif [ $(type -a rxvt 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="rxvt"

    elif [ $(type -a xterm 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="xterm"

    elif [ $(type -a gnome-terminal 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="gnome-terminal"

    elif [ $(type -a konsole 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="konsole"

    else

        alert_message "Err.: \n no terminal in this system ?"

        exit 1

    fi

fi

#

########

for fd in 0 1 2

    do

    if [ ! -t ${fd} ]

        then

        #

        ${TERM} -e ${0} && exit

        #

    fi

done

########

#

#######################################################

#######################################################

##########

########## main

##########

#######################################################

#######################################################

#

#

clear

cat <<TITLE

    

                 $(basename "${0}" ) CONSOLE

    

    

TITLE

##

#

#

After those checks, let's clear the terminal window and write a simple "title" inside.

Cp to remote host 3

#!/usr/bin/env bash

#

#

source easybashgui

#

#########################

# Initial checks...

#

if [ -n "${TERM}" ]

    then

    if [ $(type -a "${TERM}" 2>/dev/null | wc -c) -gt 0 ]

        then

        :

    elif [ $(type -a rxvt 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="rxvt"

    elif [ $(type -a xterm 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="xterm"

    elif [ $(type -a gnome-terminal 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="gnome-terminal"

    elif [ $(type -a konsole 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="konsole"

    else

        alert_message "Err.: \n no terminal in this system ?"

        exit 1

    fi

fi

#

########

for fd in 0 1 2

    do

    if [ ! -t ${fd} ]

        then

        #

        ${TERM} -e ${0} && exit

        #

    fi

done

########

#

#

#

#

#######################################################

#######################################################

##########

########## main

##########

#######################################################

#######################################################

#

#

clear

cat <<TITLE

    

                 $(basename "${0}" ) CONSOLE

    

    

TITLE

##

#

#

menu "Use rsync ..." "Use scp ..."

choice="$( 0< "${dir_tmp}/${file_tmp}" )" ; if_arg_is_an_empty_variable_then_exit "choice"

#

if [ "${choice}" = "Use rsync ..." ]

    then

    scp_or_rsync="rsync"

    COMMAND="rsync -az -e ssh"

    #

elif [ "${choice}" = "Use scp ..." ]

    then

    scp_or_rsync="scp"

    COMMAND="scp -r"

    #

fi

#

#

if [ ${#} -ge 1 ]

    then

    how_many_args=${#}

    IFS=$'\n'

    file_or_dir_to_copy=( $(for (( num=1 ; num <= ${how_many_args} ; num++ ))

                                                       do

                                                    echo "${1}"

                                                    shift

                                            done ) )

    IFS=$' \t\n'

    #

else

    menu "Copy a file ..." "Copy a directory ..."

    c="$( 0< "${dir_tmp}/${file_tmp}" )" ; if_arg_is_an_empty_variable_then_exit "c"

    if [ "${c}" = "Copy a file ..." ]

        then

        fselect

        file_or_dir_to_copy="$(0< "${dir_tmp}/${file_tmp}" )"

        #

    elif [ "${c}" = "Copy a directory ..." ]

        then

        dselect

        file_or_dir_to_copy="$( 0< "${dir_tmp}/${file_tmp}" )"

        #

    fi

    #

fi

if_arg_is_an_empty_variable_then_exit "file_or_dir_to_copy"

Let's continue with first menu: user has to choose between "scp" and "rsync"; it could happen rsync there isn't in host system, then user will select less efficient scp. After that menu, let's check about arguments... if user already gives them at command line, let's create an array called "file_or_dir_to_copy" containing all arguments; otherwise ( no arguments ) we have to ask directly to the user 1) if he wants copy a single file or an entire directory, 2) the file name/directory name.

( Note:  EasybashGUI uses two different functions for that: for file, "fselect"; for directories, "dselect". Anyway, all these go in the same temp file: "${file_tmp}/${dir_tmp}" )

Let's put user choice in a variable called "file_or_dir_to_copy", and finally let's check if it's empty with "if_arg_is_an_empty_variable_then_exit()" function...

Cp to remote host 4

#!/usr/bin/env bash

#

#

source easybashgui

#

#########################

# Initial checks...

#

if [ -n "${TERM}" ]

    then

    if [ $(type -a "${TERM}" 2>/dev/null | wc -c) -gt 0 ]

        then

        :

    elif [ $(type -a rxvt 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="rxvt"

    elif [ $(type -a xterm 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="xterm"

    elif [ $(type -a gnome-terminal 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="gnome-terminal"

    elif [ $(type -a konsole 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="konsole"

    else

        alert_message "Err.: \n no terminal in this system ?"

        exit 1

    fi

fi

#

########

for fd in 0 1 2

    do

    if [ ! -t ${fd} ]

        then

        #

        ${TERM} -e ${0} && exit

        #

    fi

done

########

#

#

#

#

#######################################################

#######################################################

##########

########## main

##########

#######################################################

#######################################################

#

#

clear

cat <<TITLE

    

                 $(basename "${0}" ) CONSOLE

    

    

TITLE

##

#

#

menu "Use rsync ..." "Use scp ..."

choice="$( 0< "${dir_tmp}/${file_tmp}" )" ; if_arg_is_an_empty_variable_then_exit "choice"

#

if [ "${choice}" = "Use rsync ..." ]

    then

    scp_or_rsync="rsync"

    COMMAND="rsync -az -e ssh"

    #

elif [ "${choice}" = "Use scp ..." ]

    then

    scp_or_rsync="scp"

    COMMAND="scp -r"

    #

fi

#

#

if [ ${#} -ge 1 ]

    then

    how_many_args=${#}

    IFS=$'\n'

    file_or_dir_to_copy=( $(for (( num=1 ; num <= ${how_many_args} ; num++ ))

                                                       do

                                                    echo "${1}"

                                                    shift

                                            done ) )

    IFS=$' \t\n'

    #

else

    menu "Copy a file ..." "Copy a directory ..."

    c="$( 0< "${dir_tmp}/${file_tmp}" )" ; if_arg_is_an_empty_variable_then_exit "c"

    if [ "${c}" = "Copy a file ..." ]

        then

        fselect

        file_or_dir_to_copy="$(0< "${dir_tmp}/${file_tmp}" )"

        #

    elif [ "${c}" = "Copy a directory ..." ]

        then

        dselect

        file_or_dir_to_copy="$( 0< "${dir_tmp}/${file_tmp}" )"

        #

    fi

    #

fi

if_arg_is_an_empty_variable_then_exit "file_or_dir_to_copy"

#

#

#

#

#

input 3 "Remote host IP address" "192.168.1.?" "Remote user" "joe" "Remote dir in wich you want copy your choice" "/home/joe/tmp"

#

IFS=$'\n'

array=( $( 0< "${dir_tmp}/${file_tmp}" ) )

IFS=$' \t\n'

remote_ip="${array[0]}" ; if_arg_is_an_empty_variable_then_exit "remote_ip"

    {

    if [ $(echo ${remote_ip} | tr -dc [:digit:] | wc -c) -gt 7 ]

        then

        : #Don't do anything... it's ok!

    else

        #

        alert_message "Err.: \n IP address not correct !"

        exit 1

        #

    fi

    }

user="${array[1]}" ; if_arg_is_an_empty_variable_then_exit "user"

remote_dir="${array[2]}" ; if_arg_is_an_empty_variable_then_exit "remote_dir"

#

#

#

#

#

ok_message "Next step will be: \n give to \"$(basename "${0}" ) CONSOLE\" \n\n remote user \n ( \"${user}\" ) password \n in remote host \n with ip: \"${remote_ip}\" ... "

Let's continue with an "input 3".

Inputs are function that iterates text input request to user depending on number: "input 1" requests only one time, "input 2" for two times, and "input 3" for three times ( only in case EasyBashGUI is in "Xdialog" mode, tough, all input fields are displayed in the same window ).

After user fills all window input fields, user writs are stored in temp file, one line per writ... for example, if user writes at first input field: "192.168.1.1", at second input field: "root", and at third one: "/tmp"; those writs will be stored in temp file in this way:

192.168.1.1

root

/tmp

It means that thanks to IFS, you can create an array filling its elements for each row is in the file :

IFS=$'\n'

array=( $( 0< "${dir_tmp}/${file_tmp}" ) )

Ok. After array creating ( and then setting IFS to normal values ), let's set the three variables "remote_ip" "user" "remote_dir" ( checking digit quantity for remote_ip ) thanks to array, and let's inform the user that next step is write remote user password in terminal window...

Cp to remote host 5

#!/usr/bin/env bash

#

#

source easybashgui

#

#########################

# Initial checks...

#

if [ -n "${TERM}" ]

    then

    if [ $(type -a "${TERM}" 2>/dev/null | wc -c) -gt 0 ]

        then

        :

    elif [ $(type -a rxvt 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="rxvt"

    elif [ $(type -a xterm 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="xterm"

    elif [ $(type -a gnome-terminal 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="gnome-terminal"

    elif [ $(type -a konsole 2>/dev/null | wc -c) -gt 0 ]

        then

        TERM="konsole"

    else

        alert_message "Err.: \n no terminal in this system ?"

        exit 1

    fi

fi

#

########

for fd in 0 1 2

    do

    if [ ! -t ${fd} ]

        then

        #

        ${TERM} -e ${0} && exit

        #

    fi

done

########

#

#

######################################################

#####################################################################

##########

########## main

##########

######################################################################

######################################################################

#

#

clear

cat <<TITLE

    

                 $(basename "${0}" ) CONSOLE

    

    

TITLE

##

#

#

menu "Use rsync ..." "Use scp ..."

choice="$( 0< "${dir_tmp}/${file_tmp}" )" ; if_arg_is_an_empty_variable_then_exit "choice"

#

if [ "${choice}" = "Use rsync ..." ]

    then

    scp_or_rsync="rsync"

    COMMAND="rsync -az -e ssh"

    #

elif [ "${choice}" = "Use scp ..." ]

    then

    scp_or_rsync="scp"

    COMMAND="scp -r"

    #

fi

#

#

if [ ${#} -ge 1 ]

    then

    how_many_args=${#}

    IFS=$'\n'

    file_or_dir_to_copy=( $(for (( num=1 ; num <= ${how_many_args} ; num++ ))

                                                       do

                                                    echo "${1}"

                                                    shift

                                            done ) )

    IFS=$' \t\n'

    #

else

    menu "Copy a file ..." "Copy a directory ..."

    c="$( 0< "${dir_tmp}/${file_tmp}" )" ; if_arg_is_an_empty_variable_then_exit "c"

    if [ "${c}" = "Copy a file ..." ]

        then

        fselect

        file_or_dir_to_copy="$(0< "${dir_tmp}/${file_tmp}" )"

        #

    elif [ "${c}" = "Copy a directory ..." ]

        then

        dselect

        file_or_dir_to_copy="$( 0< "${dir_tmp}/${file_tmp}" )"

        #

    fi

    #

fi

if_arg_is_an_empty_variable_then_exit "file_or_dir_to_copy"

#

#

#

#

#

input 3 "Remote host IP address" "192.168.1.?" "Remote user" "joe" "Remote dir in wich you want copy your choice" "/home/joe/tmp"

#

IFS=$'\n'

array=( $( 0< "${dir_tmp}/${file_tmp}" ) )

IFS=$' \t\n'

remote_ip="${array[0]}" ; if_arg_is_an_empty_variable_then_exit "remote_ip"

    {

    if [ $(echo ${remote_ip} | tr -dc [:digit:] | wc -c) -gt 7 ]

        then

        : #Don't do anything... it's ok!

    else

        #

        alert_message "Err.: \n IP address not correct !"

        exit 1

        #

    fi

    }

user="${array[1]}" ; if_arg_is_an_empty_variable_then_exit "user"

remote_dir="${array[2]}" ; if_arg_is_an_empty_variable_then_exit "remote_dir"

#

#

#

#

#

ok_message "Next step will be: \n give to \"$(basename "${0}" ) CONSOLE\" \n\n remote user \n ( \"${user}\" ) password \n in remote host \n with ip: \"${remote_ip}\" ... "

#

##

#####

##########

#

{

eval ${COMMAND} $(for (( index=0 ; index < ${#file_or_dir_to_copy[@]} ; index++ ))

            do

            file_or_dir="${file_or_dir_to_copy[${index}]}"

            echo -n "\"${file_or_dir}\" "

        done ) ${user}@${remote_ip}:${remote_dir} 2>&1

} | tee "/tmp/${scp_or_rsync}_log.txt"

##########

#####

##

#

if [ $(tr -dc '[[:alnum:]]' 0< "/tmp/${scp_or_rsync}_log.txt" | wc -c ) -gt 0 ]

    then

    #

    alert_message "Command \"${scp_or_rsync}\" \n gave this \n error message : \n\n $( 0< "/tmp/${scp_or_rsync}_log.txt" ) \n\n\n ...Sorry  :( "

    #

else

    #

    ok_message "Success !"

    #

fi

#

rm -f "/tmp/${scp_or_rsync}_log.txt" && clean_temp

At last, we can use all data to launch ${COMMAND} ( that is scp or rsync ), write a log file, and inform the user if operation was successfull, or ( if not ) prompting him what scp or rsync complained since operation found an error...

* P.S.: Since 6.0.0 version, IF 'easybashlib' library is successfully loaded (if not you will have an alert message) it's NOT mandatory anymore using clean_temp() at the end of your scripts to remove tem files, since easybashlib does it automatically.