I am creating an AWS distributor package to modify the syslog configuration on different flavors of Linux servers. I wrote a few simple bash scripts to handle the install, uninstall, and update. I thought this would be universal. However, today I found out that AWS uses sh
to execute the script (i.e. sh install.sh
) and on Ubuntu/Debian this defaults to DASH and not BASH. For that reason my scripts fail with:
update errors: update.sh: 3: update.sh: function: not found
update.sh: 9: update.sh: Syntax error: "}" unexpected
failed to run commands: exit status 2
Failed to install package; install status Failed
I can't change the AWS behavior. And I don't want to modify any configurations on the server. So, it would appear my only options are that which modify the script file itself?
The first preference would be to make the script universal so it can run on any flavor of Linux. The second preference would be to create a different script for Debian that is DASH compatible.
Unfortunately, I don't find much information about dash programming. Any suggestions?
The 3 scripts in question are....
install.sh
#!/bin/bash
function isInstalled {
if sudo ls /etc/rsyslog.d/xdr.conf >/dev/null 2>&1; then
true
else
false
fi
}
if isInstalled; then
exit 0
else
sudo cp xdr.conf /etc/rsyslog.d/xdr.conf
sudo service rsyslog restart
fi
uninstall.sh
#!/bin/bash
function isInstalled {
if sudo ls /etc/rsyslog.d/xdr.conf >/dev/null 2>&1; then
true
else
false
fi
}
if isInstalled; then
sudo rm /etc/rsyslog.d/xdr.conf
sudo service rsyslog restart
else
exit 0
fi
update.sh
#!/bin/bash
function isDiff {
if sudo diff xdr.conf /etc/rsyslog.d/xdr.conf >/dev/null 2>&1; then
false
else
true
fi
}
if isDiff; then
sudo cp xdr.conf /etc/rsyslog.d/xdr.conf
sudo service rsyslog restart
else
exit 0
fi
There are two different function syntaxes in bash/ksh/zsh. You're using the ksh style:
function name { … }
. The other form is justname() { … }
and is the form introduced late in the original Bourne shell.It really is pointless to do:
Apart from turning all non-zero return statuses to 1, this is just the same as just running
cmd
. And with the function reduced to just one line, you really don't need a function. I'd also be tempted to usecmp -s
instead ofdiff
or to simply do the copy unconditionally. And don't usels
to test for the existence of a file. Either thetest
command or the[
is intended for that purpose:[ -e /etc/rsyslog.d/xdr.conf ]
. That does exist in external form if you havesudo
.