From be1ba26da39d437725122017c8f2615d22aea2ac Mon Sep 17 00:00:00 2001 From: koalaman Date: Wed, 18 May 2016 10:08:07 -0700 Subject: [PATCH] Created SC1075 (markdown) --- SC1075.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 SC1075.md diff --git a/SC1075.md b/SC1075.md new file mode 100644 index 0000000..2b38553 --- /dev/null +++ b/SC1075.md @@ -0,0 +1,46 @@ +## Use 'elif' instead of 'else if'. + +### Problematic code: + +```sh +if [ "$#" -eq 0 ] +then + echo "Usage: ..." +else if [ "$#" -lt 2 ] +then + echo "Missing operand" +fi + +``` + +### Correct code: + +```sh +if [ "$#" -eq 0 ] +then + echo "Usage: ..." +elif [ "$#" -lt 2 ] +then + echo "Missing operand" +fi +``` +### Rationale: + +Many languages allow alternate branches with `else if`, but `sh` is not one of them. Use `elif` instead. + +### Exceptions: + +`else if` is a valid (though confusing) way of nesting an `if` statement in a parent's `else`. If this is your intention, please use canonical formatting and put a linefeed between `else` and `if`. + +```sh + +if x +then + echo "x" +else # line break here + if y + then + echo "y" + fi +fi +``` \ No newline at end of file