简体   繁体   中英

Print multiple different sized arrays in different columns using bash script

I am trying to produce a report on the state of the linux box with some summary information. I use jps to populate multiple arrays with the names of the instances of our application that is running. I then want to print these sorted arrays in 4 columns so we can see what is running easily.

I use some conditional logic to determine which array length is the longest. Because some arrays are shorter than others, simple looping through the longest value can cause array index out of bounds issues. So I would like to print an empty string in the columns where there are no values for that row.

The code below is what I current have, but it throws "Command not found" errors when it tries to execute the command inside the loop. The idea is to try and use conditional logic to determine if the current array is out of bounds and if so just print "".

I've tried with ` ` to make it appear as code. I've tried without them as well. I've tried without the " " and with them. I've tried with ${ } around variables and without. I've also tried with ( ) around the values and without.

I'm not quite understanding what is happening behind the scenes of this code and scripting on a Windows machine code that is meant for a command line linux box is not easy to debug. Any suggestions?

printf "| %-40s | %-40s | %-40s | %-40s |\n" "Array 1:" "Array 2:" "Array 3:" "Array 4:" >> ${OUTPUT}
for i in $(seq ${maxRows}); do
    (The command below is all on one line but to make it more readable here I entered on multiple.)
    printf "| %-40s | %-40s | %-40s | %-40s |\n" "`[[ ${i} > ${#Array1[@]} ]] && ("") ||
    ("${Array1[$i]}")`" "`[[ $i > ${#Array2[@]} ]] && "" || ${Array2[$i]}`" "`[[ $i > ${#Array3[@]} ]] 
    && "" || ${Array3[$i]}`" "`[[ $i > ${#Array4[@]} ]] && "" || ${Array4[$i]}`" >> ${OUTPUT}
done

in bash, array elements without a value can be access, and will return undefined values, which will be displayed as empty string. There is no need to check for individual array indices limit.

Technically, there are multiple issues with the quoting of the printf in the loop. Alternative:

for i in $(seq ${maxRows}); do
    printf "| %-40s | %-40s | %-40s | %-40s |\n" "${Array1[$i]}" "${Array2[$i]", "${Array4[$i]}" "${Array3[$i]}" >> $OUTPUT
done

Side notes: this approach will impose a limit of maxRows, as it will require the list 1 2 3... maxRows to fit into a single line. For higher limit

for ((i=1; i<=maxRows; i++)); do
    printf "| %-40s | %-40s | %-40s | %-40s |\n" "${Array1[$i]}" "${Array2[$i]", "${Array4[$i]}" "${Array3[$i]}" >> $OUTPUT
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM