Skip to content

Instantly share code, notes, and snippets.

View odadoda's full-sized avatar

Øyvind Dahl odadoda

View GitHub Profile
@odadoda
odadoda / bash-array
Last active August 29, 2015 13:56
Bash array operations
# Print the third value in the array
echo ${MY_ARRAY[2]}
# To print everyting in the array:
echo ${MY_ARRAY[@]}
# Get array length
echo ${#MY_ARRAY[@]}
# Add more elements
@odadoda
odadoda / if.sh
Last active January 9, 2018 12:43
Bash if
if [ test-commands ]; then
#do something
elif [ more-commands ]; then
#do more something]
else
#do something else
fi
@odadoda
odadoda / if-ls.sh
Created February 28, 2014 12:42
If test
if ls ; then
echo “listed all the files”
else
echo “error”
fi
@odadoda
odadoda / if-foo.sh
Created February 28, 2014 12:42
If test string
if [ “Foo” == “Foo” ] ; then
echo “Its equal”
fi
@odadoda
odadoda / if-100.sh
Created February 28, 2014 12:43
If int equals 100
$NUMBER=100
if [ $NUMBER -eq 100 ] ; then
echo “Number is equal”
fi
@odadoda
odadoda / while-syntax.sh
Created February 28, 2014 15:21
while syntax
while command ; do
something
done
# for-each
MY_ARRAY=("one", "two", "three")
for i in MY_ARRAY ; do
echo $i
done
# extended for-loop
for (( i=0; i "-lt" 3; i++ )) ; do
echo $i
@odadoda
odadoda / case.sh
Last active August 29, 2015 13:57
The case builtin in bash
case $VAL in
( 1 ) echo "The val is 1" ; echo "Echoing out some more text" ;;
( 2 ) echo "The val is 2" ;;
( * ) echo "The value is not 1 or 2" ;;
esac
# There is a shorter version to case aswell
case $OS in
ubuntu ) echo "Running ubuntu" ;;
centos ) echo "Running centos" ;;
@odadoda
odadoda / subroutine.sh
Last active August 29, 2015 13:57
subroutines in bash
# declare a subrutine
kewl(){
echo "kewl!";
#access arguments in $1, $2 ... etc
echo "$1"
# declare a local variable
local LOCAL_IN = 1
}
@odadoda
odadoda / check-file-input.sh
Last active August 29, 2015 13:57
check file
# check if param 1 is not empty and if the file exists
if [ -z $1 ] ; then
echo "Param 1 missing. Must have a file to present."
exit
else
FILE="$1"
fi
if [ ! -f $FILE ] ; then
echo "File not found."