0

I am new to writing code/scripts, please forgive me if this is a question that is simple. I have searched the site and not found the answer to fix my problem. I am trying to use $SerialOutput in the rest of the script but it is coming back {}. When I do a shellcheck on it it states the following

In Output_Test.sh line 12:
SerialOutput="$SerialOutput\n v$version $serial"
^-- SC2030: Modification of SerialOutput is local (to subshell caused by pipeline).



 In Output_Test.sh line 14:
  echo "{$SerialOutput}"
         ^-- SC2031: SerialOutput was modified in a subshell. That change might be lost.

Can anyone help clarify what I am doing wrong? Below is the script in question.

#!/bin/sh


if [ -d "/Library/Preferences/VMware Fusion/" ] ; then
 result=$(grep -iH Serial /Library/Preferences/VMware\ Fusion/license* | awk '{print $2 $4}' | sed 's/"//g')
  echo "$result" | while read -r a;
  do
    version=$(echo "$a" | cut -d "-" -f 3)
    serial=$(echo "$a" | cut -d ":" -f 2 | sed 's/Serial//g')
    SerialOutput="$SerialOutput\n v$version $serial"
  done
    else
  echo "{NA}"
fi
echo "{$SerialOutput}"
  • 2
    Stuart, in addition to checking out the answers in the duplicate link above, note that your shebang is `#!/bin/sh`, so even if your shell is bash, you are running in POSIX compatibility mode. If you're interested in portability to non-bash shells, it would be good to remove the tag. Otherwise, you should specify bash within your script so that you can take advantage of features that will let you solve this problem easily. – ghoti Jul 19 '16 at 17:43
  • See also [useless use of `grep`](http://www.iki.fi/era/unix/award.html#grep) and, of course, consider performing all the string manipulations in a single Awk script. – tripleee Jul 19 '16 at 17:45

0 Answers0