while read loop
you should try
awk '{print $2}' home/dir/file.txt | while read num
do
if [ "$num" = "0" ]; then
echo "Number is equal to zero"
else
echo "number is not equal to 0"
fi
done
for a mixed awk/bash solution.
As other have pointed out, awk redirection occur later.
It looks like you're trying to redirect the output of home/dir/file.txt | awk '{print $2}'
to the while
loop;
first I guess that the correct path should be /home/dir/file.txt
(however this is just an assumption);
second /home/dir/file.txt | awk '{print $2}'
doesn't redirect the content of /home/dir/file.txt
to awk
, while < /home/dir/file.txt awk '{print $2}'
does;
third, you're redirecting the output of the command as a file, but it's a string, and you should redirect it as it: <<< "$(< /home/dir/file.txt awk '{print $2}')"
.
Also, alternatively, you could pipe the output of the command directly to the while
loop instead: < /home/dir/file.txt awk '{print $2}' | while read num
.
while read num
do
if [ "$num" = "0" ]; then
echo "Number is equal to zero"
else
echo "number is not equal to 0"
fi
done <<< "$(< /home/dir/file.txt awk '{print $2}')"
or
< /home/dir/file.txt awk '{print $2}' | while read num
do
if [ "$num" = "0" ]; then
echo "Number is equal to zero"
else
echo "number is not equal to 0"
fi
done
Since I can't comment, just wondering, why people have not used IFS
while IFS=" " read var num year string do if [[ $num -eq 0 ]]; then echo "Number is equal to zero" else echo "number is not equal to 0" fi done < home/dir/file.txt