Basic Bash Scripting

# Begin with #!/ and the bash location
#!/usr/bash

# extension .sh

# two possibility for run a bash script:
bash script.sh
./script.sh

bash script.sh One Two Three
# One, Two and Three are arguments. You can use then in the script
echo $1     # print first argument, One
echo $3     # print third argument, Three
echo $*     # print all the arguments
echo $#     # print arguments count, 3


# Variables

var1="hello"     # create a variable (no space around =)
$var1     # reference the variable
echo "good morning, $var1 and hi"

# Variable in variable
var2="Hi equal to $(var1)"


# Array

myarray=(1 2 3 4 5)

echo ${myarray[@]}     # return all elements

echo ${myarray[@]:1:3}     # return three elements starting from second 

echo ${#myarray[@]}     # return length

echo ${myarray[2]}     # return tird element (zero-indexing)

myarray[0]=10     # change first element

myarray+=(8)     # add a new element at the end


# IF statement

if [ condition ]; then
    # do something...
else
    # do something...
fi

if [ $x == "hello" ]; then
    echo "hello!"
else
    echo "hi!"
fi

# -eq -> equal to
# -ne -> not equal to
# -gt -> greater then
# -lt -> less then
# -ge -> greater  thenor equal to
# -le -> less then or equal to
# -e -> if file exists
# -s -> if file exists and has size greater then 0
# -r -> if file exists and is readable
# -w -> if file exists and is writable
# || -> OR
# && -> AND


# FOR loops

for x in 1 2 3
do
    # do something
done

for x in {1..5..2}     # {START..STOP..INCREMENT}
do
    echo $x
done

for ((x=2;x<=8;x+=2))     # ((start;end;increment))
do
    echo $x
done

for file in dir/*.py
do
    echo $file
done
# list all file in dir with the py extension

for file in $(ls dir/ | grep -i 'hello')
do
    echo $file
done
# list all file in dir with the word hello in filename


# WHILE statements

x = 1
while [ $x -le 10]
do
    echo $x
    ((x+=1))
done


# CASE statements

case 'VAR' in
    PATTERN1)
    COMMAND1 ;;
    PATTERN2)
    COMMAND2 ;;
    *)
    DEFAULT COMMAND ;;
esac

case $(cat $1) in
    *hello*)
    mv $1 hello/ ;;
    *hi*)
    mv $2 hi/ ;;
    *ciao*|*hola*)
    rm $1 ;;
    *)
    mv $1 others/ ;;
esac
# move file with name hello, hi in their folder, remove ciao and hola files
# and move all the other file in another folder


# Basic Functions

function_name () {
    # function code
    return # something
}

# alternative syntax:
function function_name {
    # function code
    return # something
}

function_name     # call the function


# Scheduling scripts with Cron

# cron: time-based job scheduler

echo "* * * * * python create_model.py" | crontab

# echo scheduler command into cromtab

# 5 stars to set, one for each time unit:

* * * * * bash script.sh
run every minutes forever

30 13 * * 7 bash script.sh
# run every sunday at 13:30

15,30,45 * * * * bash script.sh
# run every 15, 30 and 45 minutes

*/15 * * * * bash script.sh
# run every 15 minutes

crontab -l     # see job scheduled with cron

crontab -e     # edit your cronjob list



No comments:

Post a Comment