bash shell 脚本中数组的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27183835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Length of an array in a shell script
提问by user3627319
I am writing a shell script. I need to capture the array length. Array is the output of one command. I am trying it as : N=0
我正在编写一个shell脚本。我需要捕获数组长度。数组是一个命令的输出。我正在尝试:N = 0
for pid in $(ps -ef | grep java| egrep "tomcat|jboss|JBoss" | awk '{print }') ;
do
array[$N]="$pid"
echo "${#array[@]}"
arraylen=${#array[@]}
if [ $arraylen=1 ]
then
(sleep $sleepTime;jstack ${array[array=( $(ps -ef | awk '/java/ && /tomcat|jboss|JBoss/{print }') )
echo "${#array[@]}"
]} > jStack_${array[array=()
while read -r p; do
echo "processing: $p"
array+=("$p")
done < <(ps -ef | awk '/java/ && /tomcat|jboss|JBoss/{print }')
echo "${#array[@]}"
]}_prod.txt & )
elif [$arraylen =2]
then
(sleep $sleepTime;jstack ${array[N=0
for pid in $(ps -ef | grep java| egrep "tomcat|jboss|JBoss" | awk '{print }') ;
do
array[$N]="$pid"
N=$(( N + 1 ))
done
echo "${#array[@]}"
]} > jStack_${array[##代码##]_prod.txt & )
(sleep 10;jstack ${array[]} > jStack_${array[]}_prod.txt & )
elif [$arraylen =3]
then
(sleep $sleepTime;jstack ${array[##代码##]} > jStack_${array[##代码##]}_prod.txt & )
(sleep 10;jstack ${array[]} > jStack_${array[]}_prod.txt & )
(sleep 10;jstack ${array[]} > jStack_${array[]}_prod.txt & )
done
Please help.
请帮忙。
回答by anubhava
Use this script in BASH:
在 BASH 中使用此脚本:
##代码##EDIT:If you want to run a loop you can do:
编辑:如果你想运行一个循环,你可以这样做:
##代码##回答by gman
The Problem with your script is that you never incremented the index "N".The following script may solve your problem.
您的脚本的问题在于您从未增加索引“N”。以下脚本可能会解决您的问题。
##代码##