Ok, that's pretty ugly.
Long, repeated, nearly-unreadable commands like that should generally be configured as
functions, so that they don't clutter up the code. Or at least set up the various options in variables first.
And is
exec really needed here? If you're forking off the commands and want to detach them from the script, follow them up with the
disown builtin instead.
Code:
myNetwork1=`system_profiler | grep 'IPv4 Addresses'`
myNetwork=`system_profiler | grep 'IPv4 Addresses' | awk '{ print $3 }' | sed 's/[.]/ /g' | awk '{ print $3 }'`
$(..) is highly recommended over `..`
You're running the same command twice here. Have the second command filter the
$myNetwork1 variable directly instead. You can probably replace the
awk and
sed with some built-in
string manipulations instead, as well.
Code:
if [[ "$myNetwork1" == "IPv4 Addresses: 172.31*" ]]
then
case $myNetwork in
Again, you should use variables and string substitutions to extract only the parts you need to test first, instead of testing for long text strings. It generally pays off to pre-process your input, to help keep the code following it simple and clean and to keep the focus on what's really important.
Code:
8*)
exec lpadmin -p dal1-donatello -E -L "Next to IT Room" -v lpd://172.31.8.34 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-donatello &
exec lpadmin -p dal1-leonardo -E -L "HSE Room" -v lpd://172.31.8.38 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-leonardo &
exec lpadmin -p dal1-michelangelo -E -L "Next to Freud Conf Room" -v lpd://172.31.8.37 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C6501 -D dal1-michelangelo &
exec lpadmin -p dal1-raphael -E -L "In Mail Room" -v lpd://172.31.8.33 -P /Library/Printers/PPDs/Contents/Resources/RICOH\ Aficio\ MP\ C3501 -D dal1-raphael &
;;
As I mentioned before, moving the commands into functions, and/or storing data strings in variables, can really help. In
bash you can use an
array to safely store a string of command options together. Also,
quote long strings rather than backslash escaping them.
Code:
lp_settings(){
local pD_opt L_opt v_opt P_opt
case $1 in
don)
pD_opt=dal1-donatello
L_opt='Next to IT Room'
v_opt='lpd://172.31.8.34'
P_opt='/Library/Printers/PPDs/Contents/Resources/RICOH Aficio MP C3501'
;;
raph)
...<etc>
;;
esac
lpadmin_opts=( -p "$pD_opt" -D "$pD_opt" -E -L "$L_opt" -v "$v_opt" )
}
lp_settings don && lpadmin "${lpadmin_opts[@]}" &
disown