简体   繁体   English

使用 bash 脚本在不同的列中打印多个不同大小的 arrays

[英]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.我正在尝试生成有关 linux 框的 state 的报告,其中包含一些摘要信息。 I use jps to populate multiple arrays with the names of the instances of our application that is running.我使用 jps 使用正在运行的应用程序实例的名称填充多个 arrays。 I then want to print these sorted arrays in 4 columns so we can see what is running easily.然后我想在 4 列中打印这些排序的 arrays 以便我们可以轻松查看运行的内容。

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.因为一些 arrays 比其他的短,简单循环最长的值会导致数组索引越界问题。 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.我不太了解此代码的幕后发生了什么,并且在 Windows 机器代码上编写脚本,该机器代码用于命令行 linux 框不容易调试。 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.在 bash 中,可以访问没有值的数组元素,并且将返回未定义的值,该值将显示为空字符串。 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.从技术上讲,在循环中引用 printf 存在多个问题。 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.旁注:这种方法将施加 maxRows 的限制,因为它将要求列表 1 2 3... maxRows 适合单行。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用多个数组的bash脚本 - bash script using multiple arrays 具有不同参数的多个bash脚本 - Multiple bash script with different parameters 我正在尝试使用 mkvmerge 或 ffmpeg 在 bash 脚本中使用 arrays 将不同的视频文件批量转换为 mkv - I'm trying to batch convert different video files into mkv using arrays in a bash script using mkvmerge or ffmpeg 重击多个if语句以在不同的日期运行不同的脚本变体 - Bash multiple if statements to run different variants of script on different days 仅打印与bash脚本的多个字符串不同的行 - Print only lines different from several strings of a bash script 使用bash脚本将来自不同文件的两列并排合并到一个表中 - Combine two columns from different files into one table side-by-side using bash script 使用 bash 脚本(awk、sed 等)在两个不同的列中正确地将数字更改为特定名称 - Change numbers to specific name in properly step in two different columns by using bash script (awk, sed, etc) 需要使用 bash 脚本将存储在 2 个不同变量中的值导出到 csv 文件中的两个单独列 - Need to export values stored in 2 different variables to two separate columns in csv file using bash script 在bash脚本linux中的不同列中删除相同值的重复项 - Delete repetitions of same values in different columns in bash script linux bash中两个不同数组的if语句 - If statement for two different arrays in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM