Need help interpreting a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help interpreting a function
# 1  
Old 01-12-2010
Need help interpreting a function

Hi, i was reading through a sample coding and came across this function, can anyone pls help to interpret the code for me. Thank alot

Code:
find_lines()
{
    res=-1
    if [ ! -z "$1" ]; then
        grep -i "$@" $FILENAME
        res=$?
    fi
    return $res
}


Last edited by Cheranime; 01-12-2010 at 11:36 AM.. Reason: use code tags and indention please, ty
# 2  
Old 01-12-2010
Code:
    res=-1

Assigning a value to a variable; -1 as default return code for this function if there is no parameter given when calling the function.

Code:
    if [ ! -z "$1" ]; then

...if the 1st positional parameter of the function find_lines() is not empty then...

Code:
        grep -i "$@" $FILENAME

... grep the file(name) contained in $FILENAME by ignoring case (-i) with all parameters given ($@) to the function.

Code:
        res=$?

The return code of the grep stored to res.

Code:
    fi
    return $res

Ending the if/then/fi and returning the value of res as return code of the function.
# 3  
Old 01-12-2010
Quote:
Originally Posted by Cheranime
Code:
        grep -i "$@" $FILENAME

There's a problem here. Suppose you call the function like this:
find_lines some words
The arguments of grep will be: "some" "words" "$FILENAME", but grep will interpret "words" a file, not a pattern to search for.
If you change it to "$*", then "some words" would be passed as one argument.


Also, you may want to add switch -q to grep to avoid printing the grep output.


I would rewrite the whole function like this:
Code:
function find_words()
{
    test -z "$1"  &&  return -1  ||  grep -q -i "$*" "$FILENAME"
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New to Shell Scripting: Need help interpreting example function

In this example function below, I cannot figure out what certain parts mean. if ! echo $PATHwhat is "if !"? (^|:)$1($|:) What is ^|: and$|:? pathmunge () { if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then if ; then PATH=$PATH:$1 else... (3 Replies)
Discussion started by: MemberName
3 Replies

2. UNIX for Dummies Questions & Answers

Help interpreting this freemem/freeswap graph

Hi, I am sure some gurus will recognize what this graph is. This is provided by our SA but I can't understand his explanation. I am not sure if this is from kSar or Cacti. The link that I was given to is to a kSar directory so I am assuming this output is from kSar. Hopefully, I can get a... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

Interpreting multiple values from a variable

Hi, I am writing a shell script which will check the status of a resource in a cluster and then display nicely to a user running the script at command line. Basically the script runs a status command and then pulls certain keywords from the return and then should display a concise status. ... (5 Replies)
Discussion started by: chris01010
5 Replies

4. Solaris

Interpreting xntpdc output.

Hi. I wonder what the equal sign in front of the answer means. I have read man pages and googled but found no answer. xntpdc -p =15.5.64.3 15.5.2.51 3 512 377 0.02060 0.057426 0.04965Thanks. Jan (1 Reply)
Discussion started by: vettec3
1 Replies

5. UNIX for Dummies Questions & Answers

interpreting netstat output

hi all, when I run- wcars1j5#netstat -an | grep 8090 127.0.0.1.8090 *.* 0 0 49152 0 LISTEN wcars1j5# 1. does this mean that no one is connected to this port? Regards, akash (1 Reply)
Discussion started by: akash_mahakode
1 Replies

6. Solaris

solaris way if interpreting devices?

Hi all, I wanted to know the solaris way of interpreting devices? I mean i understand all those c0t0....stuff but when i start mounting devices , most of the times i get either a I/O error or it says that the directory does not exist. eg: I have a external usb hub to which i have connected... (1 Reply)
Discussion started by: wrapster
1 Replies

7. UNIX for Advanced & Expert Users

vmstats interpreting

We are having performance issues on an alpha4100 server. I can't paste a snapshot of my vmstat in here, but... We have 4gb of memory. The actual memory stays consistant around 306k. Free is dropping into the 120 area. Wire is around 206k consistantly. consistantly. My manual says that unix... (3 Replies)
Discussion started by: MizzGail
3 Replies

8. UNIX for Dummies Questions & Answers

Shellscript Interpreting

I am trying to interpret the following shellscript and am having a very difficult time. Could one of you Unix gurus pleasssseeee help me out? You just won't know how much of a life saver you would be for me. PN=`basename "$0"` # Program name VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2` ... (3 Replies)
Discussion started by: Ann
3 Replies

9. UNIX for Dummies Questions & Answers

Interpreting netstat -s

Are there any references I can look up for to interprete "netstat -s", especially those on TCP statistics. (6 Replies)
Discussion started by: deaniyoer
6 Replies
Login or Register to Ask a Question