Below, you can find a bash script which must manage a csv file.
#!/bin/bash
IFS=";"
while read -a line;do
printf "xxxxxxxxxxx Adresse: " >> retour.txt
printf ${line[0]} >> retour.txt
for i in ${line[@]}
do
if [ "$i" != "${line[0]}" ]
then
printf "\n port: " >> retour.txt
printf $i >> retour.txt
printf "\n" >> retour.txt
nc ${line[0]} $i >> retour.txt 2>&1
fi
done
printf "\n\n" >> retour.txt
done < Classeur.csv
Each line from the csv file contains the following things:
- an id
- a few ports which must be tested with the nc software.
When i launch the script, it stops after the first line. If i remove the following line from the code, everything is treated.
nc ${line[0]} $i >> retour.txt 2>&1
Nevertheless, the removed line contains the main operation... So, i guess it is an error from the line and i would like to know how i could go on when an error appears.
----------- 25/06/2018 ------
I have following the advices which are given by people comments. When i launch the code in debug mode, i get the following result:
IFS=';'
read -a line
printf 'xxxxxxxxxxx Adresse: '
printf xxx.xxx.xxx.238
for i in '${line[@]}'
'[' xxx.xxx.xxx.238 '!=' xx.xx.xx.238 ']'
for i in '${line[@]}'
'[' yyy1 '!=' xx.xx.xx.238 ']'
printf '\n port: '
printf yyy1
printf '\n'
nc -w 2 xxx.xxx.xxx?.238 yyyy
telnet xxx.xxx.xxx.238 yyy1
Trying xxx.xxx.xxx.238...
Connected to xxx.xxx.xxx.238.
Escape character is '^]'.
Connection closed by foreign host.
for i in '${line[@]}'
'[' yyy5 '!=' xxx.xxx.xxx.238 ']'
printf '\n port: '
printf yyy5
printf '\n'
nc -w 2 xxx.xxx.xxx.238 yyy5
telnet xxx.xxx.xxx.238 yyy5
Trying xxx.xxx.xxx.238...
telnet: connect to address xxx.xxx.xxx.238: Connection refused
for i in '${line[@]}'
'[' yyy6 '!=' xxx.xxx.xxx.238 ']'
printf '\n port: '
printf yyy6
printf '\n'
nc -w 2 xxx.xxx.xxx.238 yyy6
telnet xxx.xxx.xxx.238 yyy6
Trying xxx.xxx.xxx.238...
telnet: connect to address xxx.xxx.xxx.238: Connection refused
printf '\n\n'
read -a line
I have replaced the ip by xxx.xxx.xxx.238 and the port by values like : yyy1
It stops to the first line too.
Here is the csv file:
xxx.xxx.xxx.238;yyy1;yyy5;yyy6
xxx.xxx.xxx.82;yyy1;yyy5;yyy6
xxx.xxx.xxx.15;yyy6;yyy7;yyy0
Like you can see, it is only the first line which is used.
If i remove >> retour.txt
and add , get the following text into the file which is created:
xxxxxxxxxxx Adresse: xxx.xxx.xxx.238
port: yyy1
Trying xxx.xxx.xxx...
Connected xxx.xxx.xxx.238.
Escape character is '^]'.
port: yyy5
Trying xxx.xxx.xxx.238...
port: yyy6
Trying 1xxx.xxx.xxx.238...
It doesn't go on with the csv file second line.
----- THE FOLLOWING CODE IS THE SOLUTION -----
Finally, i arrived to solve the problem by following another way:
#!/bin/bash
IFS=";"
nbLines=$(cat classeur.csv | wc -l)
for ((j=1;j<=nbLines;j++))
do
numRow="NR=="
position=$numRow$j
row=$(awk $position classeur.csv)
IFS=" " read -ra TAB <<< $row
for cell in "${TAB[@]}"
do
ipValeur=${TAB[0]}
if [ "$cell" != "$ipValeur" ]
then
printf "\n port: " >> retour.txt
printf $cell >> retour.txt
printf "\n" >> retour.txt
nc -w 2 $ipValeur $i >> retour.txt 2>&1
else
printf "xxxxxxxxxxx Adresse: " >> retour.txt
printf $ipValeur >> retour.txt
fi
done
done