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!