Topics

Package Installation Scripts (Was: Re: [RaspberryPi-4-HamRadio] Brand New to Raspberry Pi)


Larry Dighera
 

On Mon, 22 Jun 2020 11:12:56 -0500, "John Nicholas"
<stnick@...> wrote:

So when I decide to add an app to my collection. Do I add it via the etcher to the download, or onto the SD Card, or download after everything is up and running
Hello John,

Raspbian, a derivative of Debian, uses the 'apt' command, or the
curses-based menu-driven 'aptitude' command-line package manager to
add software packages to your system. You'll also find a GUI software
manager in the applications menu-tree at the upper left corner of your
desktop.

You appear to be comfortable with the command-line interface, as am I,
so below (and attached) are a few scripts I've written for my own use
in adding packages. These are a bit crude, contain inconsistencies,
and surely could be cleaned up and re-written in a more elegant
manner, but I find them very useful none the less.

The first is 'apt.sh' which I run to see if there are any updates
available, before actually downloading a package:

-----------------------------------------------------------------------
# apt.sh Initialize package handler
# LGD: Mon Jun 5 11:58:39 PDT 2017

#set -xv # DEBUG
LOGFIL="$HOME/logs/$0.log"
[[ -d ${HOME}/logs ]] || echo -e "\n$0\: Log directory $LOGFIL does
not exist$(exit 2)" >&2

# Check for internet connection
STAT(){
ping -c1 -q 8.8.8.8 2>/dev/null 1>/dev/null ;STATUS=`echo $?`
return $?
}

# Check for stale lock file
LOCKED(){
LOCFIL=/var/lib/dpkg/lock
if [[ -f $LOCFIL ]] ;then # Does stale lock file
exist?
ls -l $LOCFIL # Display lock file
parameters
read -n 1 CHOICE?"`echo -e \"\nRemove Lock File
[Y/n/q]?\c\"`";echo # Prompt for user action
case $CHOICE in
q|Q*) exit ;; # Quit this script
*) echo; rm $LOCFIL;return "$?" ;; # Remove lock file and
return to caller with command status
esac
fi
return 0
}

LOCKED # Check for stale lock
file
until [[ $STATUS -eq 0 ]] ;do # Wait for network
connection
echo -e "Waiting for network connection."
sleep 3
STAT
done
setterm -foreground green # Set text color to
green
echo -e "\n\tNetwork connection successful\n";
#setterm -foreground white # Set text color to
white
setterm --default # Set terminal to
default values
aplay -q /usr/lib/libreoffice/share/gallery/sounds/apert.wav #
Announce successful network connection

echo;date +%A", "%B" "%d", "%Y" "%R
read -n 1 CHOICE?"`echo -e \"\nUpdate apt package system before
continuing [Y/n]? \"`";echo
case $CHOICE in
[nN]) exit ;; # Don't update
*) echo -e "\n##### `date` ####" >> ${LOGFIL};apt-get update 2>&1 |
tee -a ${LOGFIL} && apt-get --show-progress upgrade 2>&1 | tee -a
${LOGFIL} ;; # Update
esac

read -n 1 CHOICE?"`echo -e \"\nUpgrade Debian system before continuing
[Y/n]? \"`";echo
case $CHOICE in
[nN]) exit ;; # Don't upgrade
*) date >> ${LOGFIL};apt-get --show-progress dist-upgrade 2>&1 | tee
-a ${LOGFIL} ;; # Ugrade
esac
-----------------------------------------------------------------------

Between the effects of word-wrap and Windows Carriage-return,
Line-feed convention to end lines, the above will require some editing
to use. The attachments won't have those issues.

The next script, 'show.sh' is used to query the package database to
find if it contains a package supplied as an argument after the
command itself. I often find it useful to append a '\*' after the
package name to display package name variants.

-----------------------------------------------------------------------
#! /bin/ksh
# show.sh Display package information
#
# LGD: Sat 25 Jun 07:56:55 PDT 2016
# LGD: Mon 13 Feb 16:23:15 PST 2017 Added smart-pager support
#

if [[ $(tty |grep pts) ]] ;then
export LESS="-P\ ?f%f .?n?m(file %i of %m) ..?ltlines %lt-%lb?L/%L.
: byte %bB?s/%s. .?e(END) ?x- Next\: %x.:?pB%pB\%..%t (pts`tty|cut -d/
-f4`) "
else
export LESS="-P\ ?f%f .?n?m(file %i of %m) ..?ltlines %lt-%lb?L/%L.
: byte %bB?s/%s. .?e(END) ?x- Next\: %x.:?pB%pB\%..%t (`tty|cut -d/
-f3`) "
fi

# USAGE: Multi-package search not yet implemented
[[ $# -lt 1 ]] && echo -e "\n\tUsage: $0 [package name] \<package
name\> \<...\>\n" >&2 && exit 1

LOGFIL="$HOME/logs/$0.log"
[[ -d ${HOME}/logs ]] || echo -e "\n$0\: Log directory $LOGFIL does
not exist$(exit 2)" >&2

#set -x
SRCH=$(echo "$@"|sed 's/*/\\\\*/') # Escape * so shell doesn't show
filenames

FOUND=$(/usr/bin/apt-cache show $SRCH 2>&1) # BUG: Fails to assign
value to $FOUND
#FOUND=$(/usr/bin/apt-cache show "$SRCH")

if [[ `echo "$FOUND"|grep "purely virtual"` ]] ;then # BUG: This
fails to display output
echo "$FOUND" >&2; exit 1
elif [[ `echo "$FOUND"|grep "No packages found"` ]] ;then
echo "$FOUND" >&2; exit 1
else # Only pipe output through less when necessary
[[ `echo "$FOUND"|wc -l` -gt 30 ]] && echo "$FOUND"| less -p
'Package:'
[[ `echo "$FOUND"|wc -l` -le 30 ]] && echo "$FOUND"; exit
fi
-----------------------------------------------------------------------

You'll notice that I'm using the Korn Shell (ksh) command-line
interpreter. If you want to use it, you'll have to install it with:
'apt-get install ksh' first. Otherwise, I'll leave the editing to
support bash or sh as an exercise. :-) I like 'ksh' because, it
supports 'vi' command-line editing and floating-point math, and it was
what I learned back in the '80s.

And finally, here is 'get.sh' to actually install the package:

-----------------------------------------------------------------------
#! /usr/bin/ksh
#
# get.sh Install packages
#
# A front-end for 'apt-get' that provides package installation
information before installing packages and logs actions
#
# LGD: Sat 25 Jun 07:56:55 PDT 2016
# LGD: Tue 05 May 2020 10:43:28 AM PDT: Bug fixes and enhancements
#


# Check for positional parameters
[[ $# -lt 1 ]] && echo -e "\n\tUsage: $0 [package names]\n" && exit 1

# Set variable values and verify log file structure
PROG=$(basename $0)
ARGS="$@"
LOGDIR="$HOME/logs/"
LOGFIL="$HOME/logs/$(basename $0).log"
# Check for existence of log directory and writable log file
if [[ -d ${LOGDIR} ]] ;then
:
else echo -e "\n${0}: Log directory $LOGDIR does not exist." >&2
exit 2
fi

if [[ -w ${LOGFIL} ]] ;then
:
else echo -e "\n${0}: Log file ${LOGFIL} does not exist or is not
writeable." >&2
exit 3
fi

echo -e "\n${PROG}: Working. Please standby ..." >&2 # Display
assurance message

# Enter date into logfile
DATSTAMP() {
echo -e "\nOOOO- $ARGS -OOOO \c" >>$LOGFIL # Blank line
separator in log file
$(/bin/date '+%_A, %B %-d, %Y %H%M %Z'\)>>$LOGFIL)
return 0
}


# Test package installation before continuing
apt-get -s install $@ 2>&1 >/dev/null; EXSTAT=$? # Test for
success: 100=Unable to locate package; 0=Already newest version

# Get the package
case $EXSTAT in # 1=fail; 100=Unable to locate package(S)
0) echo -e "\nInstall $@ [y/N]? \c"
read REPLY
[[ ${REPLY} == [yY] ]] && DATSTAMP
# apt-get -s install $@ 2>&1 |tee -a $LOGFIL ;; #
DEBUG Install package(s)
apt-get install $@ 2>&1 |tee -a $LOGFIL ;; # DEBUG
Install package(s)
"100") DATSTAMP;echo -e "\nUnable to locate package(s) $@"|tee -a
$LOGFIL ; exit 100 ;; # Log event
*) echo "EXTSTAT = $EXTSTAT";echo -e "\n${0}: Package install
failure (${?}).";exit 5 ;; # Report failure with exit
code
esac

exit
-----------------------------------------------------------------------

You'll notice that these scripts write log files of the output of
their 'apt' commands in a 'logs' subdirectory under your $HOME
directory, so you'll need to 'mkdir $HOME/logs' before using the
scripts, if you want to keep logs. This can be useful when the output
scrolls too fast to read, and as a record of what you've done.

Finally, I put the name of this file '.fun' in my '$ENV' file so that
it adds a few useful functions to my environment. The 'd' function
can be useful for navigating convoluted directory structures.

-----------------------------------------------------------------------
cls(){
tput clear
cd
tty
}

u(){
cd ..
}

d() {
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
echo "$FUNCNAME: ksh only";return 1
fi

FIGNORE= # ksh93 equivalent to bash shopt -s dotglob

if [ ${#} -gt 0 ] ;then # Only one
command line argument accepted
cd -- "$1" && return 0
fi

if [ `ls -AF1|grep /|wc -l` -eq 1 ] ;then # cd if only
one subdirectory
cd -- `ls -AF1|grep /` && return 0
fi

destdir=$(
while :; do
subdirs=( ~(N)*/ ) # ksh93 equivalent to subdirs=( */ ) with
shopt -s nullglob
(( ${#subdirs[@]} > 2 )) || break # . and .. are two entries

echo -e "\n\t\tSubdirectories below ${PWD}: \n" >&2

for idx in "${!subdirs[@]}"; do
printf '%d) %q\n' "$idx" "${subdirs[$idx]%/}" >&2
done

printf '\nSelect a subdirectory: ' >&2
read -r
if [[ $REPLY ]]; then
cd -- "${subdirs[$REPLY]}" || break # Continue to
loop through subdirectories after cding
else
break
fi
done
printf '%s\n' "$PWD"
)
[[ $destdir ]] && cd -- "$destdir" && return 0
}

f(){
echo -e "You have these functions in your environment:\n"
typeset +f
echo
}

alias rm="rm -i"
alias cp="cp -i"
alias mv="mv -i"

alias ls="ls --color"
alias date="/bin/date '+%_A, %B %-d, %Y %H%M %Z'"
-----------------------------------------------------------------------

Of course, you'll have to enable execute permissions on these script
files before you will be able to run them:

chmod 755 apt.sh show.sh get.sh

Have fun, and if you improve my scripts, please e-mail a copy to me:
@LDighera

Best regards,
Larry
WB6BBB


PS: I was chatting with a fellow on 40-meters the other day, and
happened to mention that this pandemic means "Bye Bye Boomer," and
cleaver fellow, he said that made a good mnemonic for my call. :-)


John Nicholas
 

Larry,

Thanks for all the info and the scripts.  I should handle them easily with some practice.  My Unix Command Line Skills have deteriorated over the last 15 years, with minimal usage.  

I may come back with questions as I get into it.  1st Priority is supervise my SIL install a 6 meter and 10 meter dipole, perhaps one to night and one tomorrow night. I was installing them along with a 20 and 40 meter on the roof.  Then I fell down some steps on the 13th and broke some ribs. I got my tech and general tickets on March 14.  The HF stuff is set up and awaiting a tuned antenna.  I’d like to make a few contacts with Field Day, then work on the Pi stuff.

de KE0ZUW  John Nicholas  
Amateur Radio General Class Operator
Member ARRL, Wichita ARC, & Air Capitol ARC 
Grid Square EM17im

On Jun 24, 2020, at 11:32 AM, Larry Dighera <LDighera@...> wrote:

On Mon, 22 Jun 2020 11:12:56 -0500, "John Nicholas"
<stnick@...> wrote:

So when I decide to add an app to my collection.  Do I add it via the etcher to the download, or onto the SD Card, or download after everything is up and running


Ray Wells
 

On 25/6/20 2:32 am, Larry Dighera wrote:
On Mon, 22 Jun 2020 11:12:56 -0500, "John Nicholas"
<stnick@...> wrote:

So when I decide to add an app to my collection. Do I add it via the etcher to the download, or onto the SD Card, or download after everything is up and running
Hello John,

<snip>

The first is 'apt.sh' which I run to see if there are any updates
available, before actually downloading a package:

-----------------------------------------------------------------------
<more_snip>
-----------------------------------------------------------------------

Between the effects of word-wrap and Windows Carriage-return,
Line-feed convention to end lines, the above will require some editing
to use. The attachments won't have those issues.
The tool dos2unix is handy for massaging a Windows generated text file into Unix (Linux) format, it strips the CR that can cause a script to fail.

sudo apt-get install dos2unix

Ray vk2tv


Larry Dighera
 

On Wed, 24 Jun 2020 12:43:29 -0500, "John Nicholas"
<stnick@...> wrote:

Larry,

Thanks for all the info and the scripts.
You're welcome.


I should handle them easily with some practice. My Unix Command Line Skills have deteriorated over the last 15 years, with minimal usage.
Me too. And, Unix/Linux evolves over time, so it's difficult to stay
up to date if you're not continually using it.


I may come back with questions as I get into it.
I'll be looking forward to them.


1st Priority is supervise my SIL install a 6 meter and 10 meter dipole, perhaps one to night and one tomorrow night. I was installing them along with a 20 and 40 meter on the roof.
Once you've experienced the ~6dB gain of a hexbeam, you're station
will be forever changed. You can build one from plans here:
http://www.hex-beam.com/ . And, although the minimum height for
optimal performance in 24', they work remarkable well at much lower
heights.


Then I fell down some steps on the 13th and broke some ribs.
I'm sorry to hear that.

I got my General license in 2012, and promptly erected a hexbeam and
fan-dipole all by my self. (See: http://kj6yvt.com/hex-beam.htm ) It
was difficult (and frightening) at my age. Now eight years older, I'm
unwilling to risk the roof, so I am unable to make repairs.


I got my tech and general tickets on March 14.
Congratulations. I passed both tests at one sitting too.


The HF stuff is set up and awaiting a tuned antenna. I’d like to make a few contacts with Field Day, then work on the Pi stuff.

de KE0ZUW John Nicholas
Amateur Radio General Class Operator
Member ARRL, Wichita ARC, & Air Capitol ARC
Grid Square EM17im