0

for some reason when I try to run this script:

if ["$1" == "--all"]
then
  sayOS
  sayUptime
  sayMemory
  sayDirectory
  sayTime
fi

it gives me this error:

say_stuff.sh: line 26: [: missing ']'
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Spaces matter, not just in zsh but in all shells that are even a little POSIX-flavored. `[ one = two ]`, not `[one == two]` – Charles Duffy Nov 30 '21 at 23:48
  • 1
    BTW, when writing posts here, put one set of triple-quotes on its own line before the start of your code, and a second on its own line at the end of our code. They don't need to be per-line or per-word. I've edited to that effect -- you might want to look at how the markdown is now written to use the pattern yourself in the future. – Charles Duffy Nov 30 '21 at 23:49
  • (and btw, re my first comment, `=` vs `==` is deliberate: the [POSIX-standard version of `test`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) only supports string comparison with one `=`, so writing it that way is more portable) – Charles Duffy Nov 30 '21 at 23:54
  • 1
    Your $1 is empty. Hence you give to `if` the command `[` with the arguments `==` and `-all]`. However, `[` requires for historical reasons a lone `]` as the last argument. This is a leftover from good old Bourne Shell times. If you would use `test` instead of `[`, there is no final `]` allowed or necessary. See _man test_ for a detailed explanation. Actually, I would write it simply as `if [[ $1 == --all ]]`. – user1934428 Dec 01 '21 at 10:07

0 Answers0