Skip to content

Instantly share code, notes, and snippets.

View odadoda's full-sized avatar

Øyvind Dahl odadoda

View GitHub Profile
@odadoda
odadoda / awk-NR.sh
Created March 7, 2014 12:51
Fetching one line at a time with awk
current_line=$(awk "NR==$current_row" "$FILE")
@odadoda
odadoda / zab-loop.sh
Created March 7, 2014 12:50
The loop in zab
## wait for input
read -n 1 -s INPUT
# Valid input:
#
# m: next line / slide
# n: previous slide
# q: quit presenter
while [ "$INPUT" != "q" ]; do
@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."
@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 / 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 / ls-bin
Created February 28, 2014 11:27
write out content for global variable "path"
echo $PATH
@odadoda
odadoda / ls-bin
Created February 28, 2014 11:24
list commands in /bin
ls /bin
# 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 / while-syntax.sh
Created February 28, 2014 15:21
while syntax
while command ; do
something
done
@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