I have a port number file map.ini:
50051=1
50052=1
50053=1
50054=1
50055=1
50056=1
and a script sample.sh whose contents are:
#!/bin/bash
file=map.ini
while IFS='=' read -r port varPortStatus
do
if [[ $varPortStatus -eq "1" ]]; then
printf "Available port is %s" $port
printf "Status is %d." $varPortStatus.
return 0
fi
done < "$file"
echo "No port is available"
Expected output I need is:
Available port is 50051
Status is 1
I can do this using sed
or cut
command manipulation. But I need to understand with IFS here. I am getting output as:
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
")syntax error: invalid arithmetic operator (error token is "
No port is available
Please tell where am I doing wrong?
from the output seems there's may be a dos line ending \r in map.ini or in sample.sh, try dos2unix map.ini, or cat -ve map.ini to verify.
if the input file can't be modified \r
character can be removed in bash
while IFS='=' read -r port varPortStatus
do
varPortStatus=${varPortStatus%$'\r'}
if [[ $varPortStatus -eq "1" ]]; then
printf "Available port is %s\n" $port
printf "Status is %d.\n" $varPortStatus
fi
done < "$file"
Note also fixes \n
added to format, .
removed after $varPortStatus
and return
valid only in a function or a sourced script.