3

Looking for a way to declare a boolean, something like this:

HOME_IS_BASE_DIR=$("home" == "${BASE_DIRECTORY}")    // boolean
USERS_IS_BASE_DIR=$("Users" == "${BASE_DIRECTORY}")   // boolean

except of course the above doesn't work, I get an error:

bash cannot find home command

bash cannot find Users command

the long way to do this would be:

HOME_IS_BASE_DIR=false;
USERS_IS_BASE_DIR=false;

if [ "home" == "${BASE_DIRECTORY}" ]; then
  HOME_IS_BASE_DIR=true;
else if [  "Users" == "${BASE_DIRECTORY}" ]; then
  USERS_IS_BASE_DIR=true;
fi

is there a shorthand for this?

0

3 Answers 3

4

A more compact representation can be achieved with something like the following

#!/bin/sh         
BASE_DIRECTORY="home";
[ "home" = "$BASE_DIRECTORY" ] && HOME_IS_BASE_DIR=true || HOME_IS_BASE_DIR=false;
echo $HOME_IS_BASE_DIR;
3
  • @alexander-mills your edit is correct. I just provided a more general example :)
    – jsalatas
    Dec 9, 2016 at 0:42
  • thanks, I like specific examples, it's more clear to the reader :) Dec 9, 2016 at 0:44
  • Your second edit was confusing IMHO. It wouldn't even run at all if you didn't define $FOO :)
    – jsalatas
    Dec 9, 2016 at 0:47
1
case ${BASE_DIRECTORY} in
    "home")
        HOME_IS_BASE_DIR=true
        USERS_IS_BASE_DIR=false
        ;;
    "Users")
        HOME_IS_BASE_DIR=false
        USERS_IS_BASE_DIR=true
        ;;
    *)
        # Handle heretofore unhandled values here
        ;;
esac

It's not exactly shorthand, but it's a both very clear what you're doing, and easy to extend to other locations. Rather than using those extra variables, though, why not just look at ${BASE_DIRECTORY} directly?

1
  • thanks, the reason I don't want to look at $BASE_DIRECTORY directly is because I wanted to use shorthand later in some other functionality Dec 9, 2016 at 19:47
1

You're asking for something similar to this:

HOME_IS_BASE_DIR=$("home" == "${BASE_DIRECTORY}")
USERS_IS_BASE_DIR=$("Users" == "${BASE_DIRECTORY}")

You can make the tests into functions:

HOME_IS_BASE_DIR()  { [[ "home"  == "${BASE_DIRECTORY}" ]]; }
USERS_IS_BASE_DIR() { [[ "Users" == "${BASE_DIRECTORY}" ]]; }

Then you can do tests like this:


BASE_DIRECTORY="/"

if HOME_IS_BASE_DIR        ; then echo "home is base dir"    ; fi
if ! HOME_IS_BASE_DIR      ; then echo "home is NOT base dir"; fi

if ! HOME_IS_BASE_DIR && \
   ! USERS_IS_BASE_DIR     ; then echo "neither is base dir" ; fi

# output of above:
# home is NOT base dir
# neither is base dir

# ...or use &&/|| instead of 'if':
BASE_DIRECTORY="Users"
USERS_IS_BASE_DIR && echo "Users is base dir"
HOME_IS_BASE_DIR  || echo "home is NOT base dir"

# output of above:
# Users is base dir
# home is NOT base dir

The above works for bash and zsh, and can easily be adapted for dash/POSIX.

Explanation

In shell scripts, boolean expressions are usually written for exit codes (cmd1 && cmdd) etc. The two functions will generate exit codes corresponding to the tests in [[ .. ]], and therefore we can form boolean expressions on the functions.

Remember that the functions are evaluated each time the test is done, so if BASE_DIRECTORY is changed, so will the outcome of any tests with HOME_IS_BASE_DIR and USERS_IS_BASE_DIR.

2
  • Or a bit more flexible: is_base () { [ "$1" = "$BASE_DIRECTORY" ]; }, and then later if is_base Users; then ...; fi etc., or just is_base home && echo 'home is base'. But what I think this question is really about is whether the script is running on Linux (home directories under /home) or macOS (home directories under /Users).
    – Kusalananda
    Sep 24, 2022 at 21:27
  • @Kusalananda yep, that's also a possible way to do it.
    – Carl
    Sep 24, 2022 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .