0

I'm working on an install script (written in bash) for a library. The install script lives in a subdirectory of the project's base directory. I'd like users to be able to run the install script from anywhere under the base directory, so I'm trying to make the script cd to the base directory before executing other commands.

I'm looking at something like

while [[ $PWD == *"ProjectName"* ] && [ $PWD -ne *"ProjectName" ]]; do cd .. ;done

I'm open to cleaner solutions, but at the very least I'd like to understand why the above does not work. No doubt I have brackets in the wrong spots, but I'm not sure where to put them.

Steven Scott
  • 481
  • 3
  • 14
  • 2
    Do you know where the install script lives in relation to the base directory? First cd to the script containing the script, then go up the appropriate number of levels. – Barmar Jan 15 '22 at 00:14
  • [This](https://stackoverflow.com/q/38067413/3266847) and [this](https://stackoverflow.com/q/16203088/3266847) shows you how to combine multiple conditions, but your approach might not be optimal to begin with. – Benjamin W. Jan 15 '22 at 00:35
  • 1
    You either use `[[...]]` or `[...]` - NOT combinations of both. It would be either `[[ $PWD = *ProjectName ]]` or `[ "$PWD" = *ProjectName ]` (double-quoting of all vars require in `[...]` (equivalent to `test`), but not `[[...]]`). You do not use `-ne` for comparing strings (it is a numerical not equal to), use `!=` for string inequality. Also string equality is properly `=`, but most will support `==` as well. A condition of `[[ $PWD = .. ]] && [[ $PWD != .. ]]` will never test `true` – David C. Rankin Jan 15 '22 at 07:58

1 Answers1

0

The following does what I want. Thanks to everyone (especially @David C. Rankin) for their helpful comments.

while [[ "$PWD" = *"ProjectName"* && ! "$PWD" = *"ProjectName" ]]
      do
      cd ..
done
Steven Scott
  • 481
  • 3
  • 14