Eval in for with two arguments - eval

I wrote two function in a script
for TYPE in $LIST_TYPE
do
egrep -v 'dc02|vh|dc03kh00|r0' $REPORT_37 | grep CP_ASK | grep $DC |awk -F, -v type=$TYPE '$NF == type { print $1 }' > ./$DC_'type_'$TYPE
EMPTY_TYPE_$TYPE=$(cat $DC'_type_'$TYPE | wc -l)
done
the second will use in an other for EMPTY_TYPE_$TYPE
so I did
for TYPE in $LIST_TYPE
do
eval echo \$EMPTY_TYPE_$TYPE
egrep -v 'dc02|vh|dc03kh00|r[0-2]' $REPORT_33 | grep NOT | grep $DC |awk -F, -v days=$DAYS -v version=$VERSION -v type=$TYPE '{if (($(NF-15) > days || $(NF-02)!= version) && ($(NF-20) == type)) print $1}' | eval tail -n \ EMPTY_TYPE_$TYPE > ./$DC'_TO_WORK'
done
the problem is that I get error
awk -F, -v days=180 -v version=6.7 -v type=E '{if (($(NF-15) > days || $(NF-02)!= version) && ($(NF-20) == type)) print $1}' + eval tail -n '$EMPTY_TYPE_E'
when I run only the command (not in bash script)
egrep -v 'dc02|vh|dc03kh00|r[0-2]' report_33.20170720.075339 | grep NOT | grep $DC |awk -F, -v days=$DAYS -v version=$VERSION '{if (($(NF-15) > days || $(NF-02)!= version) && ($(NF-20) == "A")) print $1}' | eval tail -n \$EMPTY_TYPE_$TYPE
it works.

in the first function
EMPTY_TYPE_$TYPE=$(cat $DC'type'$TYPE | wc -l) should be
eval EMPTY_TYPE_$TYPE=$(cat $DC'type'$TYPE | wc -l)
that was the issue

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
}

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.

Echo 2 Arrays Lists in Shell Script

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

Concat Two Command Result in File

I have two commands:
cat BIG_DATAfinal.txt | grep "STATUS" | awk '{print $5}'
cat BIG_DATAfinal.txt | grep "start" | awk '{print $3}' | sed 's/time;//g'
I want concat this two command in a file.
example:
STATUS REPORT FOR JOB: CargaDestino
Generated: 2016-06-17 10:52:14
Job start time;2015-03-30 13:11:45
Job end time;2016-06-17 10:52:14
Job elapsed time;10677:40:29
Job status;99 (Not running)
The expected result would be:
CargaDestino;2015-03-30
Thanks a lot!
For example:
a=`cat BIG_DATAfinal.txt | grep "STATUS" | awk '{print $5}'`
b=`cat BIG_DATAfinal.txt | grep "start" | awk '{print $3}' | sed 's/time;//g'`
echo "$a;$b"
You can do this with a single awk program:
awk -v OFS=";" '
/STATUS/ {status=$5}
/start/ {split($3, a, /;/); start=a[2]}
status && start {print status, start; status=start=""}
' BIG_DATAfinal.txt
If the output looks like ;2015-03-30o, then your file has \r\n line endings, and you should do this:
sed 's/\r$//' BIG_DATAfinal.txt | awk -v OFS=";" '
/STATUS/ {status=$5}
/start/ {split($3, a, /;/); start=a[2]}
status && start {print status, start; status=start=""}
'

Bash: Script for finding files by mime-type

First, I am not experienced in scripting, so be gentle with me
Anyway, I tried making a script for finding files by mime-type ( audio, video, text...etc), and here's the poor result I came up with.
#!/bin/bash
FINDPATH="$1"
FILETYPE="$2"
locate $FINDPATH* | while read FILEPROCESS
do
if file -bi "$FILEPROCESS" | grep -q "$FILETYPE"
then
echo $FILEPROCESS
fi
done
It works, but as you could guess, the performance is not so good.
So, can you guys help me make it better ? and also, I don't want to rely on files extensions.
Update:
Here's what I am using now
#!/bin/bash
FINDPATH="$1"
find "$FINDPATH" -type f | file -i -F "::" -f - | awk -v FILETYPE="$2" -F"::" '$2 ~ FILETYPE { print $1 }'
Forking (exec) is expensive. This runs the file command only once, so it is fast:
find . -print | file -if - | grep "what you want" | awk -F: '{print $1}'
or
locate what.want | file -if -
check man file
-i #print mime types
-f - #read filenames from the stdin
#!/bin/bash
find $1 | file -if- | grep $2 | awk -F: '{print $1}'
#!/usr/bin/env bash
mimetypes=$(sed -E 's/\/.*//g; /^$/d; /^#/d' /etc/mime.types | uniq)
display_help(){
echo "Usage: ${0##*/} [mimetype]"
echo "Available mimetypes:"
echo "$mimetypes"
exit 2
}
[[ $# -lt 1 ]] && display_help
ext=$(sed -E "/^${1}/!d; s/^[^ \t]+[ \t]*//g; /^$/d; s/ /\n/g" /etc/mime.types | sed -Ez 's/\n$//; s/\n/\\|/g; s/(.*)/\.*\\.\\(\1\\)\n/')
find "$PWD" -type f -regex "$ext"

Resources