bash 在shell脚本中写入日志文件的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18278223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
method to write in log files in a shell script
提问by Jorge Vega Sánchez
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
我正在开发几个 shell 脚本并希望添加一个简单的日志记录方法。但到目前为止,创建的函数只打印第一条日志行。我头疼得厉害,因为我不明白问题出在哪里
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 reperttheitroades pour les réperttheitroades d'export.
mkdir -p acharger
mkdir -p archives
logit "repImport correctement crée."
}
repExport()
{
# création des reperttheitroades pour les réperttheitroades 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
#------------------
# Obligattheitroade créer avec l'user root le réperttheitroade /home/pepsicash, après changer le propietaire pour cash --> root /> chown cash.mersi pepsicash/
# deuxième partir (réperttheitroades pour les fichiers)
#----------------------------------------------------
echo "Création des réperttheitroades 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éperttheitroades sur Exports"
cd exports
repExport
cd ..
logit "Création des réperttheitroades sur Imports"
cd imports
for index in ${!array[*]}
do
if [ $index -eq 0 ];
then
if [ -d ${array[$index]} ]; then
logit "El réperttheitroade ENCAISSEMENTS existe déjà."
else
logit "Le réperttheitroade n'existe pas."
mkdir -p ENCAISSEMENTS
logit "Réperttheitroade ENCAISSEMENTS crée correctement."
fi
cd ENCAISSEMENTS
repImport
logit "Réperttheitroade ENCAISSEMENTS crée, CP -> BNP_CB "
cd ..
if [ -d ${array[$index]} ]; then
logit "El réperttheitroade IMPAYES existe déjà."
else
logit "Le réperttheitroade n'existe pas."
mkdir -p IMPAYES
logit "Réperttheitroade IMPAYES crée correctement."
fi
cd IMPAYES
repImport
logit "Réperttheitroade 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éperttheitroade existe déjà."
else
logit "Le réperttheitroade n'existe pas."
mkdir -p ${array[$index]}
logit "Réperttheitroade ${array[$index]} crée correctement."
fi
cd ${array[$index]}
repImport
cd ..
done
echo "Fin création des réperttheitroades."
The logfile only contains the first logit
call. Why don't the other calls to the method work?
日志文件只包含第一个logit
调用。为什么对该方法的其他调用不起作用?
[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?
在网上搜索我看到我可以使用 logger 命令,但它总是写在/var/ folder
. 是否可以调整我的 shell 函数以使用记录器并写入当前脚本文件夹中的文件?
回答by inthenite
What about if you use an absolute route for the variable LOG_FILE.
如果您对变量 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"
回答by Jonathan Leffler
When I run your code a few times, I get output from creation.log
such as:
当我运行你的代码几次时,我得到如下输出creation.log
:
[jleffler][Fri Aug 16 13:53:56 PDT 2013] - Array généré
[jleffler][Fri Aug 16 13:53:56 PDT 2013] - Le réperttheitroade n'existe pas.
[jleffler][Fri Aug 16 13:53:56 PDT 2013] - Réperttheitroade 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éperttheitroade n'existe pas.
[jleffler][Fri Aug 16 13:54:37 PDT 2013] - Réperttheitroade 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éperttheitroade n'existe pas.
[jleffler][Fri Aug 16 13:54:44 PDT 2013] - Réperttheitroade 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éperttheitroade 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:
对于最后一次运行,我在mkdir BNP_CB
再次运行命令之前运行。我稍微作弊并cat $LOG_FILE
在脚本中安排:
#!/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éperttheitroade ENCAISSEMENTS existe déjà."
else
logit "Le réperttheitroade n'existe pas."
mkdir -p ENCAISSEMENTS
logit "Réperttheitroade 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).
您有一些问题需要解决,为什么在只处理索引 0 时列出数组中的所有项目,以及为什么在 BNP_CB 不存在时创建 ENCAISSEMENTS,等等,但这些问题与日志记录无关工作(就像对我一样)或不工作(因为它不适合你)。
What happens for you when you run bash -x yourscript.sh
? Does that show what is happening?
你跑步时会发生什么bash -x yourscript.sh
?这是否表明正在发生的事情?