Echo 2 Arrays Lists in Shell Script - arrays

I have a problem with array in bash. I wish to echo 2 Arrays in a list from the fist element from the convert array til the last element. The same thing for room_minute array.
#! /bin/bash
#! /usr/bin/perl
declare -a data;
declare -a convert;
declare -a sala_minutos;
data=($(./Minutes.php 2> /dev/null | grep -P -w -o [0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\} | awk -v FS=/ -v OFS=/ '{print $2,$1,$3}'));
room_minutes=($(./Minutes.php 2> /dev/null | grep -oP '(?<=room: )[0-9]+'))
for ((i=0; i< ${#data[*]}; i++));
do
convert=($(date -d "${data[i]} 10:00:00" +%s));
done
echo ${convert[*]} ${room_minutes[*]}
Obs inside the arrays:
data = [09/03/16 09/01/16 09/02/16 09/03/16 09/04/16 09/05/16 09/06/16 09/07/16 09/08/16 09/09/16 09/10/16 09/11/16 09/12/16 09/13/16 08/25/16 08/26/16 08/27/16 08/28/16 08/29/16 08/30/16 08/31/16]
covert = [1472698800 1472785200 1472871600 1472958000 1473044400 1473130800 1473217200 1473303600 1473390000 1473476400 1473562800 1473649200 1473735600 1472094000 1472180400 1472266800 1472353200 1472439600 1472526000 1472612400]
room_minutes = [7339 8748 211 15 15927 7028 34 11112 12567 686 5 13988 11279 8465 4402 60 1 10380 8078 8422]
Thanks in Advance.

i think you want convert and room_minutes in two columns. i've changed as little as possible to accomplish this using multiline strings and paste instead of arrays. the data= and room_minutes= lines are unchanged except for stripping the outermost ()s.
#!/bin/bash
data=$(./Minutes.php 2> /dev/null | grep -P -w -o [0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\} | awk -v FS=/ -v OFS=/ '{print $2,$1,$3}');
room_minutes=$(./Minutes.php 2> /dev/null | grep -oP '(?<=room: )[0-9]+');
convert=$(echo "$data" | xargs -I{} date -d "{} 10:00:00" +%s)
paste <(echo "$convert") <(echo "$room_minutes")

#!/bin/bash
data=($(./Minutes.php 2> /dev/null | grep -P -w -o [0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\} | awk -v FS=/ -v OFS=/ '{print $2,$1,$3}'));
room_minutes=($(./Minutes.php 2> /dev/null | grep -oP '(?<=room: )[0-9]+'));
for ((i=0; i< ${#data[#]}; i++));
do
convert=($(date -d "${data[$i]} 10:00:00" +%s));
echo -e Room_Minutes ${convert[#]} ${room_minutes[$i]}
done

Related

How to grep success in a for loop

Struggling with this...
for i in `cat services.txt`
do
if ! grep -q $i; then
echo " $i Is NOT Running"
else
echo " Checking $i on `hostname`..."
ps aux | grep -i $i | awk '{print $1, $11}'| cut -d' ' -f1-2| sort
echo -e " "
sleep 4
fi
done
The block just hangs - Ive not been able to capture the success/failure of grep
If a string in services.txt is NOT found ... the script hangs... Id like for grep to skip it if not found
services.txt contain just single words
thanks!
The reason your script hangs is beacuse the command grep -q $i is waiting for an input. You can try running that command separately in a shell and verify that it prompts for an input.
Changing your command to ps aux | grep -i $i in the if statement should fix your issue.
NOTE: ps aux | grep -i $i lists grep also as one of the process. Make sure you exclude that process by piping it to another grep ps aux | grep -i $i | grep -v 'grep'
here is the working code
checkservices() {
cat >$HOME/services.txt <<EOF
ganglia
hbase
hdfs
hive
hue
livy
mapred
test-missing-service
mysql
oozie
presto
spark
yarn
zeppelin
EOF
for i in `cat $HOME/services.txt`
do
if `ps -ef | grep ^$i | grep -v grep >/dev/null`
then
i=$(echo "$i" | awk '{print toupper($0)}')
echo "$i -- is running on `hostname`"
echo ""
sleep 2
else
i=$(echo "$i" | awk '{print tolower($0)}')
echo "$i -- IS NOT running on `hostname` error"
echo ""
fi
done
}

create arrays from for loop output

I'm trying to understand what I'm doing wrong here, but can't seem to determine the cause. I would like to create a set of arrays from an output for a for loop in bash. Below is the code I have so far:
for i in `onedatastore list | grep pure02 | awk '{print $1}'`;
do
arr${i}=($(onedatastore show ${i} | sed 's/[A-Z]://' | cut -f2 -d\:)) ;
echo "Output of arr${i}: ${arr${i}[#]}" ;
done
The output for the condition is as such:
107
108
109
What I want to do is based on these unique IDs is create arrays:
arr107
arr108
arr109
The arrays will have data like such in each:
[oneadmin#opennebula/]$ arr107=($(onedatastore show 107 | sed 's/[A-Z]://' | cut -f2 -d\:))
[oneadmin#opennebula/]$ echo ${arr107[#]}
DATASTORE 107 INFORMATION 107 pure02_vm_datastore_1 oneadmin oneadmin 0 IMAGE vcenter vcenter /var/lib/one//datastores/107 FILE READY DATASTORE CAPACITY 60T 21.9T 38.1T - PERMISSIONS um- u-- --- DATASTORE TEMPLATE CLONE_TARGET="NONE" DISK_TYPE="FILE" DS_MAD="vcenter" LN_TARGET="NONE" RESTRICTED_DIRS="/" SAFE_DIRS="/var/tmp" TM_MAD="vcenter" VCENTER_CLUSTER="CLUSTER01" IMAGES
When I try this in the script section though I get output errors as such:
./test.sh: line 6: syntax error near unexpected token `$(onedatastore show ${i} | sed 's/[A-Z]://' | cut -f2 -d\:)'
I can't seem to figure out the syntax to use on this scenario.
In the end what I want to do is be able to compare different datastores and based on which on has more free space, deploy VMs to it.
Hope someone can help. Thanks
You can use the eval (potentially unsafe) and declare (safer) commands:
for i in $(onedatastore list | grep pure02 | awk '{print $1}');
do
declare "arr$i=($(onedatastore show ${i} | sed 's/[A-Z]://' | cut -f2 -d\:))"
eval echo 'Output of arr$i: ${arr'"$i"'[#]}'
done
readarray or mapfile, added in bash 4.0, will read directly into an array:
while IFS= read -r i <&3; do
readarray -t "arr$i" < <(onedatastore show "$i" | sed 's/[A-Z]://' | cut -f2 -d:)
done 3< <(onedatastore list | awk '/pure02/ {print $1}')
Better, back through bash 3.x, one can use read -a to read to an array:
shopt -s pipefail # cause pipelines to fail if any element does
while IFS= read -r i <&3; do
IFS=$'\n' read -r -d '' -a "arr$i" \
< <(onedatastore show "$i" | sed 's/[A-Z]://' | cut -f2 -d: && printf '\0')
done 3< <(onedatastore list | awk '/pure02/ {print $1}')
Alternately, one can use namevars to create an alias for an array with an arbitrarily-named array in bash 4.3:
while IFS= read -r i <&3; do
declare -a "arr$i"
declare -n arr="arr$i"
# this is buggy: expands globs, string-splits on all characters in IFS, etc
# ...but, well, it's what the OP is asking for...
arr=( $(onedatastore show "$i" | sed 's/[A-Z]://' | cut -f2 -d:) )
done 3< <(onedatastore list | awk '/pure02/ {print $1}')

Shell Script not creating an Array List

I need help with a strange problem.
This line is not creating an array list, I don't know why.
date_final=($(echo $((date_end-date_start)) | grep -o "[0-9].*")) # PROBLEM
The Whole code is below, the particular array converts a wrong date into a new date but it should be an arraylist, and it creates a single variable instead.
#! /bin/bash
cd /var/lib/zabbixsrv/externalscripts/Manager
rm Unique.txt
declare -a date_final='()'
total_count=$(cat amazon.html 2>/dev/null | jq '.meta.total_count' | grep -o "[^\"]*")
i=0;
for i in $(seq 0 $total_count)
do
#compare=($(cat amazon.html 2>/dev/null | jq ".objects[$i].service_tag" | tr ' ' '\n'))
compare=($(cat amazon.html 2>/dev/null | jq ".objects[$i].conference" | grep -o "[^\"]*" | tr ' ' '\n'))
echo -e ${compare[#]} >> /var/lib/zabbixsrv/externalscripts/Manager/Unique.txt
done
compare1=($(cat Unique.txt | uniq -c | gawk '$1==1{print $2}'))
Number_line=$(echo ${#compare1[#]}) # PROBLEMA RESOLVIDO!!!!
let Number_line-=1
#echo -e ${compare[#]}
for i in $(seq 0 $Number_line)
do
#time=$(cat amazon.html 2>/dev/null | jq '.objects[] | select(.service_tag=='${compare1[$i]}')' | jq ".connect_time" | grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")
time=$(cat amazon.html 2>/dev/null | jq '.objects[] | select(.conference=='${compare1[$i]}')' 2>/dev/null | jq ".connect_time" | grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].")
#echo -e ${compare1[$i]}
date_convert=$(date -d "$time 2 hour ago" +"%H:%M:%S")
date_start=$(date -d "$day $date_convert" +%s)
date_end=$(date +"%s")
date_final=($(echo $((date_end-date_start)) | grep -o "[0-9].*")) # PROBLEM
done
# rm Tenant.txt
#echo ${date_final[0]}
#echo -e ${date_final[#]}
tempo=1
i=0
echo -e $Numero_linha
echo -e ${date_final[#]}
for i in $(seq 0 $Number_line)
do
if ((${date_final[$i]} > $tempo)) 2>/dev/null; then
echo -e ${compare1[$i]}
fi
done
Thanks Very Much
I have used the code from your example to declare date_final and initialize date_start and date_end, and I actually get an array after executing your problematic statement.
I think your problem is you are repeatedly assigning a one-element array to your variable instead of adding elements.
Try this :
date_final+=( $((date_end-date_start)) )
The key here is usine the += operator, which appends instead of assigning. The rest is just cleanup, as you do not need grep to filter for digits (there are only digits in the result of the calculation), and the echo is not required either.

How to deny empty array indexes in bash

I wrote the bash like so:
#!/bin/bash
GAP=1
Out=$1
ResultFile=$2
len=`wc -l $Out | awk '{print $1}'`
eval "(COMMAND) &"
pid=$!
i=0
while kill -0 $pid; do
if [ -N $Out ]; then
newlen=`wc -l $Out | awk '{print $1}'`
newlines=`expr $newlen - $len`
tail -$newlines $Out > temp
IP=( $(sed -n '<SomeThing>' temp) )
host=${IP[$i]}
echo "exit" | nc $host 23
if [ "$?" -eq "0" ]; then
(
<DoingSomeThing>
) | nc $host 23 1>>$ResultFile 2>&1
fi
len=$newlen
let i++
fi
sleep $GAP
done
When the command IP=( $(sed -n '<SomeThing>' temp) ) is running in my bash maybe the result of sed command is nothing and maybe the output is ip. I want only when output of sed command get ip write it into array and when the output of sed is empty does not write it to array.
Thank you
You're not doing your script right in many ways but about your question, the quick way is to store the output first on a variable:
SED_OUT=$(sed -n '<SomeThing>' temp)
[[ -n $SED_OUT ]] && IP=($SED_OUT) ## Would only alter IP if $SED_OUT has a value.

Adding a string to the front of array loop?

#!/bin/bash
#OLDIFS=$IFS
IFS=$'\r'
fortune_lines=($(fortune | fold -w 90))
#Screen_Session=$"{mainscreen}"
Screen_Session=`screen -ls|grep "\."|grep "("|awk '{print $1}'`
Screen_OneLiner=$(screen -p 0 -S ${Screen_Session} -X stuff "`printf "${fortune_lines[#]}\r"`")
#IFS=$OLDIFS;
for var in "${Screen_OneLiner[#]}"
do
echo "${var}"
done
ok this script works (sorta). I need to at the string "say " to front of the entire array index. Currently I can only get it to print out "say " to the first line.
We need not complicate things with contraptions like arrays - we can let good old sed do the job.
#!/bin/bash
fortune_lines=$(fortune | fold -w 90 | sed 's/^/say /')
Screen_Session=`screen -ls|grep "\."|grep "("|awk '{print $1}'`
screen -p 0 -S $Screen_Session -X stuff "$fortune_lines"

Resources