LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: How to grep stdout? (https://www.linuxquestions.org/questions/programming-9/bash-how-to-grep-stdout-812262/)

ryan858 06-05-2010 01:15 AM

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!

yooy 06-05-2010 01:33 AM

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"

grail 06-05-2010 02:18 AM

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.

colucix 06-05-2010 04:57 AM

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)}'

grail 06-05-2010 10:17 AM

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


All times are GMT -5. The time now is 12:32 AM.