This page will cover more BASH scripting methods, following on from the legacy site pages.
JV2 Edit LRS Pipeline code Jenkins with Git Git2 bash2 bash3 bash4 Git setup analysis injectors scripts display shells repository Jenkins x2 LR from cygwin Groovy! bash5 Calling Bamboo Bamboo Calling
#clear the injectors disc space (the timeouts and tries limit is in case the box has gone)
wget --no-check-certificate --timeout=10 --tries=1 http://54.xxx.yyy.123/job/clear_z_drive/build?delay=0sec
wget --no-check-certificate --timeout=10 --tries=1 http://54.yy.xx.202/job/clear_z_drive/build?delay=0sec
wget --connect-timeout=900 --dns-timeout=900 --read-timeout=900 --timeout=900 https://bamboo.adm.server.net/deploy/viewEnvironment.action?id=59604996
Sometimes we want to knock off a character or two from a line:
grep -o '{[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ucs_one_hour.lrs | cut -c 2-
echo "somestring1" | rev | cut -c 2- | rev
The 'rev' reverses the string if we want to do the other end. Experiment to find what you need.
TERMS=$(echo ${graphs} | awk '{ print NF }')
echo TERMS = $TERMS
if [ ! "${graphs}" ]; then
echo TERMS empty. Will include all graphs
fi
if [ "$TERMS" != "0" ]; then
echo TERMS test. Will include specified graphs
fi
for ((i=1;i<=TERMS;i++)); do
val=$(echo ${graphs} | awk -v i="$i" '{ print $i }')
echo $val
echo ____
done
echo after awk loop
You are getting confused between $i the shell variable and $i the ith field in awk. You need to pass in the value of shell variable to awk in using -v:
#!/bin/bash
for i in {1..5}; do
<command1> | awk -v i="$i" '{print $i}' | <command2>
done
This will let command2 process each column from the output of command1 separately.
how to use awk to print columns in a loop
if [ ! -f rep85.txt ]; then
echo "rep85.txt File not found!"
exit 0
fi
echo after rep85 check
--------------------------------------------------------
declare -a myarray
let i=0
ready="no"
while IFS=$'\n' read -r line_data; do
if echo "${line_data}" | grep -q ':'
then
echo got a line - ${line_data}
myarray[i]="${line_data}" # Populate array.
((++i))
else
echo not found a ':' on this line
fi
if [ "${line_data}" = "================================" ] ; then
#echo "yes"
ready="yes"
fi
if [ "${line_data}" = "analysis report generator finished" ] ; then
#echo "no"
ready="no"
fi
if [ "$ready" = "yes" ] ; then
myarray[i]="${line_data}" # Populate array.
((++i))
fi
done < rep85.txt
grep -o '{[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ${scenario} | cut -c 2- > injectors_original.txt
cat injectors_original.txt
injectors_now=$(cat injectors_original.txt)
TERMS=$( echo ${injectors_now} | awk 'BEGIN {OFS="\n"} {FS=" "} { print NF }')
for ((i=1;i<=TERMS;i++)); do
val=$(echo ${injectors_now} | awk -v i="$i" 'BEGIN {OFS="\n"} {FS=" "} { print $i }')
echo injector $i currently is $val
echo ____
done
echo after awk loop
NOTE: This is a technique I've used when working with other special characters with 'sed'. Working with dots is tricky, so replace all dots with '@', then do your sed, then put them back. Do this in the file AND the variables used.
########################## replace an injector ############################
#NEW directory update to cygwin...
#scenario1="Z:\GitHub\PerformanceTesting\scenarios\allweb\allweb_run_ci6.lrs"
scenario1=${scenario}
echo $scenario1
scenario2=$(echo ${scenario1} | sed -e 's~\\~/~g' | sed -e 's~:~~g')
echo $scenario2
scenario3=$(echo /cygdrive/${scenario2})
echo $scenario3
old1=$(echo ${old_inj[$i]} | sed 's#\.#@#g')
echo ${old1}
new1=$(echo ${new_inj[$i]} | sed 's#\.#@#g')
echo ${new1}
sed -i 's#\.#@#g' ${scenario3}
sed --in-place "s#${old1}#${new1}#g" ${scenario3}
sed -i 's#@#\.#g' ${scenario3}
########################## replace an injector ############################
grep -o '{[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' ${scenario} | cut -c 2- > injectors_original.txt
cat injectors_original.txt
injectors_now=$(cat injectors_original.txt)
TERMS=$( echo ${injectors_now} | awk 'BEGIN {OFS="\n"} {FS=" "} { print NF }')
for ((i=1;i<=TERMS;i++)); do
val=$(echo ${injectors_now} | awk -v i="$i" 'BEGIN {OFS="\n"} {FS=" "} { print $i }')
echo injector $i currently is $val
old_inj[$i]="${val}"
echo ____
done
echo after first awk loop
num_machines=$( echo ${injectors} | awk 'BEGIN {OFS="\n"} {FS=" "} { print NF }')
for ((i=1;i<=num_machines;i++)); do
val=$(echo ${injectors} | awk -v i="$i" 'BEGIN {OFS="\n"} {FS=" "} { print $i }')
echo val is ${val}
new_inj[$i]="${val}"
echo ___________
done
echo after SECOND awk loop