[Solved] problem - connecting code with external file

hello. this is the code

#!/bin/sh
total1024=0
total2048=0
total8192=0
if [ $# -eq 1 ] ; then
    if [ -e "$1" ] ; then
        while read variable
        do
            if [ echo "$variable | cut -f 2 -d/" = 1024 ] ; then
                total1024=$(( $total1024 + 1 ))
            fi
            if [ echo "$variable | cut -f 2 -d/" = 2048 ] ; then
                total2048=$(( $total2048 + 1 ))
            fi
            if [ echo "$variable | cut -f 2 -d/" = 8192 ] ; then
                total8192=$(( $total8192 + 1 ))
            fi
        done
        echo "$total1024"
        echo "$total2048"
        echo "$total8192"
    else
        echo "Wrong file name."
        break
    fi

elif [ $# -eq 0 ] ; then
    echo "No parameter was given."
    break
else
    echo "Too many parameters."
    break
fi

the external is this

John Papadakis/8192/Kavala
Peter O Toul/2048/Thessaloniki
John Pepas/1024/Thessaloniki
Tasos K/8192/Kilkis
Jason F/1024/Kozani
Telis G/2048/Serres
Mimis A/2048/Kavala

i want to count the 1024,2048 and 8192 connections and echo the total sum of each one.. for example total8192 = 2
total1024 = 2
total2048 = 3
the code is not right.. i think the problem is at these parts..

 if [ echo "$variable | cut -f 2 -d/" = 1024 ] ; then

if [ echo "$variable | cut -f 2 -d/" = 2048 ] ; then

 if [ echo "$variable | cut -f 2 -d/" = 8192 ] ; then

some help please? anyhelp will be appreciated :slight_smile:

Try changing like this:

if [ "`echo $variable | cut -f 2 -d /`" = 1024 ] ; then

this is how it was at first but it didnt work either...
the problem still remains..
this is what i write in the terminal
./b7-printstats1.sh stats.txt

stats.txt is the external file. when i write this command and press enter, the cursor changes line (goes a line down) and it stays there forever doing nothing else, and so i just terminate it by pressing Ctrl+C

#!/bin/sh
total1024=0
total2048=0
total8192=0
if [ $# -eq 1 ] ; then
    if [ -e "$1" ] ; then
        while read variable
        do
            if [ `echo "$variable" | cut -f 2 -d'/'` = 1024 ] ; then
                total1024=$(( $total1024 + 1 ))
            fi
            if [ `echo "$variable" | cut -f 2 -d'/'` = 2048 ] ; then
                total2048=$(( $total2048 + 1 ))
            fi
            if [ `echo "$variable" | cut -f 2 -d'/'` = 8192 ] ; then
                total8192=$(( $total8192 + 1 ))
            fi
        done < $1
        echo "$total1024"
        echo "$total2048"
        echo "$total8192"
    else
        echo "Wrong file name."
        break
    fi

elif [ $# -eq 0 ] ; then
    echo "No parameter was given."
    break
else
    echo "Too many parameters."
    break
fi
$ ./test.sh external_file
2
3
2

You may reduce typing effort by introducing a case statement instead of three if's:

        while read variable
        do
            case `echo "$variable" | cut -f2 -d'/'` in
                1024 ) total1024=$(( $total1024 + 1 )) ;;
                2048 ) total2048=$(( $total2048 + 1 )) ;;
                8192 ) total8192=$(( $total8192 + 1 )) ;;
            esac
        done < $1
1 Like

thank you balajesuri
this line was the problem
done < external_file
:slight_smile: