Home > Net >  bad substitution bash equivalent in sh
bad substitution bash equivalent in sh

Time:10-23

I have this script in Bash :

for conffile in $(ls -1 ${KSCFGISOLINUXFILESLOCATION}); do
  for variable in CFG_CRYPT CFG_DISK CFG_DNS CFG_DOMAIN CFG_GATEWAY CFG_HOSTNAME CFG_IFACE CFG_IP CFG_NETMASK CFG_USERKEY CFG_ACTION CFG_TITLE CFG_INITURL CFG_OSVERS CFG_ISOBASE CFG_ISOPATH CFG_ISOPATH_SIMPLE SHI_BOOT_URL DEBUG UUID LOGDIR WWWDIR OSINITDIR WKGDIR; do
    SUBSTITUTIONCOUNT=$( grep -c "%%${variable}%%" ${conffile} | cut -f1 -d" " )
    if [ ${SUBSTITUTIONCOUNT} -gt 0 ]; then
      echo "Modifying ${variable} in ${SUBSTITUTIONCOUNT} places" 
      echo "Updating ${variable} in ${conffile} to ${-variable}" 
      sed -i "s^%%${variable}%%^${-variable}^g" ${conffile} 2>&1
    fi
  done
done

and when I ran it in a sh shell it throws the following error:

/tmp/wf_script-JI1La1/rhel84isoinstall.sh: 158: /tmp/wf_script-JI1La1/rhel84isoinstall.sh: Bad substitution

The problem is that I'm trying to use a bash command in a sh shell

What would be the equivalent of ${!variable} in sh ?

I tried ${-variable} but it was a bad command.

CodePudding user response:

Sh and Dash don't support this feature.

Make sure the script always runs with bash by seeing the shebang correctly and avoiding sh myscript (see Why does my bash code fail when I run it with sh?)

Alternatively, rewrite it to sh using eval, but be careful about code injection:

#!/bin/sh
name=PATH
eval "echo \"\$$name\""
  • Related