Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install.sh: Remove bashisms and fix all other shellcheck warnings #851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andersk
Copy link

@andersk andersk commented Aug 3, 2018

ShellCheck is very good at finding shell script quality issues. In this script it detects, among other things, several bash-specific constructs that actually fail on Debian and Ubuntu systems, which have /bin/sh symlinked to dash instead of bash.

Before this commit:

$ ./install.sh
…
./install.sh: 52: ./install.sh: [[: not found
./install.sh: 57: [: unexpected operator
…
./install.sh: 95: ./install.sh: [[: not found
…

$ shellcheck install.sh

In install.sh line 13:
  printf "$cyan> Downloading tarball...$reset\n"
         ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 21:
    if echo $version | grep -qE "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$"; then
            ^------^ SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 24:
      printf "$red> Version number must match MAJOR.MINOR.PATCH.$reset\n"
             ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 31:
  tarball_tmp=`mktemp -t yarn.tar.gz.XXXXXXXXXX`
              ^-- SC2006: Use $(...) notation instead of legacy backticked `...`.

In install.sh line 33:
    yarn_verify_integrity $tarball_tmp
                          ^----------^ SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 35:
    printf "$cyan> Extracting to ~/.yarn...$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 38:
    tar zxf $tarball_tmp -C "$temp"
            ^----------^ SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 42:
    rm $tarball_tmp*
       ^----------^ SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 44:
    printf "$red> Failed to download $url.$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 52:
  if [[ -z "$(command -v gpg)" ]]; then
     ^--------------------------^ SC2039: In POSIX sh, [[ ]] is undefined.

In install.sh line 53:
    printf "$yellow> WARNING: GPG is not installed, integrity can not be verified!$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 57:
  if [ "$YARN_GPG" == "no" ]; then
                   ^-- SC2039: In POSIX sh, == in place of = is undefined.

In install.sh line 58:
    printf "$cyan> WARNING: Skipping GPG integrity check!$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 62:
  printf "$cyan> Verifying integrity...$reset\n"
         ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 67:
    printf "$red> Could not download GPG signature for this Yarn release. This means the release can not be verified!$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 73:
  if gpg --verify "$1.asc" $1; then
                           ^-- SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 74:
    printf "$green> GPG signature looks good$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 76:
    printf "$red> GPG signature for this Yarn release is invalid! This is BAD and may mean the release has been tampered with. It is strongly recommended that you report this to the Yarn developers.$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 82:
  printf "$cyan> Adding to \$PATH...$reset\n"
         ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 87:
    printf "$red> Profile not found. Tried ${YARN_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 91:
    printf "> Append the following lines to the correct file yourself:$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 95:
      if [[ $YARN_PROFILE == *"fish"* ]]; then
         ^-----------------------------^ SC2039: In POSIX sh, [[ ]] is undefined.

In install.sh line 96:
        command fish -c 'set -U fish_user_paths $fish_user_paths ~/.yarn/bin'
                        ^-- SC2016: Expressions don't expand in single quotes, use double quotes for that.

In install.sh line 102:
    printf "$cyan> We've added the following to your $YARN_PROFILE\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 104:
    printf "   $SOURCE_STR$reset\n"
           ^----------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 106:
    version=`$HOME/.yarn/bin/yarn --version` || (
            ^-- SC2006: Use $(...) notation instead of legacy backticked `...`.
             ^---^ SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 107:
      printf "$red> Yarn was installed, but doesn't seem to be working :(.$reset\n"
             ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 111:
    printf "$green> Successfully installed Yarn $version! Please open another terminal where the \`yarn\` command will now be available.$reset\n"
           ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 121:
  local DETECTED_PROFILE
  ^--------------------^ SC2039: In POSIX sh, 'local' is undefined.

In install.sh line 123:
  local SHELLTYPE
  ^-------------^ SC2039: In POSIX sh, 'local' is undefined.

In install.sh line 162:
  printf "${white}Installing Yarn!$reset\n"
         ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 165:
    if which yarn; then
       ^---^ SC2230: which is non-standard. Use builtin 'command -v' instead.

In install.sh line 166:
      local latest_url
      ^--------------^ SC2039: In POSIX sh, 'local' is undefined.

In install.sh line 167:
      local specified_version
      ^---------------------^ SC2039: In POSIX sh, 'local' is undefined.

In install.sh line 168:
      local version_type
      ^----------------^ SC2039: In POSIX sh, 'local' is undefined.

In install.sh line 171:
        specified_version=`curl -sS $latest_url`
                          ^--------------------^ SC2006: Use $(...) notation instead of legacy backticked `...`.

In install.sh line 178:
        specified_version=`curl -sS $latest_url`
                          ^--------------------^ SC2006: Use $(...) notation instead of legacy backticked `...`.

In install.sh line 182:
        specified_version=`curl -sS $latest_url`
                          ^--------------------^ SC2006: Use $(...) notation instead of legacy backticked `...`.

In install.sh line 183:
        version_type='latest'
        ^----------^ SC2034: version_type appears unused. Verify use (or export if used externally).

In install.sh line 185:
      yarn_version=`yarn -V`
                   ^-------^ SC2006: Use $(...) notation instead of legacy backticked `...`.

In install.sh line 186:
      yarn_alt_version=`yarn --version`
                       ^--------------^ SC2006: Use $(...) notation instead of legacy backticked `...`.

In install.sh line 188:
      if [ "$specified_version" = "$yarn_version" -o "$specified_version" = "$yarn_alt_version" ]; then
                                                  ^-- SC2166: Prefer [ p ] || [ q ] as [ p -o q ] is not well defined.

In install.sh line 189:
        printf "$green> Yarn is already at the $specified_version version.$reset\n"
               ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 192:
      	printf "$yellow> $yarn_alt_version is already installed, Specified version: $specified_version.$reset\n"
               ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 196:
      printf "$red> $HOME/.yarn already exists, possibly from a past Yarn install.$reset\n"
             ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 197:
      printf "$red> Remove it (rm -rf $HOME/.yarn) and run this script again.$reset\n"
             ^-- SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 202:
  yarn_get_tarball $1 $2
                   ^-- SC2086: Double quote to prevent globbing and word splitting.
                      ^-- SC2086: Double quote to prevent globbing and word splitting.

In install.sh line 208:
  read -p "$1 [y/N] " -n 1 -r
       ^-- SC2039: In POSIX sh, read -p is undefined.

In install.sh line 210:
  if [[ ! $REPLY =~ ^[Yy]$ ]]
     ^----------------------^ SC2039: In POSIX sh, [[ ]] is undefined.

In install.sh line 212:
    printf "$red> Aborting$reset\n"
           ^----------------------^ SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

In install.sh line 218:
yarn_install $1 $2
             ^-- SC2086: Double quote to prevent globbing and word splitting.
                ^-- SC2086: Double quote to prevent globbing and word splitting.

@Haroenv
Copy link
Member

Haroenv commented Aug 3, 2018

Deploy preview for yarnpkg ready!

Built with commit bfd0fbc

https://deploy-preview-851--yarnpkg.netlify.com

@andersk andersk force-pushed the shellcheck branch 2 times, most recently from 5635c77 to bfd0fbc Compare August 3, 2018 03:27
@Haroenv Haroenv requested a review from Daniel15 August 3, 2018 07:31
ShellCheck (https://www.shellcheck.net/) is very good at finding shell
script quality issues.  In this script it detects, among other things,
several bash-specific constructs that actually fail on Debian and
Ubuntu systems, which have /bin/sh symlinked to dash instead of bash.

Before this commit:

    $ ./install.sh
    …
    ./install.sh: 52: ./install.sh: [[: not found
    ./install.sh: 57: [: unexpected operator
    …
    ./install.sh: 95: ./install.sh: [[: not found
    …

    $ shellcheck install.sh

    In install.sh line 13:
      printf "$cyan> Downloading tarball...$reset\n"
             ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 21:
        if echo $version | grep -qE "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$"; then
                ^------^ SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
        if echo "$version" | grep -qE "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$"; then

    In install.sh line 24:
          printf "$red> Version number must match MAJOR.MINOR.PATCH.$reset\n"
                 ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 31:
      tarball_tmp=`mktemp -t yarn.tar.gz.XXXXXXXXXX`
                  ^-- SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

    Did you mean:
      tarball_tmp=$(mktemp -t yarn.tar.gz.XXXXXXXXXX)

    In install.sh line 33:
        yarn_verify_integrity $tarball_tmp
                              ^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
        yarn_verify_integrity "$tarball_tmp"

    In install.sh line 35:
        printf "$cyan> Extracting to ~/.yarn...$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 38:
        tar zxf $tarball_tmp -C "$temp"
                ^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
        tar zxf "$tarball_tmp" -C "$temp"

    In install.sh line 42:
        rm $tarball_tmp*
           ^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
        rm "$tarball_tmp"*

    In install.sh line 44:
        printf "$red> Failed to download $url.$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 52:
      if [[ -z "$(command -v gpg)" ]]; then
         ^--------------------------^ SC3010 (warning): In POSIX sh, [[ ]] is undefined.

    In install.sh line 53:
        printf "$yellow> WARNING: GPG is not installed, integrity can not be verified!$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 57:
      if [ "$YARN_GPG" == "no" ]; then
                       ^-- SC3014 (warning): In POSIX sh, == in place of = is undefined.

    In install.sh line 58:
        printf "$cyan> WARNING: Skipping GPG integrity check!$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 62:
      printf "$cyan> Verifying integrity...$reset\n"
             ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 67:
        printf "$red> Could not download GPG signature for this Yarn release. This means the release can not be verified!$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 73:
      if gpg --verify "$1.asc" $1; then
                               ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
      if gpg --verify "$1.asc" "$1"; then

    In install.sh line 74:
        printf "$green> GPG signature looks good$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 76:
        printf "$red> GPG signature for this Yarn release is invalid! This is BAD and may mean the release has been tampered with. It is strongly recommended that you report this to the Yarn developers.$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 82:
      printf "$cyan> Adding to \$PATH...$reset\n"
             ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 87:
        printf "$red> Profile not found. Tried ${YARN_PROFILE} (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 91:
        printf "> Append the following lines to the correct file yourself:$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 95:
          if [[ $YARN_PROFILE == *"fish"* ]]; then
             ^-----------------------------^ SC3010 (warning): In POSIX sh, [[ ]] is undefined.

    In install.sh line 96:
            command fish -c 'set -U fish_user_paths $fish_user_paths ~/.yarn/bin'
                            ^-- SC2016 (info): Expressions don't expand in single quotes, use double quotes for that.

    In install.sh line 97:
            printf "$cyan> We've added ~/.yarn/bin to your fish_user_paths universal variable\n"
                   ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 100:
            printf "$cyan> We've added the following to your $YARN_PROFILE\n"
                   ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 104:
          printf "   $SOURCE_STR$reset\n"
                 ^----------------------^ SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 107:
        version=`$HOME/.yarn/bin/yarn --version` || (
                ^-- SC2006 (style): Use $(...) notation instead of legacy backticks `...`.
                 ^---^ SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
        version=$("$HOME"/.yarn/bin/yarn --version) || (

    In install.sh line 108:
          printf "$red> Yarn was installed, but doesn't seem to be working :(.$reset\n"
                 ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 112:
        printf "$green> Successfully installed Yarn $version! Please open another terminal where the \`yarn\` command will now be available.$reset\n"
               ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 122:
      local DETECTED_PROFILE
      ^--------------------^ SC3043 (warning): In POSIX sh, 'local' is undefined.

    In install.sh line 124:
      local SHELLTYPE
      ^-------------^ SC3043 (warning): In POSIX sh, 'local' is undefined.

    In install.sh line 153:
      if [ ! -z "$DETECTED_PROFILE" ]; then
           ^-- SC2236 (style): Use -n instead of ! -z.

    In install.sh line 163:
      printf "${white}Installing Yarn!$reset\n"
             ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 167:
          local latest_url
          ^--------------^ SC3043 (warning): In POSIX sh, 'local' is undefined.

    In install.sh line 168:
          local specified_version
          ^---------------------^ SC3043 (warning): In POSIX sh, 'local' is undefined.

    In install.sh line 169:
          local version_type
          ^----------------^ SC3043 (warning): In POSIX sh, 'local' is undefined.

    In install.sh line 172:
            specified_version=`curl -sS $latest_url`
                              ^--------------------^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

    Did you mean:
            specified_version=$(curl -sS $latest_url)

    In install.sh line 179:
            specified_version=`curl -sS $latest_url`
                              ^--------------------^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

    Did you mean:
            specified_version=$(curl -sS $latest_url)

    In install.sh line 183:
            specified_version=`curl -sS $latest_url`
                              ^--------------------^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

    Did you mean:
            specified_version=$(curl -sS $latest_url)

    In install.sh line 184:
            version_type='latest'
            ^----------^ SC2034 (warning): version_type appears unused. Verify use (or export if used externally).

    In install.sh line 186:
          yarn_version=`yarn -V`
                       ^-------^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

    Did you mean:
          yarn_version=$(yarn -V)

    In install.sh line 187:
          yarn_alt_version=`yarn --version`
                           ^--------------^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

    Did you mean:
          yarn_alt_version=$(yarn --version)

    In install.sh line 189:
          if [ "$specified_version" = "$yarn_version" -o "$specified_version" = "$yarn_alt_version" ]; then
                                                      ^-- SC2166 (warning): Prefer [ p ] || [ q ] as [ p -o q ] is not well defined.

    In install.sh line 190:
            printf "$green> Yarn is already at the $specified_version version.$reset\n"
                   ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 193:
            printf "$yellow> $yarn_alt_version is already installed, Specified version: $specified_version.$reset\n"
                   ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 197:
          printf "$red> $HOME/.yarn already exists, possibly from a past Yarn install.$reset\n"
                 ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 198:
          printf "$red> Remove it (rm -rf $HOME/.yarn) and run this script again.$reset\n"
                 ^-- SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 203:
      yarn_get_tarball $1 $2
                       ^-- SC2086 (info): Double quote to prevent globbing and word splitting.
                          ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
      yarn_get_tarball "$1" "$2"

    In install.sh line 209:
      read -p "$1 [y/N] " -n 1 -r
           ^-- SC3045 (warning): In POSIX sh, read -p is undefined.

    In install.sh line 211:
      if [[ ! $REPLY =~ ^[Yy]$ ]]
         ^----------------------^ SC3010 (warning): In POSIX sh, [[ ]] is undefined.

    In install.sh line 213:
        printf "$red> Aborting$reset\n"
               ^----------------------^ SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

    In install.sh line 219:
    yarn_install $1 $2
                 ^-- SC2086 (info): Double quote to prevent globbing and word splitting.
                    ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

    Did you mean:
    yarn_install "$1" "$2"

    For more information:
      https://www.shellcheck.net/wiki/SC2034 -- version_type appears unused. Veri...
      https://www.shellcheck.net/wiki/SC2166 -- Prefer [ p ] || [ q ] as [ p -o q...
      https://www.shellcheck.net/wiki/SC3010 -- In POSIX sh, [[ ]] is undefined.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants