Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2023-01-20 17:25:07
Size: 152
Comment:
Revision 5 as of 2023-01-20 22:05:53
Size: 1131
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
{{{
if [ TEST ]; then
  echo "TEST is true"
elif grep /path/to/file --fixed-strings foo >/dev/null; then
  echo "TEST is false and '/path/to/file' contains 'foo'"
else
  echo "TEST is false and '/path/to/file' does not contain 'foo'"
fi
}}}

[[Shell/Test|Shell tests]] are typically used as the condition (as seen above), but any command that sets the exit code in the expected pattern (zero for true, non-zero for false; as seen above with `grep(1)`) can be used.

----



== Conditional Commands ==

To execute a command conditionally after another command, try:

{{{
true && echo "This only executes because the first command set an exit code of 0"

false || echo "This only executes because the first command set an exit code of 1"
}}}

These operators have equal precedence.
Line 17: Line 45:
{{{
case $INPUT_STRING in
  y)
    echo "Proceeding..."
    ;;
  n)
    echo "Quitting..."
    ;;
  *)
    echo "Please enter y or n only"
    ;;
esac
}}}

Shell Logic


Conditional Logic

if [ TEST ]; then
  echo "TEST is true"
elif grep /path/to/file --fixed-strings foo >/dev/null; then
  echo "TEST is false and '/path/to/file' contains 'foo'"
else
  echo "TEST is false and '/path/to/file' does not contain 'foo'"
fi

Shell tests are typically used as the condition (as seen above), but any command that sets the exit code in the expected pattern (zero for true, non-zero for false; as seen above with grep(1)) can be used.


Conditional Commands

To execute a command conditionally after another command, try:

true && echo "This only executes because the first command set an exit code of 0"

false || echo "This only executes because the first command set an exit code of 1"

These operators have equal precedence.


Case Switching Logic

case $INPUT_STRING in
  y)
    echo "Proceeding..."
    ;;
  n)
    echo "Quitting..."
    ;;
  *)
    echo "Please enter y or n only"
    ;;
esac


CategoryRicottone

Shell/Logic (last edited 2023-01-20 22:05:53 by DominicRicottone)