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 |
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 ...`? |
Actually I would be curious how this function works at all!!
You have said: Quote:
$1 = a.txt $2 = RBS15_SITE1 Therefore, based on your script: Code:
find_client(){ Code:
res=`exec awk '/a.txt/ {print $1}' a.txt |cat` I would also agree with irmin's last 2 questions as well??? |
All times are GMT -5. The time now is 05:05 PM. |