简体   繁体   English

Bash / Dash:进程在“ If if else else”语句中被“ fi”杀死吗?

[英]Bash/Dash: Process killed on “fi” in an “If then else” statement?

I have the following script which is creating SQL dumps out of my mysql database. 我有以下脚本正在从我的mysql数据库中创建SQL转储。 The files are then historized by git. 然后通过git对文件进行历史化。 In this script I encounter a strange behaviour which I am -even with some research- not able to answer. 在此脚本中,我遇到了一种奇怪的行为,即使经过一些研究也无法回答。

If I run the script I receive an error saying that in line 25 (which is the "fi" statement) the process "/usr/bin/git commit -m "$NOW" is terminated: 如果我运行脚本,则会收到一条错误消息,指出在第25行(这是“ fi”语句),进程“ / usr / bin / git commit -m“ $ NOW”被终止:

xxx@xxx:/var/log# /opt/mysql_backup.sh 
[master 12fd643] 2016-12-29
/opt/mysql_backup.sh: Line 25: 11163 Killed                /usr/bin/git commit -m "$NOW"

While debugging I commented out the if-then-else statement which checks for presence of git and the scipts runs without an error. 在调试时,我注释掉了if-then-else语句,该语句检查git的存在,并且scipts正常运行。

My Question is: Why does dash "terminate" the git command at the "fi" statement? 我的问题是:为什么在“ fi”语句中破折号“终止” git命令? In fact the commit is performed so I am wondering what the reason for the error message might be. 实际上执行了提交,所以我想知道错误消息的原因可能是什么。 The script is running on dash on Raspbian Jessie. 该脚本在Raspbian Jessie上的破折号上运行。

Thanks in advance! 提前致谢!

Joerg 约尔格

Script: /opt/mysql_backup.sh 脚本:/opt/mysql_backup.sh

#!/bin/dash
# TARGET: Backup-Ziel
# IGNORE: Liste zu ignorierender Datenbanken (durch | getrennt)
# CONF: MySQL Config-Datei, welche die Zugangsdaten enthaelt
TARGET=/srv_ext/z_backup/mysql
IGNORE="phpmyadmin|mysql|information_schema|performance_schema|test"
CONF=/etc/mysql/debian.cnf
if [ ! -r $CONF ]; then /usr/bin/logger "$0 - auf $CONF konnte nicht zugegriffen werden"; exit 1; fi
if [ ! -d $TARGET ] || [ ! -w $TARGET ]; then /usr/bin/logger "$0 - Backup-Verzeichnis nicht beschreibbar"; exit 1; fi

DBS="$(/usr/bin/mysql --defaults-extra-file=$CONF -Bse 'show databases' | /bin/grep -Ev $IGNORE)"
NOW=$(date +"%Y-%m-%d")

for DB in $DBS; do
    /usr/bin/mysqldump --defaults-extra-file=$CONF --skip-extended-insert --skip-comments $DB > $TARGET/$DB.sql
done

if [ -x /usr/bin/git ] && [ -d ${TARGET}/.git/branches ]
then
  cd $TARGET
  /usr/bin/git add .
  /usr/bin/git commit -m "$NOW"
else
  /usr/bin/logger "$0 - git nicht verfuegbar oder Backup-Ziel nicht unter Versionskontrolle"
fi

/usr/bin/logger "$0 - Backup von $NOW erfolgreich durchgefuehrt"
exit 0

Output of run with "set -x": 使用“ set -x”运行的输出:

xxx@xxx:/opt# ./mysql_backup.sh 
+ TARGET=/srv_ext/z_backup/mysql
+ IGNORE=phpmyadmin|mysql|information_schema|performance_schema|test
+ CONF=/etc/mysql/debian.cnf
+ [ ! -r /etc/mysql/debian.cnf ]
+ [ ! -d /srv_ext/z_backup/mysql ]
+ [ ! -w /srv_ext/z_backup/mysql ]
+ /usr/bin/mysql --defaults-extra-file=/etc/mysql/debian.cnf -Bse show databases
+ /bin/grep -Ev phpmyadmin|mysql|information_schema|performance_schema|test
+ DBS=c_db
s1_db
s2_db
zabbix
+ date +%Y-%m-%d
+ NOW=2016-12-29
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments c_db
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments s1_db
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments s2_db
+ /usr/bin/mysqldump --defaults-extra-file=/etc/mysql/debian.cnf --skip-extended-insert --skip-comments zabbix
+ [ -x /usr/bin/git ]
+ [ -d /srv_ext/z_backup/mysql/.git/branches ]
+ cd /srv_ext/z_backup/mysql
+ /usr/bin/git add .
+ /usr/bin/git commit -m 2016-12-29
[master b8952b9] 2016-12-29
Killed
+ /usr/bin/logger ./mysql_backup.sh - Backup von 2016-12-29 erfolgreich durchgefuehrt
+ exit 0

Of course the raspberry has just 1G and I did not enabled swap due to wearleveling of the SD memory card. 当然,树莓只有1G,由于SD存储卡的耗损平衡,我没有启用交换功能。 With swap its working! 随着交换其工作! – Joerg –约尔格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM