sh: Make use of bashisms in bash scripts

Both [[ ]] and (( )) bashisms are better than the alternative
in POSIX sh, since they are builtin and don't suffer from quoting
or number-of-args issues.

Signed-off-by: Manuel Mendez <mmendez@equinix.com>
This commit is contained in:
Manuel Mendez 2021-04-27 17:39:50 +00:00
parent b8d94f5278
commit 0fff3e6d7f
3 changed files with 22 additions and 22 deletions

View File

@ -34,7 +34,7 @@ setup_nat() (
main() ( main() (
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
if [ ! -f ./.env ]; then if ! [[ -f ./.env ]]; then
./generate-env.sh eth1 >.env ./generate-env.sh eth1 >.env
fi fi

View File

@ -14,7 +14,7 @@ ERR="${RED:-}ERROR:${RESET:-}"
source ./current_versions.sh source ./current_versions.sh
err() ( err() (
if [ -z "${1:-}" ]; then if [[ -z ${1:-} ]]; then
cat >&2 cat >&2
else else
echo "$ERR " "$@" >&2 echo "$ERR " "$@" >&2
@ -94,7 +94,7 @@ EOF
) )
main() ( main() (
if [ -z "${1:-}" ]; then if [[ -z ${1:-} ]]; then
err "Usage: $0 network-interface-name > .env" err "Usage: $0 network-interface-name > .env"
exit 1 exit 1
fi fi

View File

@ -38,7 +38,7 @@ NEXT="${GREEN:-}NEXT:${RESET:-}"
get_distribution() ( get_distribution() (
local lsb_dist="" local lsb_dist=""
# Every system that we officially support has /etc/os-release # Every system that we officially support has /etc/os-release
if [ -r /etc/os-release ]; then if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091 # shellcheck disable=SC1091
lsb_dist="$(. /etc/os-release && echo "$ID")" lsb_dist="$(. /etc/os-release && echo "$ID")"
fi fi
@ -50,7 +50,7 @@ get_distribution() (
get_distro_version() ( get_distro_version() (
local lsb_version="0" local lsb_version="0"
# Every system that we officially support has /etc/os-release # Every system that we officially support has /etc/os-release
if [ -r /etc/os-release ]; then if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091 # shellcheck disable=SC1091
lsb_version="$(. /etc/os-release && echo "$VERSION_ID")" lsb_version="$(. /etc/os-release && echo "$VERSION_ID")"
fi fi
@ -112,10 +112,10 @@ setup_networking() (
fi fi
NAT_INTERFACE="" NAT_INTERFACE=""
if [ -r .nat_interface ]; then if [[ -r .nat_interface ]]; then
NAT_INTERFACE=$(cat .nat_interface) NAT_INTERFACE=$(cat .nat_interface)
fi fi
if [ -n "$NAT_INTERFACE" ] && ip addr show "$NAT_INTERFACE" &>/dev/null; then if [[ -n $NAT_INTERFACE ]] && ip addr show "$NAT_INTERFACE" &>/dev/null; then
# TODO(nshalman) the terraform code would just run these commands as-is once # TODO(nshalman) the terraform code would just run these commands as-is once
# but it would be nice to make these more persistent based on OS # but it would be nice to make these more persistent based on OS
iptables -A FORWARD -i "$TINKERBELL_NETWORK_INTERFACE" -o "$NAT_INTERFACE" -j ACCEPT iptables -A FORWARD -i "$TINKERBELL_NETWORK_INTERFACE" -o "$NAT_INTERFACE" -j ACCEPT
@ -135,10 +135,10 @@ setup_networking_manually() (
setup_network_forwarding() ( setup_network_forwarding() (
# enable IP forwarding for docker # enable IP forwarding for docker
if [ "$(sysctl -n net.ipv4.ip_forward)" != "1" ]; then if (($(sysctl -n net.ipv4.ip_forward) != 1)); then
if [ -d /etc/sysctl.d ]; then if [[ -d /etc/sysctl.d ]]; then
echo "net.ipv4.ip_forward=1" >/etc/sysctl.d/99-tinkerbell.conf echo "net.ipv4.ip_forward=1" >/etc/sysctl.d/99-tinkerbell.conf
elif [ -f /etc/sysctl.conf ]; then elif [[ -f /etc/sysctl.conf ]]; then
echo "net.ipv4.ip_forward=1" >>/etc/sysctl.conf echo "net.ipv4.ip_forward=1" >>/etc/sysctl.conf
fi fi
@ -171,7 +171,7 @@ setup_networking_netplan() (
) )
setup_networking_ubuntu_legacy() ( setup_networking_ubuntu_legacy() (
if [ ! -f /etc/network/interfaces ]; then if ! [[ -f /etc/network/interfaces ]]; then
echo "$ERR file /etc/network/interfaces not found" echo "$ERR file /etc/network/interfaces not found"
exit 1 exit 1
fi fi
@ -224,7 +224,7 @@ EOF
local cfgfile="/etc/sysconfig/network-scripts/ifcfg-$TINKERBELL_NETWORK_INTERFACE" local cfgfile="/etc/sysconfig/network-scripts/ifcfg-$TINKERBELL_NETWORK_INTERFACE"
if [ -f "$cfgfile" ]; then if [[ -f $cfgfile ]]; then
echo "$ERR network config already exists: $cfgfile" echo "$ERR network config already exists: $cfgfile"
echo "$BLANK Please update it to match this configuration:" echo "$BLANK Please update it to match this configuration:"
echo "$content" echo "$content"
@ -245,12 +245,12 @@ setup_osie() (
local osie_current=$STATEDIR/webroot/misc/osie/current local osie_current=$STATEDIR/webroot/misc/osie/current
local tink_workflow=$STATEDIR/webroot/workflow/ local tink_workflow=$STATEDIR/webroot/workflow/
if [ ! -d "$osie_current" ] || [ ! -d "$tink_workflow" ]; then if [[ ! -d $osie_current ]] || [[ ! -d $tink_workflow ]]; then
mkdir -p "$osie_current" mkdir -p "$osie_current"
mkdir -p "$tink_workflow" mkdir -p "$tink_workflow"
pushd "$SCRATCH" pushd "$SCRATCH"
if [ -z "${TB_OSIE_TAR:-}" ]; then if [[ -z ${TB_OSIE_TAR:-} ]]; then
curl "${OSIE_DOWNLOAD_LINK}" -o ./osie.tar.gz curl "${OSIE_DOWNLOAD_LINK}" -o ./osie.tar.gz
tar -zxf osie.tar.gz tar -zxf osie.tar.gz
else else
@ -305,7 +305,7 @@ check_container_status() (
--filter "event=health_status" \ --filter "event=health_status" \
--format '{{.Status}}') --format '{{.Status}}')
if [ "$status" != "health_status: healthy" ]; then if [[ $status != "health_status: healthy" ]]; then
echo "$ERR $container_name is not healthy. status: $status" echo "$ERR $container_name is not healthy. status: $status"
exit 1 exit 1
fi fi
@ -314,7 +314,7 @@ check_container_status() (
generate_certificates() ( generate_certificates() (
mkdir -p "$STATEDIR/certs" mkdir -p "$STATEDIR/certs"
if [ ! -f "$STATEDIR/certs/ca.json" ]; then if ! [[ -f "$STATEDIR/certs/ca.json" ]]; then
jq \ jq \
'. '.
| .names[0].L = $facility | .names[0].L = $facility
@ -325,7 +325,7 @@ generate_certificates() (
>"$STATEDIR/certs/ca.json" >"$STATEDIR/certs/ca.json"
fi fi
if [ ! -f "$STATEDIR/certs/server-csr.json" ]; then if ! [[ -f "$STATEDIR/certs/server-csr.json" ]]; then
jq \ jq \
'. '.
| .hosts += [ $ip, "tinkerbell.\($facility).packet.net" ] | .hosts += [ $ip, "tinkerbell.\($facility).packet.net" ]
@ -353,7 +353,7 @@ generate_certificates() (
# update host to trust registry certificate # update host to trust registry certificate
if ! cmp --quiet "$STATEDIR/certs/ca.pem" "$certs_dir/tinkerbell.crt"; then if ! cmp --quiet "$STATEDIR/certs/ca.pem" "$certs_dir/tinkerbell.crt"; then
if [ ! -d "$certs_dir/tinkerbell.crt" ]; then if ! [[ -d "$certs_dir/" ]]; then
# The user will be told to create the directory # The user will be told to create the directory
# in the next block, if copying the certs there # in the next block, if copying the certs there
# fails. # fails.
@ -363,7 +363,7 @@ generate_certificates() (
echo "$ERR please copy $STATEDIR/certs/ca.pem to $certs_dir/tinkerbell.crt" echo "$ERR please copy $STATEDIR/certs/ca.pem to $certs_dir/tinkerbell.crt"
echo "$BLANK and run $0 again:" echo "$BLANK and run $0 again:"
if [ ! -d "$certs_dir" ]; then if ! [[ -d $certs_dir ]]; then
echo "sudo mkdir -p '$certs_dir'" echo "sudo mkdir -p '$certs_dir'"
fi fi
echo "sudo cp '$STATEDIR/certs/ca.pem' '$certs_dir/tinkerbell.crt'" echo "sudo cp '$STATEDIR/certs/ca.pem' '$certs_dir/tinkerbell.crt'"
@ -406,7 +406,7 @@ bootstrap_docker_registry() (
setup_docker_registry() ( setup_docker_registry() (
local registry_images="$STATEDIR/registry" local registry_images="$STATEDIR/registry"
if [ ! -d "$registry_images" ]; then if ! [[ -d $registry_images ]]; then
mkdir -p "$registry_images" mkdir -p "$registry_images"
fi fi
start_registry start_registry
@ -469,7 +469,7 @@ check_prerequisites() (
;; ;;
esac esac
if [ $failed -eq 1 ]; then if ((failed == 1)); then
echo "$ERR Prerequisites not met. Please install the missing commands and re-run $0." echo "$ERR Prerequisites not met. Please install the missing commands and re-run $0."
exit 1 exit 1
fi fi
@ -489,7 +489,7 @@ do_setup() (
echo "$INFO starting tinkerbell stack setup" echo "$INFO starting tinkerbell stack setup"
check_prerequisites "$lsb_dist" "$lsb_version" check_prerequisites "$lsb_dist" "$lsb_version"
if [ ! -f "$ENV_FILE" ]; then if ! [[ -f $ENV_FILE ]]; then
echo "$ERR Run './generate-env.sh network-interface > \"$ENV_FILE\"' before continuing." echo "$ERR Run './generate-env.sh network-interface > \"$ENV_FILE\"' before continuing."
exit 1 exit 1
fi fi