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 ...
- 03-24-2010 #1Just 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"
- 03-24-2010 #2
I'm not really a basher but this sticks out
Why the $ in front of '\n'Code:IFS=$'\n'
Make mine Arch Linux
- 03-24-2010 #3Just Joined!
- Join Date
- Oct 2009
- Posts
- 22
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:
Here's the output (working, on the Fedora box) if I use $'\n':Code:chris something 3 1 4 1 bob latida 5 9 2 6 foo bar
Here's the output if I use just '\n':Code:$ ./strcmp out bob chris ..... bob not eq bob ..... bob found it
I think without the $ it doesn't do the value-of return.Code:$ ./strcmp out bob chris ..... bob not eq g bob foo ..... bob not eq
- 03-24-2010 #4
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..
Even tried it with a function like below and it workedCode:#! /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
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 0Make mine Arch Linux
- 03-25-2010 #5
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:
I ran this on BASH (using your data) and got the following: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"
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.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
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.


Reply With Quote
