[Solved] problem - connecting code with external file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] problem - connecting code with external file
# 1  
Old 03-15-2012
[Solved] problem - connecting code with external file

hello. this is the code

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
Code:
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..
Code:
 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 Smilie
# 2  
Old 03-15-2012
Try changing like this:

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

# 3  
Old 03-15-2012
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
# 4  
Old 03-15-2012
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 < $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

Code:
$ ./test.sh external_file
2
3
2

You may reduce typing effort by introducing a case statement instead of three if's:
Code:
        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


Last edited by balajesuri; 03-15-2012 at 03:09 PM..
This User Gave Thanks to balajesuri For This Post:
# 5  
Old 03-15-2012
thank you balajesuri
this line was the problem
done < external_file
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] File Splitting And Renaming Problem

OK So I Recently Bought A whatbox Seed-box Act!!:cool: I am connected to whatbox via SSH!!! Now i have downloaded a movie and renamed it to 2yify.mp4 (800MB):o When I TYPE the command to split it which is:) split -b 400m 2yify.mp4 It gets renamed into two parts with different names... (4 Replies)
Discussion started by: anime12345
4 Replies

2. Solaris

[Solved] Issues connecting to storage array

I have two T2000 servers connected to sun storage array StorEdge 3310 as follows: CH0 connected to SNGL BUS CONF CH1 connected to HOST1 CH2 open CH3 connected to HOST2 DUAL BUS CONF open I have created some luns which I can mount and use on HOST1 but from HOST2 I cant see them ... (3 Replies)
Discussion started by: aliyesami
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Problem bash if file exists then < do...> else <do...>

Hi ! I have a problem with an if/else statement in bash. I want to check if the file exists before running a task (for example here, counting lines), and if not I need to create an empty output file if then wc -l input.tab > output.temp else >output.temp fi The problem is even if the... (2 Replies)
Discussion started by: beca123456
2 Replies

4. UNIX for Dummies Questions & Answers

[Solved] How to refer to input file in code?

This may be a dumb question, but googling is not giving me an answer. I'm trying to figure out how to refer to an input file in my code. Lets say i run a script in bash: "sh shellscript.sh inputfile" (Inputfile will be variable...whatever file i run the script on) I wanted to make... (5 Replies)
Discussion started by: legato22
5 Replies

5. UNIX for Dummies Questions & Answers

Problem connecting with Reflection X 14.1

Other people at work are able to connect but I am not. I am not sure if there is a setting that I am missing. Connecting from Win7 to Solaris 10. Connection attempt log: gnome-session Connecting 144.243.90.235 via TELNET Thu Mar 29 15:15:32 2012 login: *** Password: ******* Last login: Thu... (2 Replies)
Discussion started by: SIFT3R
2 Replies

6. Shell Programming and Scripting

[Solved] Value of a variable is not recognised for commands comes from external file

Hi, my script is setting a variable with value and this variable is present in my another command that is coming from external file and this command is internally called after this variable is set. but while execution of this command, the value is not retrieved properly. say, my script... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

[solved] SSH connecting to one server but failed in another

Hello I have connected to a remote server via ssh but its not working now ,but the same method I applied in another server and its working fine now Below are the logs For Successfully connected server debug1: Authentications that can continue:... (1 Reply)
Discussion started by: Pratik4891
1 Replies

8. AIX

problems connecting with exceed (solved)

Hi, We have a user who cannot connect to the aix system anymore with hummingbird exceed. Our unix servers are aix 5.3. She is running windows xp and using hummingbird 12. She used to not have any problem, but now when she tries to use xstart she gets an error 'unable to connect to host using... (0 Replies)
Discussion started by: fwellers
0 Replies

9. HP-UX

Connecting To An External Network Using A logical (PACKAGE) IP Address

Hie everyone, I am currently facing a problem whereby I can not connect to an external network from a package ip address on a HP-UX cluster. Below is the illustration: Primary Server IP Address : n.n.n.202 Secondary Server IP Address : n.n.n.212 Package IP Address : n.n.n.211 ... (1 Reply)
Discussion started by: cchilenga
1 Replies

10. UNIX for Dummies Questions & Answers

Connecting an external SCSI drive to a unix computer

I am interested in booting up my unix computer by connecting an external SCSI drive( intergraph) to it. The unix box is also an intergraph computer. Whenever I boot it, it gives me an error that says Drive not ready, Insert Boot Diskette in A. What am I doing wrong? Any input would be... (3 Replies)
Discussion started by: teruotor
3 Replies
Login or Register to Ask a Question