When installing, command in `postinst` always results in `$?` = `0`
I have a postinst
script in my debian package:
#!/bin/bash
oqm-config -g system.hostname
RESULT="$?"
if [ "$RESULT" -eq 1 ]; then
oqm-config -s system.hostname $(hostname).local "."
fi
This is intended to populate a config variable (if it doesn’t exist). These commands function outside of the install process:
$ sudo ./test.sh
+ oqm-config -g system.hostname
ERROR: Config key not found: system.hostname
+ RESULT=1
+ [ 1 -eq 1 ]
+ hostname
+ oqm-config -s system.hostname oqm-demo.local .
{
"system": {
"hostname": "oqm-demo.local"
},
"captain": {},
"snapshots": {
"location": "/data/oqm-snapshots/",
"numToKeep": 5,
"frequency": "weekly"
}
}
However, when executed during install, the return code from the initial oqm-config -g system.hostname
is always 0
(or more accurately empty string? unsure why this particular error from an empty string, should be 0
?):
Setting up open+quarter+master-manager-station+captain (1.0.18) ...
+ oqm-config -g system.hostname
ERROR: Config key not found: system.hostname
+ RESULT=0
+ '[' '' -eq 1 ']'
/var/lib/dpkg/info/open+quarter+master-manager-station+captain.postinst: line 5: [: : integer exp
ression expected
Am I missing something?
Building the debian file on Ubuntu 20.04, attempting install on 22.04
Debian file: https://github.com/Epic-Breakfast-Productions/OpenQuarterMaster/releases/tag/manager-station%2Bcaptain-1.0.18
File that makes the debian (I know it’s messy, but works [usually]): https://github.com/Epic-Breakfast-Productions/OpenQuarterMaster/blob/dev.388-fr-finish-keycloak-infra-component/software/Station-Captain/makeInstallers.sh#L107-L117
The problem is this:
cat <<EOT >> "$buildDir/$debDir/DEBIAN/postinst"
#!/bin/bash
oqm-config -g system.hostname
RESULT=$?
if [ $RESULT -eq 1 ]; then
oqm-config -s system.hostname $(hostname).local "."
fi
EOT
Bash performs parameter expansion, among other things, in heredocs:
If
word
is unquoted, all lines of the here-document are subjected to
parameter expansion, command substitution, and arithmetic expansion,
the character sequencenewline
is ignored, and ‘’ must be used
to quote the characters ‘’, ‘
$
’, and ‘`
’.
(Here, word
is the delimiter for the heredoc, EOT
in this case.) That means that $?
and $RESULT
are expanded, and the heredoc writes the result of the expansion into the final input. You’re relying on this behaviour further down the script.
You can see this in the final postinst
file:
$ curl -sL https://github.com/Epic-Breakfast-Productions/OpenQuarterMaster/releases/download/manager-station%2Bcaptain-1.0.18/open+quarter+master-manager-station+captain_1.0.18_all.deb | dpkg-deb --ctrl-tarfile - | tar -xO ./postinst
#!/bin/bash
set -x
oqm-config -g system.hostname
RESULT="0"
if [ "" -eq 1 ]; then
oqm-config -s system.hostname oqm-dev.local "."
fi
# /usr/share/update-notifier/notify-reboot-required
Quote the delimiter to prevent this from happening:
cat <<'EOT' > "$buildDir/$debDir/DEBIAN/postinst"
#!/bin/bash
oqm-config -g system.hostname
RESULT=$?
if [ $RESULT -eq 1 ]; then
oqm-config -s system.hostname $(hostname).local "."
fi
EOT