Jump to content

Conditions

From Squishu Wiki
Revision as of 18:57, 5 July 2025 by Princesssquishuskush (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

General

  • A shell script can test the exit code of any command that can be invoked from the cli
    • That's why you should include an exit command at the end of any scripts you write
  • test and [ ] are synonymous
    • don't name programs test or it will get jacked up
  • Here is the first line of an if statement written each way
    • if test -f blah.py
    • if [ -f blah.py ]
      • The spaces after the first brace and before the second one are required
      • Mnemonic: [ ] means test, and you wouldn't do test-f
  • Conditions fall into 3 categories
    • string comparison
    • arithmetic comparison
    • file conditionals

string comparison

  • string1 = string2
  • string1 != string2
  • -n string => true if the string is not null
  • -z string => true if the string is null (an empty string)

arithmetic comparison

  • expression1 -eq expression2
  • expression1 -ne expression2
  • expression1 -gt expression2
  • expression1 -ge expression2
  • expression1 -lt expression2
  • expression1 -le expression2
  • ! expression = true if expression is false, and vice versa

file conditionals

  • -d file => true if the file is a directory
  • -e file => true if the file exists
  • -f file => true if it's a regular file, more portable than -e
  • -g file => true if sgid is set on the file
  • -r file => true if file is readable
  • -s file => true if file is not zero in sie
  • -u file => tru if suid is set on file
  • -w file => true if file is writeable
  • -x file => true if file is executable