Find the answer to your Linux question:
Results 1 to 5 of 5
Bash script, simple string comparison, doesn't match on confirmed matches (added debug prints to check).. I know this is going to be a "duh" thing... does someone see something glaringly ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    22

    Bash string comparison function.. something probably stupid wrong.

    Bash script, simple string comparison, doesn't match on confirmed matches (added debug prints to check).. I know this is going to be a "duh" thing... does someone see something glaringly wrong?

    Code:
    CheckName () {
    
        local pushIFS=$IFS #add pop it later
        IFS=$'\n'
    
        recordfound=-1
    
        for line in $(cat $OUTFILE)
        do
            recname=`echo $line | cut -f1`
            echo "$recname ..... $1"
            if [ "$recname" = "$1" ]
            then
                recordfound=0
                echo "found it"
                return
            elif [ "$recname" != "$1" ]
            then
                echo "not eq"
                continue
            fi
        done
    
        return
    }
    
    read name
    CheckName "$name"

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I'm not really a basher but this sticks out

    Code:
    IFS=$'\n'
    Why the $ in front of '\n'
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    22
    Quote Originally Posted by gerard4143 View Post
    I'm not really a basher but this sticks out

    Code:
    IFS=$'\n'
    Why the $ in front of '\n'
    Without it, I was getting rather wonky results. I googled around and saw another example where someone used that, so presumably that gets the ASCII code for the newline char?

    Interestingly enough, I just tried the exact script I've posted on a Fedora box (without the surrounding code) and it works fine... so it's either the surrounding code or OpenSATAN.. I mean SUSE. *shudder* (I'm a Fedora/Ubuntu guy..) I don't have the rest of the script with me, but I don't know what code before it could be causing the problem really given it's just reading a string and passing it to a function...


    EDIT: what I mean by the wonky results:

    Here's just an example file it's reading from:
    Code:
    chris   something       3       1       4       1 
    bob     latida  5       9       2       6
    foo     bar
    Here's the output (working, on the Fedora box) if I use $'\n':
    Code:
    $ ./strcmp out
    bob
    chris ..... bob
    not eq
    bob ..... bob
    found it
    Here's the output if I use just '\n':
    Code:
    $ ./strcmp out
    bob
    chris ..... bob
    not eq
    g
    bob
    foo ..... bob
    not eq
    I think without the $ it doesn't do the value-of return.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I tried this on Arch Linux and it work...well once I corrected the data file to have tabs delimiters...Damn copy and paste from web pages..

    As you can see it pretty much what you had except I hard coded "bob" as the choice..

    Code:
    #! /bin/sh
    
    echo -n "enter filename->"
    read myfile
    
    if [ -f "$myfile" ]
    then
    	IFS=$'\n'
    	for line in $(cat $myfile)
    	do
    		recname=`echo $line | cut -f1`
    		if [ "$recname" = "bob" ]
    		then
    			echo	$line
    		fi
    	done
    fi
    
    exit 0
    Even tried it with a function like below and it worked

    Code:
    #! /bin/sh
    
    echo -n "enter filename->"
    read myfile
    
    echo -n "enter name->"
    read myname
    
    getit()
    {
    	for line in $(cat $myfile)
    	do
    		recname=`echo $line | cut -f1`
    		if [ "$recname" = $1 ]
    		then
    			echo	$line
    		fi
    	done
    }
    
    IFS=$'\n'
    
    if [ -f "$myfile" ]
    then
    	getit $myname
    fi
    
    exit 0
    Make mine Arch Linux

  5. #5
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    First off, the IFS variable is used to control parsing on input and arguments. It defaults to basic white space characters (space, tab, and newline). You have to include the $ in setting it as you have because otherwise it would set IFS to the ' character (single quote). But since you wanted the newline, you have to enclose the symbol for newline in single quotes and then escape the single quote when setting.

    But I think the way you are going about reading the list might be a bit convoluted, unless you really want to run "cut" on each pass. The whole for loop is unneeded I think. Consider this version:

    Code:
    #!/bin/bash
    
    myfile="$1"
    
    CheckName() {
    
        while read name thing eolstuff
        do
        if [ "$name" = "$1" ]; then
            echo "found it"
            return
        else
            echo "not found yet"
        fi
        done < "$myfile"
    
        return
    }
    
    echo -n "enter name to find: "
    read name
    CheckName "$name"
    I ran this on BASH (using your data) and got the following:

    Code:
    $ ./strcmp data
    enter name to find: bob
    not found yet
    found it
    
    $ ./strcmp data
    enter name to find: chris
    found it
    
    $ ./strcmp data
    enter name to find: foo
    not found yet
    not found yet
    found it
    
    $ ./strcmp data
    enter name to find: dummy
    not found yet
    not found yet
    not found yet
    The use of the "here" document in the read loop gets your one line at a time. The read of different variable names implicitly cuts the input line around white space giving you the pieces of the line without calling cut.

    Now if you need to act on the remaining fields of the record you found, you can simple take the "eolstuff" variable and then chomp around on it to break it out into its various pieces.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...