patternbashMinor
MacPorts - Quick Install Script
Viewed 0 times
scriptinstallmacportsquick
Problem
I wrote this script to automatically install the relevant version of MacPorts.
It's supposed to be used in a fashion similar to the official installer for HomeBrew.
FYI: The real reason that I decided to write this script is; I'm not all that confident in every aspect of my bash scripting abilities and I needed a project to practice on.
While I have a decent working knowledge of bash from an REPL standpoint, I haven't been scripting very long; so keep that in mind.
macports-installer.sh
Also, don't forget to mention if there's anything good about the script. If there's anything you do like about it; or if it does something well, let me know..
It's supposed to be used in a fashion similar to the official installer for HomeBrew.
FYI: The real reason that I decided to write this script is; I'm not all that confident in every aspect of my bash scripting abilities and I needed a project to practice on.
While I have a decent working knowledge of bash from an REPL standpoint, I haven't been scripting very long; so keep that in mind.
macports-installer.sh
#!/bin/bash
#
urisvn=https://svn.macports.org/repository/macports/trunk/www/includes/common.inc
uridist=https://distfiles.macports.org/MacPorts
#
mpvers=$(curl -s $urisvn | grep -m 1 -e \'*.*.*\' | cut -d \' -f 2)
osmajor=$(uname -r | cut -d . -f 1)
#
case $osmajor in
8)
osvers=10.4
osname=Tiger
;;
9)
osvers=10.5
osname=Leopard
;;
10)
osvers=10.6
osname=SnowLeopard
;;
11)
osvers=10.7
osname=Lion
;;
12)
osvers=10.8
osname=MountainLion
;;
13)
osvers=10.9
osname=Mavericks
;;
14)
osvers=10.10
osname=Yosemite
;;
15)
osvers=10.11
osname=ElCapitan
;;
*)
echo Warning: unknown OS version
;;
esac
#
mppkg=MacPorts-${mpvers}-${osvers}-${osname}
mpchk=MacPorts-${mpvers}
echo $mppkg.pkg
echo $mpchk.chk.txt
curl $uridist/$mppkg.pkg -O
curl $uridist/$mpchk.chk.txt -O
sha256=$(shasum -a 256 ./$mppkg.pkg)
#
if grep $sha256 $mpchk.chk.txt;
then sudo installer -verboseR -allowUntrusted -pkg ./$mppkg.pkg -target /;
fi
#
rm ./$mppkg.pkg
rm ./$mpchk.chk.txt
#
if ! grep -F 'export PATH="/opt/local/bin:/opt/local/sbin:$PATH"' .bash_profile;
then echo 'export PATH="/opt/local/bin:/opt/local/sbin:$PATH"' >> .bash_profile;
fi
#Also, don't forget to mention if there's anything good about the script. If there's anything you do like about it; or if it does something well, let me know..
Solution
You've got 0 validation in there, so the slightest hiccup will halt your script halfway. That's fine for you, but for a user, there will be a script that runs, crashes along the way, and then leaves all sorts of junk lying about.
You also have a "warning" for an unknown OS version, but then the os version seems to be required - without it,
Lastly, the two paired curl calls
Could be ran asynchronously (see this serverfault question https://serverfault.com/questions/456490/execute-curl-requests-in-parallel-in-bash) with
You also have a "warning" for an unknown OS version, but then the os version seems to be required - without it,
mppkg won't make sense and it's likely that the download will fail (unless there is a standard file, MacPorts-someversion-.pkg?). You'd be better off exiting the script then and there. Fail fast.Lastly, the two paired curl calls
curl $uridist/$mppkg.pkg -O
curl $uridist/$mpchk.chk.txt -OCould be ran asynchronously (see this serverfault question https://serverfault.com/questions/456490/execute-curl-requests-in-parallel-in-bash) with
& and wait. That way the script would be a bit faster.Code Snippets
curl $uridist/$mppkg.pkg -O
curl $uridist/$mpchk.chk.txt -OContext
StackExchange Code Review Q#128081, answer score: 4
Revisions (0)
No revisions yet.