#!/bin/bash # Function (put them first) isroot() { myUser=$(whoami) if [ $myUser = root ] then echo "You are root" else echo "You are user myUser" fi } # # Start here # # Arguments are $1 $2 $3 etc echo Program name: $0, Number of args: $# # By default arg will be all command line parameters for arg do echo $arg done # Variables mine=Hello mineOther=" World !" concated=$mine$mineOther echo Var set to $concated # Numbers let "first=1" let "first=first+1" echo First incremented to $first # for loop. Double brackets mean maths not strings for ((i=0; i<4; i++)) do # The [ ] invokes program 'test' if [ $first -eq $i ] # integer comparison then echo Found first: $first to equal i: $i elif [ $i -lt 1 ] then echo Working directory is $(pwd) else true # Do nothing fi done # Method call isroot # Take text as comand line input until THE_MARKER cat > crib-test.txt << THE_MARKER This,is,a,test on,several,lines into,a,file THE_MARKER # Simple file processing for line in $(cat crib-test.txt) do echo The line is: $line second=$(echo $line | awk -F , '{print $2}') # The -F tells awk what field separator to use, default to space echo Second field: $second done # Process processing procDetails=$(ps -Ao pid,pcpu,cmd | grep -v PID | sort -n -k 2 | tail -1 | awk '{print "Process "$1" is using highest percentage of "$2" pct with command "$3}') memDetails=$(ps -Ao pid,pmem,cmd | grep -v PID | sort -n -k 3 | tail -1 | awk '{print "Process "$1" is using highest memory of "$2" pct with command "$3}') echo Processor details: $procDetails echo Memory details: $memDetails