LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Random file lines directed to a new file. In script an error. In command line no err (https://www.linuxquestions.org/questions/programming-9/random-file-lines-directed-to-a-new-file-in-script-an-error-in-command-line-no-err-487454/)

leventis 09-27-2006 12:32 PM

Random file lines directed to a new file. In script an error. In command line no err
 
Sorry for the longs subject.. I ment to make it descriptive

By performing the following command on the command prompt no error is received. The job is performed correctly.

cat test1 |while read l ; do echo "0$RANDOM $l" ; done|sort -n -o test1.shi

If I put the following line in a script I receive the following error.

redirection error: cannot duplicate fd: Too many open files

Full script code
Code:

#Sort text file
sort()
{
        sort $filename.on > $filename.on.done
        sort $filename.off > $filename.off.done
        rm -rf $filename.on
        rm -rf $filename.off
        mv $filename.on.done $filename.on
        mv $filename.off.done $filename.off
        rm -rf $filenameBAK
}


if [ $# -lt 1 ]
then
        #call usage function
        usage
fi

#Variables
filename=$1
filenameBAK="$1BAK"
debug="echo"
#debug=":"


cat $filename |
while read line #read each line of the file into variable line
do 

        #randomize text file
        #ERROR EHRE~~~
        cat test1 |while read l ; do echo "0$RANDOM $l" ; done|sort -n -o test1.shi
       
       
        #Generate random port
        rand=0
        RANGE=65535 #total ports
        FLOOR=1025 # greater than 1024 (eliminating root ports)
        while [ "$rand" -le $FLOOR ]
        do
                rand=$RANDOM
          let "rand %= $RANGE"  # Scales $number down within $RANGE.
        done
        $debug "Random number between $FLOOR and $RANGE ---  $rand"
        #end of generate random port


        #Split ip from host name to use it
        oldifs="$IFS"

        $debug "input line is :$line:"
              IFS="${IFS}=" #IFS BEFORE USING!
        set -- $line #use line splited from ip=dns to ip dns ($1 $2)

        ip=$1
        dns=$2
             
        $debug "IP: $1"
        $debug "DNS: $dns"
       
        IFS="$oldifs"
        #End of splitting ip and dns

        #Search crafted string and execute command
        #Special
        # -p random number
        # -i time (waittime, 20 secs)
        #-s port (source port)
        #--keep
        #./hping2 -c 1 -A -V  -p $rand -i time
        #Send one packet, ACK, Verborse, at random port, every 20 secs, source port 80
        #./hping2 -c 1 -A -V -p $rand -i 20 -s 80 --keep $ip
        # Find: 1 packets tramitted, 1 packets received, 0% packet loss
       
        status="" #ON/OFF
        #hping output is not the statistics.. its the response
        #ACK returns RST flag
        ./hping2 -c 1 -A -V -p $rand -i 20 -s 80 --keep $ip  |grep "flags=R"

        # $? stands for last commands exit status
        # If its 0 then grep was successfull and host is alive :)
        if [ "$?" -eq "0" ]; then
       
                status="ON"
                echo "$status $ip=$dns" >> $filename.on
        else
                status="OFF"
                echo "$status $ip=$dns" >> $filename.off
        fi
        $debug "Status: $status"
        #End of search crafted string

        sort #call sort funtion
       
done

Thanks in advance!

leventis 09-28-2006 07:16 AM

Stupid me!
 
I had created a function called sort. So the script instead of calling the sort command would call the sort function.

:)


All times are GMT -5. The time now is 03:43 AM.