store the values from command into an array bash - arrays

svn mergeinfo --show-revs eligible http://svn.test.com/INT_1.0.0/ http://svn.test.com/DEV/ | cut -d"r" -f2 | cut -d" " -f1
6097
6099
when i put this in a script, i get only last value but not all:
#!/usr/bin/bash
src_url="http://svn.test.com/INT_1.0.0/"
target_url="http://svn.test.com/DEV/"
eligible_revs=(`svn mergeinfo --show-revs eligible $src_url $target_url | cut -d"r" -f2 | cut -d" " -f1`)
echo ${eligible_revs[#]}
output:
6099

If you are running Cygwin the line endings can mess it up
$ foo=(`printf 'bar\r\nbaz'`)
$ echo ${foo[*]}
baz

Related

jq: 1 compile error jq: error: schedule/0 is not defined at <top-level>, line 1: .Christchurch.bus-schedule.from["Weekday"] |= . + ["1646"]

I have a react application which uses a .sh file to generate a part of the application. But I have come across an error which prevents the application from reading the data files.
I've looked at versioning, possible syntax changes in the .sh file which is emap.sh
This is the .sh file
#!/bin/bash
object='{}'
find 'public/data' -type f -name '*.csv' | while read -r filename;
do
filename=$(echo "$filename" | sed 's/public\/data\///' | sed 's/ /\//')
city=$(echo "$filename" | cut -d'/' -f1)
medium=$(echo "$filename" | cut -d'/' -f2)
direction=$(echo "$filename" | cut -d'/' -f3)
date=$(echo "$filename" | cut -d'/' -f4)
time=$(echo "$filename" | cut -d'/' -f5| head -c-5)
object=$(echo $object | jq -c ".$city.$medium.$direction[\"$date\"] |= . + [\"$time\"]")
echo $object > public/available.json
done
It should be successful so that when we yarn the application, it shows up having read the data, but we get a page without data in the area using the information that needs to be processed.
The shell variables should be passed to jq in a more robust manner, e.g. along these lines:
jq -c --arg city "$city" \
--arg medium "$medium" \
--arg direction "$direction" \
--arg date "$date" \
--arg time "$time" \
'.[$city][$medium][$direction][$date] += [$time]'
As #OguzIsmail points out, though, you would probably be better off avoiding all the messiness by doing everything with just find and jq.

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.

Submitting a job on PBS with while loop, changing output file names

I have a (let's call it original) script to parse a file (~1000 lines) line by line, generate arguments to execute a C++ program.
#!/bin/bash
i = 0
while IFS='' read -r line || [[ -n "$line" ]]; do
a="$line" | cut -c1-2
b="$line" | cut -c3-4
c="$line" | cut -c5-6
d="$line" | cut -c7-8
e="$line" | cut -c9-10
f="$line" | cut -c11-12
g="$line" | cut -c13-14
h="$line" | cut -c15-16
i="$line" | cut -c17-18
j="$line" | cut -c19-20
k="$line" | cut -c21-22
l="$line" | cut -c23-24
m="$line" | cut -c25-26
n="$line" | cut -c27-28
o="$line" | cut -c29-30
p="$line" | cut -c31-32
./a.out "$a" "$b" "$c" "$d" "$e" "$f" "$g" "$h" "$i" "$j" "$k" "$l" "$m" "$n" "$o" "$p" > $(echo some-folder/output_${i}.txt)
done < test_10.txt
I want to schedule this job in a batch, so that each run is queued and ran on separate cores.
I checked the PBS and qsub writing styles. I could write a PBS file (simple one, without all options for now. Lets call it callPBS.PBS):
#!/bin/bash
cd $PBS_O_WORKDIR
qsub ./a.out -F "my arguments"
exit 0
I can call this file instead of ./a.out -------- in original script. BUT how do I pass "my arguments"? Problem is they are not fixed.
Secondly I know qsub takes -o as an option for output file. But I want my output file name to be changed. I can pass that as an argument again, but how?
Can I do this in my original script:
callPBS.pbs > $(echo some-folder/output_${i}.txt)
I am sorry if I am missing something here. I am trying to use all that I know!

how to start c program from bash script under apache

Can please anyone help me.
i have the following script:
#!/bin/bash
cache_query=$QUERY_STRING
xxx=`echo $cache_query | grep -o 'x=[0-9]*' | sed 's/x=//g'`;
yyy=`echo $cache_query | grep -o 'y=[0-9]*' | sed 's/y=//g'`;
zzz=`echo $cache_query | grep -o 'z=[0-9]*' | sed 's/z=//g'`;
let "zzz=18-$zzz"
content_type=`echo $cache_query | grep -o 'type_image=[a-z]*' | sed 's/type_image=//g'`;
db_name=`echo $cache_query | grep -o 'db=[a-z]*' | sed 's/db=//g'`;
echo "Content-type: image/"${content_type}
echo ""
case "$db_name" in
"genshtab" ) db_path="/home/bases/gena.sqlite";;
"yasat" ) db_path="/srv/ftp/upload/yasat.sqlitedb";;
esac
echo "x = " $xxx "y = " $yyy "z = " $zzz "type = " $content_type "database = " $db_name "db_path = " $db_path; #оставляю на всякий $
./tget ${db_path} ${db_name} "/mnt/tmpfs" ${content_type} ${zzz} ${yyy} ${xxx}
cat /mnt/tmpfs/${db_name}/${zzz}/${yyy}/${xxx}.${content_type}
i wrote tget program on c, so if i write
./tget /srv/ftp/upload/yasat.sqlitedb yasat /mnt/tmpfs png 17 0 1
in bash line everything go great, but if i try to call this program from bash script under apache
http://46.182.21.31/?type_image=png&db=yasat&x=2&y=1&z=15
the program doesn't start and no errors in my log,
is somebody have any ideas why?
i was resolve a trouble the next way start c program with sudo
sudo ./tget ......
and add to /etc/sudoers ability to user www-data to make sudo queries without password

Resources