Check number of Params
-
#Check the input paramaters are correct
-
if [[ $# != 2 ]]
-
then
-
echo "ERROR!!!"
-
exit 1
-
fi
File Exists File Exist
-
if [[ -f ${LIVECUSTOMPLL} ]]
-
then
-
echo "File Exists"
-
else
-
echo "File Missing"
-
exit 1
-
fi
Dir Exists
-
if [ -d "${DIR_SVN_COMP}/${PIPNAME}" ]; then
-
echo "Sucess"
-
else
-
echo "Fail"
-
echo "Please check the pip name is correct"
-
exit 1
-
fi
Write Permissions
-
if [ -w "${DIR_SVN_COMP}/${PIPNAME}" ]; then
-
echo "Sucess"
-
else
-
echo "Fail"
-
echo "You can't write to this dir"
-
exit 1
-
fi
Enviroment Var contains something
-
if [[ $(echo ${TWO_TASK} | grep -i "SOMETHING" | wc -l ) = 1 ]]
-
then
-
echo "TWO_TASK contains SOMETHING"
-
exit 1
-
fi
Param has one of two values
-
parm_two_ok=0
-
for X in "VALUE1" "VALUE2"
-
do
-
if [[ "${X}" = "${2}" ]]
-
then
-
parm_two_ok=1
-
fi
-
done
-
if [[ ${parm_two_ok} = 0 ]]
-
then
-
echo "Parameter 2 is not set correctly"
-
echo "It should be VALUE1 or VALUE2 only"
-
exit 1
-
fi
Compare two files
-
CURSETUP=X
-
cmp ${FILEONE} ${FILETWO} 1>/dev/null 2>&1
-
if [[ $? -eq 1 ]]
-
then
-
echo "They are diffrent"
-
else
-
echo "Files match"
-
fi
Substring
-
string="this is a substring test"
-
substring=`echo $string| cut -c 11-19`
-
echo "String:"$string
-
echo "subString:"$substring
after it is run $substring = substring
Command as VAR 2
-
string="this is a substring test"
-
substring=$(echo $string| cut -c 11-19)
-
echo "String:"$string
-
echo "subString:"$substring
Last Command Failed
-
T_CMD="svn update $AIA_HOME/AIAMetaData --non-interactive"
-
${T_CMD}
-
if [ 0 != $? ]; then
-
echo "Error - Was unable to update the AIAMetaData directory"
-
echo " ${T_CMD} failed"
-
exit 1
-
fi
Log running user
-
echo $(date) - Start > /tmp/some.log
-
echo $(date) - Running as $(whoami) >> /tmp/some.log
-
echo $(date) - End >> /tmp/some.log
Mutli dimensional arrays
-
#!/bin/bash
-
#make it exit on error
-
set -e
-
-
FILES=()
-
FILES_N=0
-
FILES_GRPID=()
-
FILES_ARTID=()
-
FILES_VERSION=()
-
-
function AddFile {
-
FILES_N=$((${FILES_N} + 1))
-
FILES+=(${FILES_N})
-
FILES_GRPID[${FILES_N}]=${1}
-
FILES_ARTID[${FILES_N}]=${2}
-
FILES_VERSION[${FILES_N}]=${3}
-
}
-
-
AddFile Art1 Grp1 Ver1
-
AddFile Art2 Grp2 Ver2
-
AddFile Art3 Grp3 Ver3
-
AddFile Art4 Grp4 Ver4
-
AddFile Art5 Grp5 Ver5
-
-
echo "Release from Maven start"
-
-
for i in "${FILES[@]}"
-
do
-
echo "Grp:=${FILES_GRPID[${i}]}"
-
echo "art:=${FILES_ARTID[${i}]}"
-
echo "ver:=${FILES_VERSION[${i}]}"
-
echo "------------"
-
done
-
-
echo "Release from Maven end"
-
-
exit 0
Repeat a command based on output from another command
Command
-
for a in $(ls); do echo $a; done
-
for a in $(docker images | grep metcarob/dockjob | awk '{print $3}'); do docker rmi $a; done
Select 3rd column of output
-
docker images | grep metcarob/dockjob | awk '{print $3}'
Search in file displaying preceeding or following text
With lines after
-
echo $FILENAME | grep -A 3 SearchText
With lines before
-
echo $FILENAME | grep -B 3 SearchText
Links
http://www.arachnoid.com/linux/shell_programming.html
http://stackoverflow.com/questions/428109/extract-substring-in-bash
http://tldp.org/LDP/abs/html/fto.html - file test operators
RJM Article Type
Quick Reference