LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Subsystem shell (https://www.linuxquestions.org/questions/programming-9/subsystem-shell-177998/)

AMMullan 05-04-2004 11:40 PM

Subsystem shell
 
Hey all :)

I'm trying to get port numbers to sit in an array in a bash script but not sure how to use subsystem shells properly. Can anyone tell me what i've done wrong in the following:

Code:

#! /bin/bash

FILE=/tmp/a.tmp

echo -n "Enter a server: "
read SERVER

nmap ${SERVER} > ${FILE}
CNT=0

cat ${FILE} | grep ^[0-9] | cut -d'/' -f 1 | cut -d' ' -f 1 | while read LINE
do
  PORT[${CNT}]=$LINE
  (( CNT += 1 ))
done

TMP=0
while [ $TMP -lt $CNT ]
do
  echo "Port-${TMP} : $PORT[${TMP}]"
  (( TMP += 1 ))
done

Thanks in advance :D

jim mcnamara 05-05-2004 03:53 PM

I can't tell what the cut statements are doing, but I changed the increment
for the loop counters.

Code:

FILE=/tmp/a.tmp

echo -n "Enter a server: "
read SERVER

nmap ${SERVER} > ${FILE}
CNT=0

cat ${FILE} | grep ^[0-9] | cut -d'/' -f 1 | cut -d' ' -f 1 | while read LINE
do
  echo $LINE                  # try this to see if you are getting data
  PORT[${CNT}]=$LINE
  let CNT=CNT+1
done

TMP=0
while [ $TMP -lt $CNT ]
do
  echo "Port-${TMP} : $PORT[${TMP}]"
  let TMP=TMP+1
done


AMMullan 05-05-2004 04:09 PM

I tried this but it still doesn't work properly - the count should go up and as it does it should add a port to the array....

I.e.

PORT[0] = 22
PORT[1] = 25

Not sure whats going on, this should just ork but thats why I think it's got something to do with the subshell...

jim mcnamara 05-05-2004 04:47 PM

What output do you get from the changes I posted?

What is the input line from the file?

AMMullan 05-05-2004 05:23 PM

From what you posted I get a list of ports but it only lists once and if I add a
Code:

echo ${CNT}
after the array setup it still says 0...

The input line from the file is the cat statement that cuts the line with the ports out and then cuts the port from line.


All times are GMT -5. The time now is 01:23 AM.