[英]How to deny empty array indexes in bash
I wrote the bash like so: 我这样写bash:
#!/bin/bash
GAP=1
Out=$1
ResultFile=$2
len=`wc -l $Out | awk '{print $1}'`
eval "(COMMAND) &"
pid=$!
i=0
while kill -0 $pid; do
if [ -N $Out ]; then
newlen=`wc -l $Out | awk '{print $1}'`
newlines=`expr $newlen - $len`
tail -$newlines $Out > temp
IP=( $(sed -n '<SomeThing>' temp) )
host=${IP[$i]}
echo "exit" | nc $host 23
if [ "$?" -eq "0" ]; then
(
<DoingSomeThing>
) | nc $host 23 1>>$ResultFile 2>&1
fi
len=$newlen
let i++
fi
sleep $GAP
done
When the command IP=( $(sed -n '<SomeThing>' temp) )
is running in my bash maybe the result of sed
command is nothing and maybe the output is ip
. 当我的bash中运行命令
IP=( $(sed -n '<SomeThing>' temp) )
, sed
命令的结果可能为IP=( $(sed -n '<SomeThing>' temp) )
,输出可能是ip
。 I want only when output of sed command get ip write it into array and when the output of sed is empty does not write it to array. 我只希望当sed命令的输出获取ip时将其写入数组,而当sed的输出为空时不将其写入数组。
Thank you 谢谢
You're not doing your script right in many ways but about your question, the quick way is to store the output first on a variable: 您不是通过多种方式正确地执行脚本,而是关于您的问题,快速的方法是首先将输出存储在变量中:
SED_OUT=$(sed -n '<SomeThing>' temp)
[[ -n $SED_OUT ]] && IP=($SED_OUT) ## Would only alter IP if $SED_OUT has a value.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.