Reputation: 21
Tried looking this up, but don't know what I'm looking for.
I have 4 servers that are keyed to a license file, so the hostnames have to match. I have 4 license files, and I am running this through Opsworks, so hands off installs. I have an if in a bash that appears to be kicking off an unexpected end of file.
if [ "$HOSTNAME" = devprint01 ]; then
cp /home/user/installs/config/linux.cfg.01 /var//filters/linux.cfg
if [ "$HOSTNAME" = devoprint01 ]; then
cp /home/user/installs/config/linux.cfg.d01 /var//filters/linux.cfg
if [ "$HOSTNAME" = devoprint02 ]; then
cp /home/user/installs/config/linux.cfg.d02 /var//filters/linux.cfg
if [ "$HOSTNAME" = priprint01 ]; then
cp /home/user/installs/config/linux.cfg.p01 /var//filters/linux.cfg
if [ "$HOSTNAME" = priprint02 ]; then
cp /home/user/installs/config/linux.cfg.p02 /var//filters/linux.cfg
fi
There is a bunch of cp and other commands in front of this that are working fine, so it has to be something I am missing on this. Should there be something in between each if statement, or should I fi between each one?
Upvotes: 0
Views: 279
Reputation: 20012
You can always consider a function. Using a function makes it easier to use the code on different places (an array will help to).
When there is some algorithm, you can use code for the translation. That way you can also see possible mistakes or add hosts later:
getcfg() {
echo "$1" | sed -r 's/devprint//;s/(d|p).*print/\1/'
}
/home/user/installs/config/linux.cfg.$(getcfg ${HOSTNAME}) /var/filters/linux.cfg
# Or test it with
for testhostname in devprint01 devoprint01 devoprint02 priprint01 priprint02; do
echo "${testhostname} => $(getcfg ${testhostname}) "
done
Upvotes: 0
Reputation: 241928
You are missing fi
for the first four ifs.
If your bash is recent enough (4.0+), you can use an associative array instead:
declare -A suffix=([devprint01]=01
[devoprint01]=d01
[devoprint02]=d02
[priprint01]=p01
[priprint02]=p02
)
cp /home/user/installs/config/linux.cfg.${suffix[$HOSTNAME]} /var/filters/linux.cfg
Upvotes: 1