I have a shell script to mount different remote systems. While it does work, the area I struggle with (not being a programmer) is the logic of the script: it feels quite stilted and there seems to be some unnecessary duplication.
I would appreciate any comments as to how I could improve it—both in terms of the logic, but also any other tips about style or approach.
#!/bin/sh
# mount boxes by SSHFS
usage () {
cat <<EOF
sshmnt -[c,s,u,h]
-c Box1
-s Box2
-u unmount
-h print this message
EOF
}
mnt(){
sshfs jason@"$host":/home/jason /media/"$dir" \
-C -p "$port" \
-o reconnect,IdentityFile=/home/jason/.ssh/id_rsa \
&& ls /media/"$dir"
}
umnt(){
fusermount -u /media/"$dir"
}
# unmounting
if [ "$1" = "-u" ]; then
box=$(mount | grep Box1)
if [ -n "$box" ]; then
dir=Box1
else
dir=Box2
fi
umnt && exit
fi
# check if on LAN
lan="$(ip addr | grep .102/)"
if [ -n "$lan" ]; then
case "$1" in
-c) int=100
;;
-s) int=200
;;
esac
host="192.168.1.$int"
else
host="XXX.XXX.XXX.XXX"
fi
# box specifics
case "$1" in
-c) dir=Box1 port="XXXX"
mnt
;;
-s) dir=Box2 port="XXXX"
mnt
;;
*) usage && exit
;;
esac
I have obscured the relevant ports and hostnames.