I have an issue with the script below. It uses a few sql statements in a for loop to set variables and produces a log file. The current db produces +- 1200 lines of output.
The original version is with the commented echo line, it adds one line to a text file (report.csv) for each loop (one write operation to disk).
The new intended version would append the data to an array (dcimlog), and then do only one write operation for the whole dataset in newreport.csv.
My issue is that somehow the array is not appended correctly by the child processes, so newreport.csv only contains the first line done before the for loop, and is not appended by any child.
The order of the lines is not important, but the speed of execution is, hence the "do () &" -- which I believe might be the reason for the failure...
Any idea how I can get this to work?
N=$(nproc) dcimlog=();dcimlog=("${dcimlog[@]}" "some stuff") # works for i in "${sqlanswer[@]}"; do ( echo somework #get the data from sql # echo "$DDeviceID;$NBPort;$PortNumber;$CConnectedDevice;$Connectedport;$CabinetLocation;$HeightDevice;$PositionDevice;$ZoneName;$DataCenterName" >> report.csv dcimlog=("${dcimlog[@]}" "$DDeviceID;$NBPort;$PortNumber;$CConnectedDevice;$Connectedport;$CabinetLocation;$HeightDevice;$PositionDevice;$ZoneName;$DataCenterName") # does not work ) & if [[ $(jobs -r -p | wc -l) -gt $N ]]; then wait -n fi done wait echo "${dcimlog[@]}" > newreport.csv
echo
redirected to the report file should work from the subshell, so why not leave it in?echo
, and redirect the entire loop's stdout to the file? Likefor FN in *; do (echo $BASHPID, $$, $FN) & done >result