I have a shell script print function like below:
print_status()
{
echo "$1|$2|$3|$4"
paramArr=("$@")
for i in "${#paramArr[@]}"
do
if [[ -z ${paramArr[i]} ]];
then
paramArr[i]="Unknown"
fi
done
echo "${paramArr[*]}"
}
The function is supposed to print 4 values, with "|" as a delimiter. I have kept the print statement at the top of the function, just to show how the actual values to be printed.
I also wanted to check that each argument coming to this function, if is a blank value (i.e the variable actually contains nothing), then print "Unknown". I wanted to get this done via an array paramArr as above. But It doesn't work as expected.
Example :
Output1:
Running|162.103.172.46|3.345|-0.076
Running 162.103.172.46 3.345 -0.076 Unknown
Expected Output 1:
Running|162.103.172.46|3.345|-0.076
Running 162.103.172.46 3.345 -0.076 <-- With a "|" instead of space
Output2:
Not Running|||
Not Running Unknown
Expected Output2:
Not Running|||
Not Running Unknown Unknown Unknown <-- With a "|" instead of space
What exactly is going wrong/missing in my function ?
for i in "${#paramArr[@]}"is a wrong construct, to loop over array elements