shell script - unexpected result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script - unexpected result
# 1  
Old 08-21-2011
shell script - unexpected result

I hv a file --am executing a script which is giving me unexpected results


COntents of file:

f1
Code:
CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX:
CMT_AP1:/opt/sybase/syboc125:150:ASE12_5::Y:UX

f1.tmp
Code:
CMT_AP1_CONT:/opt/sybase/syboc125:150:ASE12_5::Y:UX:
CAT_AP1:/opt/sybase/syboc125:150:ASE12_5::Y:UX

Difference is at 2nd line "CMT" and "CAT"

Script#1
Code:
for i in `grep AP1 f1 |awk -F: '{print $1}'`
do
  DSQUERY=$i
  A15F=`grep AP1  f1| grep ${DSQUERY} | awk -F: '{print $3}'`
  echo "$A15F ----- $i"
  if [ "$A15F" = "150" ]; then
    echo $i
  else
   echo "Version 12.5",$i
  fi
done

ouput:
Code:
150 ----- CMT_AP1_CONT
CMT_AP1_CONT
150
150 ----- CMT_AP1
Version 12.5,CMT_AP1

Script#2:
Code:
for i in `grep AP1 f1 |awk -F: '{print $1}'`
do
  DSQUERY=$i
  A15F=`grep AP1  f1| grep ${DSQUERY} | awk -F: '{print $3}'`
  echo "$A15F ----- $i"
  if [ "$A15F" = "150" ]; then
    echo $i
  else
    echo "Version 12.5",$i
  fi
done

Output 2:
Code:
150 ----- CMT_AP1_CONT
CMT_AP1_CONT
150 ----- CAT_AP1
CAT_AP1

++++++expected behaviour is 2 why script #1 is not working in a desired way..Pls hlelp me..Thanks

Last edited by Scott; 08-21-2011 at 06:03 AM.. Reason: Please use code tags...
# 2  
Old 08-21-2011
This should work

Code:
for i  in  `grep AP1 f1 |awk -F: '{print $1}'`
do
DSQUERY=$i
export A15F
A15F=`grep AP1 f1| grep -w ${DSQUERY} | awk -F: '{print $3}'`
echo "$A15F ----- $i"
if [ "150" = "$A15F" ]; then
echo $i
else
echo "Version 12.5",$i
fi
done

This is because the second time the for loop runs it select both the lines "CMT_AP1" matches both line as a result A15F is 150 new line 150 which doesnt match 150
# 3  
Old 08-21-2011
useless use of grep | awk -- awk can handle searching for DSQUERY itself. Also useless use of backticks, the latter of which can actually be dangerous -- that splits on whitespace, not just lines, for one thing. It may truncate a list that ends up too long for a shell variable to hold too.

Code:
awk -F: '/'${DSQUERY}'/ { print $3 }' | while read LINE
do
...
done

Side-effect of the pipe means variables set inside the while loop may not be visible outside it. To avoid this you can send the output into a temp file, then read the temp file like done < /path/to/file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Unexpected result from awk

Hello, Giving those commands: cat > myfile 1 2 3 ^D cat myfile | awk '{ s=s+$1 ; print s}' The output is: 1 3 6 It seems like this command iterates each time on a different row so $1 is the first field of each row.. But what caused it to refer to each row ?. What I mean... (3 Replies)
Discussion started by: uniran
3 Replies

2. UNIX for Dummies Questions & Answers

Weird: unexpected result after piping a sort

Hello, And when you think you know the basics of something, UNIX in this case, something like what I will describe below comes along.... On a Linux system, a "typical" directory with some files. Say 20. I do: > ls | sort > mylisting Now when I: > vi mylisting There is mylisting... (13 Replies)
Discussion started by: stavros
13 Replies

3. Shell Programming and Scripting

`(' unexpected error when running a shell script in AIX

Hi, We are moving from linux to AIX servers, and as a result testing our scripts on the new platform. When I run one of our scripts, I get the following error message: ./branchDataUpdate.sh: syntax error at line 21 : `(' unexpected Following is an extract from the script: ...... ........ (1 Reply)
Discussion started by: dawgfather80
1 Replies

4. Shell Programming and Scripting

Capture unexpected exit in shell script

Hi, I have shell script that checks processes forever. But somehow it is killed and I want to know what causes it. while do check the processes if they are running, if not restart them done I want to capture the output when the script is terminated, how can I do that? /Andreas (2 Replies)
Discussion started by: mr_andrew
2 Replies

5. Homework & Coursework Questions

A function in shell script,how can i get the right result

there is a directory /data/users/osa/psidp/dmp/files/cimdir ,it have some subdirectories ,and also the subdirectoriy have it's subdirectoriis. I want to get all the leaf nodes of the directory path . but the result of the script is wrong ,how can i get the right result somebody who can help... (1 Reply)
Discussion started by: fw0037
1 Replies

6. Shell Programming and Scripting

shell script result to file

Since I'm not an expert in shell scripting, I have question on sending script result to file. I have script like this... if condition=0: then echo "service is not running" | mail -s "Server Status" uname@companyname fi sleep 10 if configtion=1: then echo "service is not running" | mail -s... (3 Replies)
Discussion started by: s_linux
3 Replies

7. Shell Programming and Scripting

get result from database into shell script

hi, I have a script that will logon to a database siebel and do the select query and then get the result in command prompt of unix.Below the script. #!/bin/ksh . $HOME/conf/systemProperties/EnvSetup.properties #set -x while read i do echo $i connect1=`sqlplus -silent... (1 Reply)
Discussion started by: ali560045
1 Replies

8. Shell Programming and Scripting

Unexpected sed result.

I am in the process of writing a script to change the grub password in the grub.conf file. I thought I had it figured out, but am running into an a problem I can't put my finger on. Command I am running when I find that the grub.conf file contains "password --md5". sed... (1 Reply)
Discussion started by: viRaven
1 Replies

9. Shell Programming and Scripting

unexpected pipeline result with find -exec

Hi All, I probably miss something fundamental here. I want to rename a bunch of files in subdirectories (that might contain white spaces) with names that are related. I thought following could do the job: find . -name *.sh -exec mv {} $(echo {} | sed -e 's/0/1/g') \; Now to be able to... (5 Replies)
Discussion started by: blued
5 Replies

10. Shell Programming and Scripting

Could any one tell me how to do get result in fraction in shell script

Could any one tell me how to do get result in fraction in shell script. I am using expr for division of 2 number but it is giving Quitent. I need it in full fraction value. please helpp. how to do it using "dc" or "bc" (5 Replies)
Discussion started by: Neerajjaiswal
5 Replies
Login or Register to Ask a Question