Jorge Vega Sánchez
Jorge Vega Sánchez

Reputation: 7590

method to write in log files in a shell script

I'm developing a couple of shell scripts and want to add an easy logging method. But till now the the function created only print the first log line. I have a terrible headache cause I cannot understand where is the problem

The logging functions is so easy to develop and to understand:

LOG_FILE="creation.log"

logit() 
{
    echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}

And some lines of the script.

    LOG_FILE="creation.log"

logit() 
{
    echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}

repImport()
{
    # création des repertoires pour les répertoires d'export.
    mkdir -p acharger
    mkdir -p archives

    logit "repImport correctement crée."
}

repExport()
{
    # création des repertoires pour les répertoires d'import.
    mkdir -p archives
    mkdir -p atraiter

    logit "repExport correctement crée."
}

array=(BNP_CB collection_XTL collection_QT sp xcl xtl pgi qnt visualr)

logit "Array généré"

REP_TEST="/tmp/jorge/modules"
REP=${REP_TEST}        # Descomentar para hacer test.

REP_LIV_VX="/tmp/jorge/modules/"


# NOTE IMPORTANTE
#------------------
# Obligatoire créer avec l'user root le répertoire /home/pepsicash, après changer le propietaire pour cash --> root /> chown cash.mersi pepsicash/


# deuxième partir (répertoires pour les fichiers)
#----------------------------------------------------
echo "Création des répertoires pour les fichiers"
mkdir -p "${REP}""/journaux"    # originel: /home/pepsicash/journaux
mkdir -p "${REP}""/data"        # originel: /home/pepsicash/data

cd $REP/data            # originel: /home/pepsicash/data    
mkdir -p imports
mkdir -p exports
mkdir -p tmpcft

logit "Création des répertoires sur Exports"
cd exports
repExport
cd ..

logit "Création des répertoires sur Imports"
cd imports

for index in ${!array[*]}
do

    if [ $index -eq 0 ]; 
    then
        if [ -d ${array[$index]} ]; then
            logit "El répertoire ENCAISSEMENTS existe déjà."
        else
            logit "Le répertoire n'existe pas."
            mkdir -p ENCAISSEMENTS
            logit "Répertoire ENCAISSEMENTS crée correctement."
        fi

        cd ENCAISSEMENTS
        repImport
        logit "Répertoire ENCAISSEMENTS crée, CP -> BNP_CB "
        cd ..

        if [ -d ${array[$index]} ]; then
            logit "El répertoire IMPAYES existe déjà."
        else
            logit "Le répertoire n'existe pas."
            mkdir -p IMPAYES
            logit "Répertoire IMPAYES crée correctement."
        fi

        cd IMPAYES
        repImport
        logit "Répertoire ENCAISSEMENTS crée, CP -> BNP_CB "
        cd ..

        logit "Case particulier BNP_CB crée."

        continue
    fi

    if [ -d ${array[$index]} ]; then
        # Control will enter here if $DIRECTORY exists.
        logit "El répertoire existe déjà."
    else
        logit "Le répertoire n'existe pas."
        mkdir -p ${array[$index]}
        logit "Répertoire ${array[$index]} crée correctement."
    fi

    cd ${array[$index]}

    repImport
    cd ..
done

echo "Fin création des répertoires."

The logfile only contains the first logit call. Why don't the other calls to the method work?

[JV07483S][Fri Aug 16 18:19:04 CEST 2013] - Array généré
[JV07483S][Fri Aug 16 18:19:30 CEST 2013] - Array généré

Searching in the web I saw that I can use the logger command, but it always write in the /var/ folder. Is it possible to adapt my shell function to use logger and to write in a file at the current script's folder?

Upvotes: 1

Views: 25468

Answers (2)

inthenite
inthenite

Reputation: 116

What about if you use an absolute route for the variable LOG_FILE.

Every time you change the directory, you are changing where are you writing.

Edit: you can create the variable like this, if you want the log file in the directory you are executing the script:

LOG_FILE="`pwd`/creation.log"

Upvotes: 3

Jonathan Leffler
Jonathan Leffler

Reputation: 753465

When I run your code a few times, I get output from creation.log such as:

[jleffler][Fri Aug 16 13:53:56 PDT 2013] - Array généré
[jleffler][Fri Aug 16 13:53:56 PDT 2013] - Le répertoire n'existe pas.
[jleffler][Fri Aug 16 13:53:56 PDT 2013] - Répertoire ENCAISSEMENTS crée correctement.
[jleffler][Fri Aug 16 13:54:37 PDT 2013] - Array généré
[jleffler][Fri Aug 16 13:54:37 PDT 2013] - Le répertoire n'existe pas.
[jleffler][Fri Aug 16 13:54:37 PDT 2013] - Répertoire ENCAISSEMENTS crée correctement.
[jleffler][Fri Aug 16 13:54:44 PDT 2013] - Array généré
[jleffler][Fri Aug 16 13:54:44 PDT 2013] - Le répertoire n'existe pas.
[jleffler][Fri Aug 16 13:54:44 PDT 2013] - Répertoire ENCAISSEMENTS crée correctement.
[jleffler][Fri Aug 16 13:54:58 PDT 2013] - Array généré
[jleffler][Fri Aug 16 13:54:58 PDT 2013] - El répertoire ENCAISSEMENTS existe déjà.

For the last run,, I ran mkdir BNP_CB before running the command again. I cheated slightly and arranged to cat $LOG_FILE within the script:

#!/bin/bash

LOG_FILE="creation.log"

logit() 
{
    echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
}

array=(BNP_CB cashcollection_EXTELIA cashcollection_QENAT espace excel extelia pgi qenat visualr)

logit "Array généré"

for index in ${!array[*]}
do
    if [ $index -eq 0 ];
    then
        if [ -d ${array[$index]} ]; then
            logit "El répertoire ENCAISSEMENTS existe déjà."
        else
            logit "Le répertoire n'existe pas."
            mkdir -p ENCAISSEMENTS
            logit "Répertoire ENCAISSEMENTS crée correctement."
        fi
    fi
done

cat $LOG_FILE

You have some issues to resolve around why list all the items in the array when you're only going to process the index 0, and why create ENCAISSEMENTS when BNP_CB doesn't exist, and so on, but those issues are tangential to the logging working (as it does for me) or not (as it doesn't for you).

What happens for you when you run bash -x yourscript.sh? Does that show what is happening?

Upvotes: 1

Related Questions