Skip to content

Instantly share code, notes, and snippets.

@keinydev
Last active January 20, 2023 21:21
Show Gist options
  • Save keinydev/d07b10372a769cbcbd07b91b008d8ed7 to your computer and use it in GitHub Desktop.
Save keinydev/d07b10372a769cbcbd07b91b008d8ed7 to your computer and use it in GitHub Desktop.

Bash Control Structure


Bash Series

Basic Bash commands

Programming with Bash

🔵 Bash Control Structure

User interaction with Bash


IF Condition

# Simple condition
if condition
then 
  script
fi

# If/else condition
if condition
then 
  script
else 
  script
fi

# If/elif/else condition
if condition
then 
  script
elif condition
then
  script
else 
  script
fi

Examples

script_name.sh

#!/usr/bin/env bash
declare -i number=5

if [[ $number -gt 10 ]]
then 
  echo "$number is greater than 10"
else 
  echo "$number is not greater than 10"
fi

if (($number>10)) #The same condition as above
then 
  echo "$number is greater than 10"
else 
  echo "$number is not greater than 10"
fi

if (($number>10))
then 
  echo "$number is greater than 10"
elif (($number>4))
then 
  echo "$number is greater than 4"
else 
  echo "$number is not greater than 10"
fi

While and until condition

# While condition
while condition
do 
  script
done

# Until condition
until condition
do 
  script
done

While loop

script_name.sh

#!/usr/bin/env bash
declare -i n=0

while ((n<10))
do 
  echo "n:$n"
  ((n++))
done

Until loop

script_name.sh

#!/usr/bin/env bash
declare -i n=0

until ((n==10))
do 
  echo "n:$n"
  ((n++))
done

For loop

for condition
do 
  script
done

Example

script_name.sh

#!/usr/bin/env bash

for i in 1 2 3 #For in list
do 
  echo "n:$i"
done

for i in {1..20} #For in range
do 
  echo "n:$i"
done

for (( i=1; i<=10; i++)) #Iteration
do 
  echo "n:$i"
done

declare -a fruits=("apple" "orange" "cherry")
for i in ${fruits[@]} #Iterates an array
do 
  echo $i
done

declare -A person
person[name]="Keiny"
person[country]="Colombia"
for i in "${!person[@]}" #Iterates an associated array
do 
  echo $i: "${person[$i]}"
done

for i in $(ls) #Iterates all files
do 
  echo "Found a file: $i"
done

Case

script_name.sh

#!/usr/bin/env bash
animal="dog"

case $animal in
  bird) echo "Avian";;
  dog|puppy) echo "Canine";;
  *) echo "No match";;
esac

Functions

Example with no arguments

script_name.sh

#!/usr/bin/env bash
greet(){
  echo "Hi there"
}

echo "See me"
greet

Example with arguments

script_name.sh

#!/usr/bin/env bash
greet(){
  echo "Hello $1"
}

echo "See me"
greet Keiny

#Listing things
see_list(){
  echo "Here the list from the function $FUNCNAME"
  i=1
  for f in "$@"; do
    echo $i: "$f"
    ((i++))
  done
}

see_list /*
echo 
see_list cat dog donkey ant

Writing and reading text files

Write files with output redirection operators (> and >>)

  • echo "abc" > out.txt overwrites the content of out.txt
  • echo "abc" >> out.txt appends to the end of out.txt

Read from files with input redirection (<) and read command

  • while read line; do echo $line; done < in.txt

Example

script_name.sh

#!/usr/bin/env bash
for i in 1 2 3 4 5
do 
  echo "This is line $i" > my_output.txt
done

#Printing content from the previous file
while read f
do echo "I read a line and it says: $f"
done < my_output.txt

Example from linkedin

script_name.sh

# A fortune-telling game

echo -e "\t\t   Welcome  to  the "
echo -e "\t\t🔮 \033[5mMYSTICAL  SPHERE\033[0m 🔮"
echo

waitingnumber=$(( 0 + RANDOM % 3 ))
mysterynumber=$(( 1 + RANDOM % 10 ))

declare -a fortunes=(
    "You are likely to achieve the outcome you seek."
    "Today is not a good day to do that."
    "While it might seem unlikely, your chances are good."
    "If you believe you will be successful, that's half the battle."
    "If you cared enough to ask, you care enough to make it happen."
    "I think you already know the answer to that."
    "Stop wondering and start doing!"
    "Yes, sure, whatever, I'm busy."
    "Next Thursday might be a better day to do that."
    "Sure, but what will the neighbors think?"
)

case $waitingnumber in
    0) sleep 1; echo "One moment please..."; sleep 1;;
    1) sleep 1; echo "Your fortune will be along shortly..."; sleep 2;;
    2) sleep 1; echo "Preparing your fate..."; sleep 1;;
    3) sleep 1; echo "The veil of mystery is dark today..."; sleep 3;;
esac

echo
echo "${fortunes[mysterynumber]}"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment