Bash Tests Fail on Mac OS

I have a script where I need to determine if the path /usr/share/terminfo exists as a directory, and then if so, subsequently test for if the directory is readable by the user running the script.

The tests succeed if I enter them in line by line into an interactive bash shell, yet when executed within script (as the same user that they succeed above), the tests fail**.

I have also tested while sudo su - to root, and the same behavior is observed. I thought perhaps sandboxing or a file system firewall of sorts was limiting me, so I even tried going into Mac OS’s Settings > Security & Privacy > [Privacy], selecting “Full Disk Access”, and then adding “Terminal” to the list to grant it “Full Disk Access”, then restarting the “Terminal” app as hinted, but to no avail.

They fail on Mac OS 12.5.0, executing within BASH 3.2.57(1)-release (x86_64-apple-darwin21)

function checkUsrShareTerminfo() {
  echo "Testing /usr/share/terminfo for existence..."
  if [ -d "/usr/share/terminfo" ]; then
    echo "Success. The path /usr/share/terminfo exists."
    echo "Now checking if the path /usr/share/terminfo is readable by the user running this process..."
    if [ -r "/usr/share/terminfo" ]; then
      echo "Success. The path /usr/share/terminfo is readable by the user running this process."
      echo "Now attempting to define variable TERMINFO..."
      TERMINFO="/usr/share/terminfo"
      envExportVarTerminfo
      return
    else
      echo "The path at /usr/share/terminfo is not readable by the user running this process."
      false
      return
    fi
  else
    echo "The path at /usr/share/terminfo does not exist."
    false
    return
  fi
};
export -f checkUsrShareTerminfo
checkUsrShareTerminfo

If I run the test as a one-liner in an interactive BASH console, the test succeeds…

if [ -d "/usr/share/terminfo" ]; then echo "The directory exists"; else echo "The directory does not exist."; fi

Result from one-liner:

Users-MacBook-Pro:scripts user$ whoami
user

Users-MacBook-Pro:scripts user$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.

Users-MacBook-Pro:scripts user$ if [ -d "/usr/share/terminfo" ]; then echo "The directory exists"; else echo "The directory does not exist."; fi
The directory exists

So we can see that from the one-liner, it succeeds, however when run from a script by the very same user, it fails.

The directory does in fact exist:

user$ ls -alh /usr/share/terminfo
total 0
drwxr-xr-x   44 root  wheel   1.4K Jul 14 02:48 .
drwxr-xr-x   45 root  wheel   1.4K Jul 14 02:48 ..
drwxr-xr-x    4 root  wheel   128B Jul 14 02:48 31
drwxr-xr-x    5 root  wheel   160B Jul 14 02:48 32
drwxr-xr-x    4 root  wheel   128B Jul 14 02:48 33
drwxr-xr-x    5 root  wheel   160B Jul 14 02:48 34
drwxr-xr-x    7 root  wheel   224B Jul 14 02:48 35
drwxr-xr-x    8 root  wheel   256B Jul 14 02:48 36
drwxr-xr-x    7 root  wheel   224B Jul 14 02:48 37
drwxr-xr-x    3 root  wheel    96B Jul 14 02:48 38
drwxr-xr-x    5 root  wheel   160B Jul 14 02:48 39
drwxr-xr-x    3 root  wheel    96B Jul 14 02:48 41
drwxr-xr-x    6 root  wheel   192B Jul 14 02:48 45
drwxr-xr-x    3 root  wheel    96B Jul 14 02:48 4c
drwxr-xr-x    5 root  wheel   160B Jul 14 02:48 4d
drwxr-xr-x    4 root  wheel   128B Jul 14 02:48 4e
drwxr-xr-x   19 root  wheel   608B Jul 14 02:48 50
drwxr-xr-x    8 root  wheel   256B Jul 14 02:48 51
drwxr-xr-x    3 root  wheel    96B Jul 14 02:48 58
drwxr-xr-x  313 root  wheel   9.8K Jul 14 02:48 61
drwxr-xr-x   51 root  wheel   1.6K Jul 14 02:48 62
drwxr-xr-x  126 root  wheel   3.9K Jul 14 02:48 63
drwxr-xr-x  289 root  wheel   9.0K Jul 14 02:48 64
drwxr-xr-x   30 root  wheel   960B Jul 14 02:48 65
drwxr-xr-x   27 root  wheel   864B Jul 14 02:48 66
drwxr-xr-x   53 root  wheel   1.7K Jul 14 02:48 67
drwxr-xr-x  142 root  wheel   4.4K Jul 14 02:48 68
drwxr-xr-x   82 root  wheel   2.6K Jul 14 02:48 69
drwxr-xr-x    6 root  wheel   192B Jul 14 02:48 6a
drwxr-xr-x   36 root  wheel   1.1K Jul 14 02:48 6b
drwxr-xr-x   26 root  wheel   832B Jul 14 02:48 6c
drwxr-xr-x   79 root  wheel   2.5K Jul 14 02:48 6d
drwxr-xr-x  186 root  wheel   5.8K Jul 14 02:48 6e
drwxr-xr-x   48 root  wheel   1.5K Jul 14 02:48 6f
drwxr-xr-x  122 root  wheel   3.8K Jul 14 02:48 70
drwxr-xr-x   43 root  wheel   1.3K Jul 14 02:48 71
drwxr-xr-x   30 root  wheel   960B Jul 14 02:48 72
drwxr-xr-x   76 root  wheel   2.4K Jul 14 02:48 73
drwxr-xr-x  243 root  wheel   7.6K Jul 14 02:48 74
drwxr-xr-x   10 root  wheel   320B Jul 14 02:48 75
drwxr-xr-x  133 root  wheel   4.2K Jul 14 02:48 76
drwxr-xr-x  239 root  wheel   7.5K Jul 14 02:48 77
drwxr-xr-x  124 root  wheel   3.9K Jul 14 02:48 78
drwxr-xr-x   34 root  wheel   1.1K Jul 14 02:48 7a

Are you running the script using sudo or other user?

When run as a script, I run it as a non-privileged user, “user”; I am not using sudo su -.

The interesting thing though is that even when run as sudo, it still has the same results (failing the checks for existence and readability, when the directory does exist and is readable).

Well I am humbled to admit that the problem was my own doing. :man_facepalming: I mistakenly was checking for /usr/lib/terminfo instead of /usr/share/terminfo in my script. Upon correcting this, the test succeeded as expected.

For what it’s worth for anyone stumbling across this in the future, Mac OS (as of 12.5.0) has /usr/share/terminfo but not /usr/lib/terminfo.

1 Like

Hey, no issue. I am glad it is all sorted out. A cup of joe never hurts anyone, including a small break. Cheers.

1 Like

I just wanted to also pass along my thanks to you, Vivek, for all your educational materials you provide the world on your website. I have been coming to your website since 2009. I used to download your combined PDF of guides (back when it was available) and read through it when work was slow on my night shift way back then. Thank you for your contributions to educating the world. :slight_smile:

1 Like