Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Last active January 16, 2023 23:43
Show Gist options
  • Save aimtiaz11/b6b43ed2caac89593dd1c28675347ba6 to your computer and use it in GitHub Desktop.
Save aimtiaz11/b6b43ed2caac89593dd1c28675347ba6 to your computer and use it in GitHub Desktop.
Bash Reference - If-statements

Bash Reference - If-statements

Arithmatic Operation

Wrap expression in the if-statement with (( expression));

STRING_COUNT=$(grep -c 'hello world' path/to/file.txt)
echo "String count: $STRING_COUNT"

if (($STRING_COUNT == 1 )); then
  sed -i 's/hello world/hello mars/' path/to/file.txt
  echo "substitution complete..."
else
  echo "Unable to do substitution. Did not find expression."
  exit 1
fi

If/elif/else Expression with Or condition

if [ "$(APP_TYPE)" == "if_a" ] || [ "$(APP_TYPE)" == "if_b" ]; then
  echo "do if"
elif [ "$(APP_TYPE)" == "elif_a" ] || [ "$(APP_TYPE)" == "elif_b" ]; then
  echo "do elif"
else
  echo "doing else"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment