-
Best Bash Guide Ever (apparently) - http://tldp.org/LDP/abs/html/
-
Bash Special Parameters - http://www.thegeekstuff.com/2010/05/bash-shell-special-parameters/#more-4383
$_
- Always use round brackets for numeric comparisons. With square brackets you have to use -eq instead of == ascii comparison
die () {
echo >&2 "$@"
exit 1
}
[ "$#" -eq 3 ] || die "Usage: $0 arguments "
####Check Argument Set Or Default Value
[ -z "$RPATH" ] && RPATH="/usr/bin/R"
# Grab the servername then shift
SERVER=$1
shift
counter=$((counter+1))
if (( $counter == 3 )); then
kill -9 $PID
else
counter=$((counter+1))
fi
[[ -f file ]]
[[ -d dir ]]
[[ -r entry ]]
[[ -w entry ]]
[[ -x entry ]]
[[ -n var ]] // non zero
[[ -z var ]] // zero length
let
(( j > 0 ))
typeset -i k=$1
Set exit status with exit n
Previous exit status with
for JMETERTEST in "$@"
do
<stuff>
done
Take the output from a variable and store this:
numscripts=`ls -l metrics*.rb 2> /dev/null | wc -l`
] Alternatively, use a subshell using $()
for cmd in $(ls metrics*.rb)
In this case an R package
#!/usr/bin/env bash
function checkpackage() {
output=""
output=`Rscript -e 'installed.packages(lib.loc = "/opt/mango/navigator/rlibs")' | grep "$1.*\"$1\""`
if [ -n "$output" ]; then
echo "$1: PASS"
else
echo "$1: FAIL"
fi
}
checkpackage "RNMImport"
#!/bin/bash
for i in $(find . -name *.ctl); do
directory=`dirname $i`
filename=`basename $i`
`cd $directory; execute $filename >> output.txt`
done
#!/bin/bash
function restartservice() {
service $1 stop;
RETRY_LIMIT=3
PID="$(ps -ef | grep $1 | grep java | awk ' { print $2 } ')"
counter=1
while kill -0 $PID >/dev/null 2>&1
do
echo "Waiting for tomcat to die, $counter"
sleep 3
if (( $counter == $RETRY_LIMIT )); then
kill -9 $PID > /dev/null 2>&1
else
counter=$((counter+1))
fi
done
service $1 start;
}
restartservice tomcat7
remotepids=("${remotepids[@]}" $pid)
if (( ${#remotepids[@]} > 0 )); then
echo -ne "\nStarted processes [ ${remotepids[@]} ] \n"
echo ${remotepids[@]} > $pidsfile
`date +"%m-%d-%y_%T"-$serverhost`
See: http://www.linuxjournal.com/content/return-values-bash-functions
local sets a local variable
function clean {
echo "Parameter is $1"
}
Pass variables with
clean tomcatlogdir
command > /dev/null 2>&1 &
declare -a arr=("a", "b", "c")
## now loop through the above array
for srv in "${arr[@]}"
do
echo "Handling: $srv"
done