#!/bin/bash

########################################################
# Basic stuff
########################################################

echo "Basic variable assignment:"
my_var="Hello world"

echo "Basic variable use:"
new_var=$my_var
echo $my_var
echo "my_var is $my_var"
echo 'my_var is $my_var'

echo "Capturing command output:"
result=$(echo "my_var is $my_var")
echo $result

echo "Redirection to file (hello.txt):"
echo "Hello World!" > hello.txt
echo "Goodbye World!" >> hello.txt

# Pipe results of one command to another
# qstat | grep " H "

#  View stdout and save to file
# qstat | tee current_queue.txt

########################################################
# Arithmetic
########################################################

echo "Integer arithmetic:"
meaning_of_life=$((6 * 7))
echo $meaning_of_life

echo "Floating point arithmetic requires an external program"
big=189.0
small=4.5
echo "$big $small" | awk '{print $1/$2}'
echo "print $big/$small" | python

########################################################
# String manipulation
########################################################
echo "Search and replace in string"
infile="myjob.in"
outfile=${infile/.in/.out}
echo $outfile

echo "Default value for variable:"
initialfile=""
infile="${initialfile:-job1}".in"
echo $infile
initialfile="bigjob1"
infile="${initialfile:-job1}".in"
echo $infile

########################################################
# For loops
########################################################

echo "for each loop:"
list=$(ls)
for item in $list; do
   echo $item
done
echo "C-style loop:"
for ((i=0; i<10; i++)); do
   echo $i
done

########################################################
# if, elif, else
########################################################

echo "if: String comparisons:"
var="Two"
if [ $var == "One" ]; then
   echo "The answer is one"
elif [ $var == "Two" ]; then
   echo "The answer is two"
else
   echo "I do not know the answer"
fi

echo "if: arithmetic comparison:"
var=1
if (( $var == 1 )); then
   echo "The answer is one"
elif (( $var > 2 )); then
   echo "The answer is greater than one"
else
   echo "I do not know the answer"
fi

########################################################
# Arrays
########################################################

echo "Basic array usage:"
array=(red green blue yellow orange)
echo ${#array[@]}
echo ${array[2]}
array[5]="pink"
echo ${#array[@]}

echo "Looping over arrays:"
for ((i=0; i<${#array[@]}; i++)); do
   echo ${array[$i]}
done

# Generate array from lines in file
# IFS=$'\n' lines_array($(<data.txt))

echo "Generate array from string with elements separated by spaces:"
line="4 3 5 10 6 12"
read -ra array <<< "$line"
for ((i=0; i<${#array[@]}; i++)); do
   echo ${array[$i]}
done

echo "Generate array from string with elements separated by commas:"
line="4,3,5,10,6,12"
IFS="," read -ra array <<< "$line"
for ((i=0; i<${#array[@]}; i++)); do
   echo ${array[$i]}
done

########################################################
# printf
########################################################
pi=3.14159265359
printf "pi is %.2f\n" $pi


