Skip to content

Latest commit

 

History

History
343 lines (318 loc) · 5.32 KB

bash_course.md

File metadata and controls

343 lines (318 loc) · 5.32 KB

Bash - Course

Data Structures

Scalar | String

  • Declaration :
    lulu="Hello world!"
  • Usage :
    lulu+=Jean     # Add "Jean" to lulu
    echo $lulu     # Display : Hello world!Jean
    echo "$lulu"   # Display : Hello world!Jean
    echo '$lulu'   # Display : $lulu
    current_date=`date`  # Store the current date in a variable
    echo `ls -lh`        # Execute the command 'ls -lh'
  • Length :
    length=${#lulu}
  • Find an occurrence :
    result=$(echo "$lulu" | grep -o "pattern")
    or
    if [[ $variable == *"$element"* ]]; then
      result="The element is present in the variable."
    else 
      result="The element is not present in the variable."
    fi
  • Slice :
    lulu="Hello World!"
    echo "${lulu:3}"              # Displays : lo World!
    lulu="Hello World!"
    echo "${lulu:0:${#lulu}-2}"   # Displays : Hello Worl
  • Replace :
    lulu="Hello World!"
    lulu="${lulu//Hello/Bonjourno}"
    echo "$lulu"

Int

  • Declaration :
    lulu=$((42))
    or
    let "lulu=5"
    or
    declare -i lulu
    lulu="42"
  • Usage :
    lulu=$(($lulu+1)) # Add 1 to lulu
    echo $lulu

Float

  • Declaration :
    precision=3  # precision after the decimal point
    nb1=$((5))
    nb2=$((3))
    lulu=$(echo "scale=$precision; $nb1/$nb2" | bc)

Array

  • Declaration :
    array=("apple" "banana" "cherry")
  • Add an element :
    array+=("orange")
  • Usage :
    echo ${array[1]} # Displays: banana
  • Iterate on it :
    for fruit in "${array[@]}"; do
      echo $fruit
    done

Dictionary | Hashmap

  • Declaration :
    declare -A dico
  • Add an element :
    dico["France"]="Paris"
  • Usage :
    echo ${dico["France"]} # Displays: Paris
  • Iterate on it :
    for country in "${!dico[@]}"; do
      echo "The capital of $country is ${dico[$country]}"
    done

Comparisons Operators

Warning

These comparisons are used in conditions, to store the result you can do this :

c=$([[ $a -eq $b ]] && echo "true" || echo "false")

Scalar | String

  • Equal :
    $a = $b
  • Different :
    $a != $b
  • Empty :
    -z $a
  • Not Empty :
    -n $a

Int

  • Equal :
    $a -eq $b
  • Different :
    $a -ne $b
  • Lower than :
    $a -lt $b
  • Lower or Equal :
    $a -le $b
  • Greater than :
    $a -gt $b
  • Greater or Equal :
    $a -ge $b

Conditions

lulu=$((88))
if [ $lulu -lt $((50)) ]
then
  echo Lower than 50
elif [ $(($lulu%2)) -eq 1 ]
then
  echo Greater than 50 and odd
else
  echo Greater than 50 and peer
fi

You can use logic operators

lulu=$((5))
if [ $lulu -gt $((50)) ] && ! [[ $(($lulu%2)) -eq 1 ]]
then
  echo Greater than 50 and peer
else
  echo Lower than 50 or odd
fi
lulu=$((88))
if [ $lulu -eq 50 ] || [ -z $lulu ]
then
  echo Lulu is empty or equal to 50
fi

Case-block

case $# in
  2)
    name=$1
    age=$2
    ;;
  *)
    echo "Usage : ./main.sh <Name> <Age>"
    exit
    ;;
esac
echo $name is $age years old

# Script to check the existence of a file

dir="$PWD/data"
if [ -e "$dir" ]; then
  echo "There is a file or folder named 'data'"
else
  echo "There isn't a file or folder named 'data'"
fi

Loop

  • While
    lulu=10
    while [ $lulu -ge 0 ]
    do
      echo $lulu
      lulu=$(($lulu-1))
    done
    echo Happy new year !
  • For in
    names=("Jean" "Bruno" "Hugo" "François")
    for name in "${names[@]}"
    do
        echo "Hello $name"
    done
  • For index in
    for i in `seq 0 10`    # start:0 ; end:10 
    do
        echo Day $i
    done
    for i in `seq 0 2 10`   # start:0 ; step:2 ; end:10 
    do
        echo Day $i
    done

Function

Caution

In Bash, functions have side effects, changes to a variable in the body of a function directly affect the scope in which the function was called.

function ADD
{
  a=$((a+1))
}
a=5
echo $a  # Displays : 5
ADD      # Call the function 'ADD'
echo $a  # Displays : 6

Function with arguments

function add_numbers
{
  result=$(($1+$2))
}
add_numbers 5 2
echo $result  # Displays : 7

I/O

Read prompt data

# To interact with the user directly in the terminal

read -p "What's your name ? " name
read -p "How old are you ? " age
echo $name : $age

Setting up the script at launch

echo $#    # Displays the number of parameters

main.sh :

echo $1 is $2 years old

In your terminal execute :

./main.sh Lulu 90

The previous command displays : Lulu is 90 years old

Standard input redirection | Files

  • File as input data.txt :
    Lulu
    90
    main.sh :
    echo $1 is $2 years old
    In your terminal execute :
    ./main.sh < data.txt
    The previous command displays : Lulu is 90 years old
  • File as output In your terminal execute :
    echo Lulu is everywhere... > output.txt