LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2010, 12:51 PM   #1
Damon_Qu
LQ Newbie
 
Registered: Jun 2010
Posts: 2

Rep: Reputation: 0
bash


Hi All,
I have a question about shell script
I have a file named a.txt
#a.txt
RBS155_SITE1
IP=10.42.33.3
OptBootF="/WRAN/smrsslave1/AIF/Site1RBS155/summaryFile.xml"

RBS15_SITE1
IP=10.42.33.2
OptBootF="summaryFile.xml"


eRBS15_SITE1
IP=192.168.1.2
OptBootF="summaryFile.xml"
NTPservs=10.42.52.99
UTCoffst=23005253

Now I want to write a function who has two parameters

#para1: target file for example a.txt
#para2: target for example RBS15_SITE1
#return: 1: find target in the file 0: doesn't find the target in the #file
find_client(){
res=`exec awk '/'"$1"'/ {print $1 }' a.txt |cat`
if [ $res = $2 ]; then
return 1
else
return 0
fi
}

the problem is when I invoke the function as follows
find_client a.txt RBS15_SITE1

I find the value of res is
RBS15_SITE1 eRBS15_SITE1

the expected value of the res is RBS15_SITE1

How can I modify the function?


Thanks and BRs
Damon
 
Old 06-21-2010, 04:02 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
Welcome to LQ,

the output is as can be expected from your code:
As awk is called, it will print every first field in each record matching your regexp /$1/. Therefore two values are in res after this call. If you want only to extract the first result use "|head -1" instead of "|cat". If you want also to check for beginning and ending of lines in your regexp use ^ for the beginning and $ to match the end of the line.

Some additional questions:
1. What is the purpose of "|cat" at the end of the pipe command?
2. Why do you use `exec ...`?
 
Old 06-21-2010, 08:24 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Actually I would be curious how this function works at all!!

You have said:
Quote:
the problem is when I invoke the function as follows
find_client a.txt RBS15_SITE1
This would make:

$1 = a.txt
$2 = RBS15_SITE1

Therefore, based on your script:
Code:
find_client(){
    res=`exec awk '/'"$1"'/ {print $1 }' a.txt |cat`

    if [ $res = $2 ]; then 
        return 1
    else
        return 0
    fi
}
This would mean that your awk is actually saying (after variable expansion):
Code:
res=`exec awk '/a.txt/ {print $1}' a.txt |cat`
Based on this I would say that res has the following string: #a.txt

I would also agree with irmin's last 2 questions as well???
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 12:42 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 02:14 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM


All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration