I am trying to find an example for reading an array instead of reading a file and send out an email in ksh. Can you please help is that possible?
Algorithm
#!/bin/ksh
i=0
set -A ARR
if [ a -ne b ]
then
let i=$[$i+1]
ARR[$i]="A does n't match with B"
fi
if [ p -ne q ]
then
let i=$[$i+1]
ARR[$i]="P does n't match with Q"
fi
-- send an single email reading the array
Do i need to read the array in a loop? it's not working
#!/bin/ksh
i=0
set -A ARR
if [ a -ne b ]
then
let i=$[$i+1]
ARR[$i]="A does n't match with B"
fi
if [ p -ne q ]
then
let i=$[$i+1]
ARR[$i]="P does n't match with Q"
fi
while [ $i -eq ${#ARR[*]} ]
do
IFS=$'\n' echo "${ARR[@]}" | mailx -s "Hello" xxx@abc.com
i=$i-1
done
#!/bin/ksh
i="0"
a="1"
b="2"
p="3"
q="4"
set -A ARR
if [ a -ne b ]
then
ARR[$i]="A does n't match with B"
fi
if [ p -ne q ]
then
i=$i+1
ARR[$i]="P does n't match with Q"
fi
#IFS=$'\n' echo "${ARR[@]}" | mailx -s "Hello" abc@xyz.com
echo "${ARR[@]}" | mailx -s "Hello" abc@xyz.com
But how do i print new line after each array element?
printf and echo are not substitutes for each other, they work very different ways. echo just prints what you give it, printf can format it in various ways.
If you want to print several newlines, printf is a good bet. You don't get echo -e everywhere, but printf works nearly the same way wherever you go.
If you want to print a string without a newline, printf can do that everywhere too. printf "%s" "mystring"
If you want to format numbers, printf can also do that for you.
printf "%.3f\n" 10
10.000
In fact it can do quite a few things, more than I have time to mention here. See man printf for details.