LinuxQuestions.org
Help answer threads with 0 replies.
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 01-14-2016, 01:36 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Rep: Reputation: 2
awk need to stop the whole script


Code:
bigFunction ()
{
  awk '{
        if ($thing) > 50)
            { exit 1 };
       }'
  
   
}

bigFunction
echo "XXXXX" ##### "should not print this if $thing > 50"
so basically I do want this script not to print XXXXX

Any ideas.
 
Old 01-14-2016, 02:03 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
What has this to do with awk? Bash is the controlling system here so it will be the one that needs to exit. It is hard to tell what you are trying to do as the awk command currently makes no sense.

So unless you provide more detail, the best I can say is you need to tell bash it is time to exit within the function.
 
Old 01-14-2016, 03:03 PM   #3
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
double post.

Last edited by PoleStar; 01-14-2016 at 03:06 PM. Reason: double post.
 
Old 01-14-2016, 03:03 PM   #4
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
Code:
bigFunction ()

{
  flag=0
  awk '{
        if ($thing) > 50)
            { flag=1 };
       }'
  
  if [ flag = 1 ]; then
     echo "exiting..."
     exit 1 
  fi
}

bigFunction
echo "XXXXX" ##### "should not print this if $thing > 50"
Is this better now ?

Last edited by PoleStar; 01-14-2016 at 03:05 PM.
 
Old 01-14-2016, 03:16 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Still won't work.

1 $thing doesn't exist within the context of awk.
2 flag is internal to awk, and nothing outputs that value...
4 flag in the bash if should be a syntax error (it isn't a command...)
 
Old 01-14-2016, 03:33 PM   #6
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
Code:
something=$( cat input | awk '{print $2}')

if [ $something < 50 ]; then
    exit 1 
fi
I think thats how it had to be done.
Thanks for ideas.
 
Old 01-14-2016, 03:39 PM   #7
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,159
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Quote:
Originally Posted by PoleStar View Post
Code:
something=$( cat input | awk '{print $2}')

if [ $something < 50 ]; then
    exit 1 
fi
I think thats how it had to be done.
Thanks for ideas.
How about
Code:
something=$(awk '{print $2}' input)
 
Old 01-14-2016, 03:46 PM   #8
PoleStar
Member
 
Registered: Jul 2010
Posts: 165

Original Poster
Rep: Reputation: 2
off course.
Actually out put would be form system command, I put cat to illustrate, simply.
 
Old 01-14-2016, 03:57 PM   #9
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,159
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Quote:
Originally Posted by PoleStar View Post
off course.
Actually out put would be form system command, I put cat to illustrate, simply.
Naturally!
 
Old 01-15-2016, 07:00 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well you seem to have your solution now, but in addition to jpollard's summary of issues, I would add one more for future knowledge:
Code:
  awk '{
        if ($thing) > 50)
            { flag=1 };
       }'
In addition to thing and flag not meaning anything between bash and awk, the above would simply wait forever (try it on the command line) as there is no input for awk to read.
The other choice is it may error thinking that the 'if' statement was supposed to be the input.

Should you wish to run awk and not require an input source, you would need to use the BEGIN variant utilised by awk. This would still not help overall (as per information supplied by jpollard),
but it at least would not error from awk's point of view.
 
Old 01-15-2016, 07:08 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
the solution can be to check the exit code of awk
either with set -e
Code:
set -e
bigfunction {
...
   awk ' { ... exit 1 }'
...
}

bigfunction
echo xXXxXxxXx
or $?
Code:
bigfunction {
...
   awk ' { ... exit 1 }'
   if [ $? -ne 0 ]; then
     echo exiting
     exit 1
   fi
...
}

bigfunction
echo xXXxXxxXx

(not tested)
 
Old 01-15-2016, 12:48 PM   #12
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 110

Rep: Reputation: 53
Missing Piece

Once you solve any issues of getting your input into awk - as detailed by the other replies - you perform any tests/processing in awk and then exit awk with a status value (exit 0 or exit 1 ...)

At this point, you're back in bash (or whatever shell you're using) and you have to solve the rest of the problem there.

Using set -e is a bit extreme and usually not necessary. Just handle the return status yourself.

The code below is one way of doing this.

Code:
awk ... ## run your awk program (or any other program which returns an exit status)
rc=$?   ## save the return status in a variable for reporting and debugging
if (( rc )) ## Anything other than 0 means something went wrong
then
  ## handle the error
else
  ## handle success
fi
 
  


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
stop script used to stop tomcat server is working intermittently BhaktiK Linux - Server 4 03-15-2013 10:38 AM
[awk script] Help me delete lines in a file using script ? sieukid Programming 5 03-20-2012 02:23 PM
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 10:06 PM
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 04:38 PM
AWK- processing data inside start/stop pairs but ignoring start/stop lines cliffoij Programming 2 10-15-2008 07:17 AM


All times are GMT -5. The time now is 10:25 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