3.2 Data structures and conditionals

DATA STRUCTURES

<variable_name>=<value>

Notice that there are no spaces before and after the equal (=) operator; otherwise, you’ll get an error. Why? Because the shell will interpret the variable_name as a command, not a variable.

  • Variable PATH_TO_FILES='/some/path'

  • Constants readonly PATH_TO_FILES='/some/path'

  • Lists/Arrays

# !/usr/bin/env bash
declare -a projects

# Instantiate the array with values
projects=("introduction" \
          "environments" \
          "linux" \
          "scripts" )

CONDITIONALS

IF CONDITIONS

if [[ condition ]]; then
elif
    [COMMANDS]
else
    [COMMANDS]
fi

Example

touch conditional && chmod u+x conditional && vim conditional
# !/usr/bin/env bash

if [[ ${1} -ge 10 ]]; then
    echo "Number is greater than 10"
else
    echo "Number is lower than 10"
fi

LOOPS

FOR LOOP

# !/usr/bin/env bash

for item in [LIST]; do
    [COMMANDS]
done

Example:

touch for_loop1 && chmod u+x for_loop1 && vim for_loop1
# !/usr/bin/env bash

for i in {1..10}; do
    echo "Print ${i}"
done
touch for_loop2 && chmod u+x for_loop2 && vim for_loop2
# !/usr/bin/env bash

for i in /var/*; do
  echo $i
done

WHILE LOOP

# !/usr/bin/env bash

while [ condition ]; do
    [COMMANDS]
done

Example

touch while_loop && chmod u+x while_loop && vim while_loop
# !/usr/bin/env bash

num=1
while [ $num -le 10 ]; do
    echo $(($num * 7))
    num=$(($num+1))
done

UNTIL LOOP

until [ condition ]; do
[COMMANDS]
Done

Example:

touch until_loop && chmod u+x until_loop && vim until_loop
# !/usr/bin/env bash

num=1
until [ $num -gt 10 ]; do
    echo $(($num * 7))
    num=$(($num+1))
done

Warning

Don’t forget about -le vs -lt and -ge vs gt