LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2010, 01:15 AM   #1
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Rep: Reputation: 17
Bash: How to grep stdout?


I'm making a bash script, basically it's a quick shortcut for using apt-get... and I need to search the stdout for a pattern and figure out what the value of a character is within that pattern...

Here is my script:

Code:
#!/bin/bash
#
# get - A Quick shortcut to apt-get for installing packages


# Print the usage.
usage () {
  cat << EOF
  Usage: `basename $0` [OPTION][PACKAGES...]
  A quick shortcut to apt-get

  Options:
    -u, --update [PACKAGES]     Update package database. If packages are specified after switch,
                                they will be installed after updating.
    -c, --check                 Check whether updates are availible, and verify that there are
                                no broken dependienceis, and then exit.
    -h, --help                  Print this message and exit.

  If you need more options, you should use apt-get directly.

EOF
}

# Check whether packages were specified after update switch
update_check_for_packages () {
  if [ $2 -n ] ; then
    apt-get update && apt-get install ${@##--update}
    else
    apt-get update
  fi
}

# Check if updates are available... Need to a way to read output of apt-get 
check_for_updates () {
  1>$output
  grep ... $output # Need to find how many not upgraded
  # Figure out if 'not upgraded' isn't 0
  if [ ... ] ; then
    update_status=1
    else
    update_status=0
  fi
}

# Check the arguments.
for option in "$@" ; do
  case "$option" in
  "" | -h | --help)
    usage ; exit 0 ;;
  -u | --update)
    update_check_for_packages ;;
  -c | --check)
    apt-get update
    apt-get check
    apt-get install
    if [ $update_status = 1 ] ; then
      read -p "
      $num_updates Updates are available... Upgrade now? [Y/n]: " usropt
      case $usropt in
        Y|y) apt-get upgrade ;;
        N|n) opt_out ;;
        "") apt-get upgrade ;;
        *) opt_out ;;
      esac
      else
        echo "No updates are available."
    fi
  -*)
    echo "Unrecognized option '$option'" 1>&2
    usage ; exit 1 ;;
    *) apt-get install $@ || exit 1 ;;
  esac
done
This is the bit I'm stuck on...

Code:
# Check if updates are available... Need to a way to read output of apt-get 
check_for_updates () {
  1>$output
  grep ... $output # Need to find how many not upgraded
  # Figure out if 'not upgraded' isn't 0
  if [ ... ] ; then
    update_status=1
    else
    update_status=0
  fi
}
the command "apt-get install" prints out "0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded." and I need to figure out how many 'not upgraded' are... and if it's not 0, then it prompts the user to chose whether to install them... I also need to put the number of avail. updates in the prompt, i.e "4 Updates are available... Upgrade now? [Y/n]: "

I could redirect the command to a variable instead of redirecting stdout to a var... but then I would need it to figure out what the names of the available packages are and tell the user... Actually I might need that anyway....

So basically I'm stuck here... Does anyone know a way to do this? Also, I'd be very grateful if anyone could offer some tips or advice to improving other things in this script.

Thank you!
 
Old 06-05-2010, 01:33 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 174Reputation: 174
you can get the number of upgrades using awk:

Quote:
a=$(xrandr | grep "*" | awk '{ print $1 }' )
this gets me my resolution


try to keep your script simple, as in xbuntu is aptitude being exchanged for "software center"
 
Old 06-05-2010, 02:18 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
Is grep really needed here:
Quote:
a=$(xrandr | grep "*" | awk '{ print $1 }' )
I am not sure what the output is from "xrandr" but awk has regex capabilities too.
 
Old 06-05-2010, 04:57 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As grail said! Example:
Code:
echo "0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded" | awk '/not upgraded/{gsub(/.* and | not upgraded/,""); print}'
or
Code:
echo "0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded" | awk '/not upgraded/{print gensub(/.* ([0-9]*) not upgraded/,"\\1",1)}'
 
Old 06-05-2010, 10:17 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
And if the format is always the same just pick the field that holds the data you want.
 
  


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
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
Bash : add cr to stdout romainp Linux - General 4 08-30-2007 11:05 AM
demultiplexing bash stdout stream pobbz Linux - Software 3 06-21-2007 09:14 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 AM.

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
Open Source Consulting | Domain Registration