shelllings

a practical way to learn shell
git clone https://git.davidvoz.net/shelllings.git
index
logs
tree
license

16_andand_oror.sh
1#!/bin/sh
2
3# We used the if/else case but that is often not needed in shell. We can
4# instead use && and || for many cases. Take a look at the snippet below
5#
6# $ sh foo.sh && sh bar.sh
7#
8# The snippet above means that if 'sh foo.sh' is ran successfully,
9# without error, then 'sh bas.sh' will be ran.
10#
11# $ sh foo.sh || sh bar.sh
12#
13# If 'sh foo.sh' is ran and an error does occur, then 'sh bar.sh' is
14# ran.
15#
16# Some conditions require square brackets, these are test expressions.
17# Examples of conditions that require square brackets are usually string
18# and string comparisons, existence of files, and empty or not files.
19#
20# $ mv file1 file2 || echo "Could not be moved"
21# $ [ -z "$1" ] || echo "Nothing was entered"
22#
23# You can also extend it
24#
25# $ [ "$1" = "password123" ] || { echo "power off"; sh shutdown.sh; }
26#
27# The semi colon is used the same way as other programming languages
28# where you can combine multiple lines into a single line. Note that
29# it should still be easily readable to humans to debug and understand.
30# If you go back to exercise 14, you can see them being used to condense
31# the 'do' word
32#
33# Fix the code below so the echo commands make sense with the pin 1412
34# Leave the two && in place
35
36[] && echo "incorrect pin"
37[] && echo "correct pin"
38
39# You can also combine it into 1 line but it doesn't always comply with
40# many shells
41#
42# $ [] && echo "good" || echo "bad"