0

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
vincent
  • 45
  • 1
  • 9
  • (1) What do you mean by _it stops_? Does it terminate or does it hang? (2) What is `nc`? – user1934428 Jun 22 '18 at 15:38
  • @user1934428 : it goes on until the end of the code. But it doesn't start another loop. – vincent Jun 22 '18 at 17:47
  • 1
    Save yourself some work. take `>> retour.txt` off all the individual lines and add it after `done < Classeur.csv` like `done < Classeur.csv >> retour.txt` (or maybe just as `done < Classeur.csv > retour.txt` ) – Paul Hodges Jun 22 '18 at 19:15
  • @Paul Hodges: i am a bit lost (i dont have a good level). So, i remove ">> retour.txt" everywhere and i put it after the "done". It will be "done Classeur.csv >> retour.txt". Nevertheless, will i keep all the data that i need to put into the retour.txt ? An other goal is to create a file, " retour.txt". By reading the file, i can know if the nc command works in specific cases which are defined by the other data. I need to have them inside the return file. Please, tell me if they would be in the file ? I am sorry, i am not at work. – vincent Jun 22 '18 at 19:45
  • This is not a fix for your trouble, just a way to simplify the code. Try it. ...what is `nc`? – Paul Hodges Jun 22 '18 at 19:52
  • It would be `done < Classeur.csv > retour.txt`, which means the whole loop will read the csv and write to the txt. – Paul Hodges Jun 22 '18 at 19:53
  • @Paul Hodges : it is like telnet. It permits to verify if the connection between servers is valid on specific ports. – vincent Jun 22 '18 at 19:57
  • @Paul Hodges: Ok. I will try on monday. Thank you for your answers. – vincent Jun 22 '18 at 19:59
  • Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jun 23 '18 at 05:26
  • @jw : thank you. I will try it on monday. – vincent Jun 23 '18 at 10:10
  • @vincent : Doesn't running it under `-x` provide some insight? i.e. `bash -x YOURSCRIPT`? – user1934428 Jun 25 '18 at 06:48
  • @user1934428 : The use of -x doesn't seem to give a solution. I have put the returned code and the csv which is used into my post. – vincent Jun 25 '18 at 07:40
  • @PaulHodges: I tried your advice (doesn't work) and i have put the text which is retrieved into the post. – vincent Jun 25 '18 at 08:02
  • Finally, i solved the problem and i put the solution into the post. – vincent Jun 25 '18 at 12:38
  • Apologies - it was a somewhat irrelevant aside. I was just trying to simplify the code for readability, not offering a solution to the request. I shouldn't have suggested an irrelevant edit. Mea Culpa. – Paul Hodges Jun 25 '18 at 13:22
  • Thank you to everybody for your help – vincent Jun 25 '18 at 17:45

0 Answers0