diff options
Diffstat (limited to 'scripts/.local/bin/dmnt')
| -rwxr-xr-x | scripts/.local/bin/dmnt | 103 |
1 files changed, 52 insertions, 51 deletions
diff --git a/scripts/.local/bin/dmnt b/scripts/.local/bin/dmnt index bb98902..7966d07 100755 --- a/scripts/.local/bin/dmnt +++ b/scripts/.local/bin/dmnt @@ -1,15 +1,15 @@ #!/bin/sh #shopt -s expand_aliases -DMENU="$(command -v cdmenu)" +dmenu="$(command -v cdmenu)" getmount() { \ [ -z "$chosen" ] && exit 1 # shellcheck disable=SC2086 - mp="$(find $1 2>/dev/null | "$DMENU" -l 16 -i -p "Type in mount point.")" || exit 1 + mp="$(find $1 2>/dev/null | "$dmenu" -l 16 -i -p "Type in mount point.")" || exit 1 [ "$mp" = "" ] && exit 1 if [ ! -d "$mp" ]; then - mkdiryn=$(printf "No\nYes" | "$DMENU" -i -p "$mp does not exist. Create it?") || exit 1 + mkdiryn=$(printf "No\nYes" | "$dmenu" -i -p "$mp does not exist. Create it?") || exit 1 [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp") fi } @@ -45,14 +45,14 @@ mountusb() { \ mountandroid() { \ getmount "/mnt -maxdepth 1 -type d" jmtpfs -device="$1" "$mp" - #echo "OK" | "$DMENU" -i -p "Tap Allow on your phone if it asks for permission and then press enter" || exit 1 + #echo "OK" | "$dmenu" -i -p "Tap Allow on your phone if it asks for permission and then press enter" || exit 1 #jmtpfs -device="$1" "$mp" notify-send "Android mounted to $mp" "" } unmountdev() { \ - result="$(sudo umount \"$1\" 2>&1)" + result="$(sudo umount "$1" 2>&1)" if [ -z "$result" ]; then notify-send "$1 unmounted" "" return @@ -61,10 +61,10 @@ unmountdev() { \ pids="$(echo "$processes" | awk '{print $2}')" #notify-send -u critical "Unmount device" "$result\nPreparing list of processes blocking $1..." notify-send "Unmount $1" "$result\nBlocking processes:\n$processes" - action=$(echo -e "nothing\nterminate\nkill" | "$DMENU" -i -p "What to do to processes blocking $1" -l 3) + action=$(printf 'nothing\nterminate\nkill' | "$dmenu" -i -p "What to do to processes blocking $1" -l 3) case $action in - terminate) echo "$pids" | while IFS='\n' read -r pid ; do kill $pid; done ;; - kill) echo "$pids" | while IFS='\n' read -r pid ; do kill -9 $pid; done ;; + terminate) echo "$pids" | while IFS="$(printf '\n')" read -r pid ; do kill "$pid"; done ;; + kill) echo "$pids" | while IFS="$(printf '\n')" read -r pid ; do kill -9 "$pid"; done ;; nothing) notify-send "Unmount of $1 aborted" "" return ;; @@ -74,16 +74,16 @@ unmountdev() { \ } selectusb() { \ - chosen=$(lsblk -lpo "vendor,model,name,label,size,mountpoint" | "$DMENU" -i -l 16 | xargs | cut -d' ' -f 1) + chosen=$(lsblk -lpo "vendor,model,name,label,size,mountpoint" | "$dmenu" -i -l 16 | xargs | cut -d' ' -f 1) # if mounted - if [ ! -z "$(grep $chosen "/etc/mtab")" ] ; then - choice=$(echo -e "No\nYes" | "$DMENU" -i -l 2 -p "Partition is mounted. Unmount?") + if grep -q "$chosen" "/etc/mtab" ; then + choice=$(printf 'No\nYes' | "$dmenu" -i -l 2 -p "Partition is mounted. Unmount?") case $choice in - Yes) unmountdev $chosen ;; + Yes) unmountdev "$chosen" ;; No) exit ;; esac else - mountusb $chosen + mountusb "$chosen" fi } @@ -91,15 +91,15 @@ selectusb() { \ mountsamba() { \ mounted=$(mount -t cifs) - if [[ -z "$mounted" ]]; then + if [ -z "$mounted" ]; then actions="mount\nunmount" - action=$(printf "$actions" | "$DMENU" -i -p "Samba action:") + action=$(printf '%s' "$actions" | "$dmenu" -i -p "Samba action:") - if [[ $action == "unmount" ]]; then - mount=$(printf "$mounted" | "$DMENU" -i -p "Share to unmount:") - printf "Selected: $mount\n" - mount_dir=$(printf "$mount" | cut -d' ' -f 3) - printf "$mount_dir\n" + if [ "$action" = "unmount" ]; then + mount=$(printf '%s' "$mounted" | "$dmenu" -i -p "Share to unmount:") + printf 'Selected: %s\n' "$mount" + mount_dir=$(printf '%s' "$mount" | cut -d' ' -f 3) + printf '%s\n' "$mount_dir" sudo -A umount "$mount_dir" exit 0 fi @@ -108,57 +108,58 @@ mountsamba() { \ # get IP of this device IP=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/') - printf "IP: $IP\n" + echo "IP: $IP" # prepare subnet choices: choose IP or run a scan if a wildcard specified - subnets="$IP\n$(echo $IP | cut -d. -f1-3).1\n$(echo $IP | cut -d. -f1-3).*\n$(echo $IP | cut -d. -f1-2).*.*" - printf "Subnets available:\n$subnets\n" - subnet=$(printf "$subnets" | "$DMENU" -i -l 16 -p "Input samba scan range") - printf "Subnet chosen: $subnet\n" + subnets="$IP\n$(echo "$IP" | cut -d. -f1-3).1\n$(echo "$IP" | cut -d. -f1-3).*\n$(echo "$IP" | cut -d. -f1-2).*.*" + printf 'Subnets available:\n%s\n' "$subnets" + subnet=$(printf '%s' "$subnets" | "$dmenu" -i -l 16 -p "Input samba scan range") + printf 'Subnet chosen: %s' "$subnet" - if [[ $subnet == *[*]* ]]; then + # if there's a * in subnet + if [ "${subnet#*\*}" != "$subnet" ]; then notify-send "Scanning network for samba shares..." "" - servers="$(nmap "$1" -p 445 --open -oG -)" - printf "$servers\n" + servers="$(nmap "$subnet" -p 445 --open -oG -)" + echo "$servers" IPs=$(echo "$servers" | grep Scan | cut -d' ' -f2-) - printf "Server IPs:\n$IPs\n" + printf 'Server IPs:\n%s\n' "$IPs" - IP=$(printf "$IPs" | "$DMENU" -i -l 16 -p "Choose samba server IP") - printf "Selected IP: $IP\n" + IP=$(printf '%s' "$IPs" | "$dmenu" -i -l 16 -p "Choose samba server IP") + echo "Selected IP: $IP" else IP="$subnet" fi - user=$(printf "$(whoami)\nanonymous" | "$DMENU" -p "Samba username:") - printf "$user\n" - if [[ $user != "anonymous" ]]; then + user=$(printf '%s\nanonymous' "$(whoami)" | "$dmenu" -p "Samba username:") + echo "$user" + if [ "$user" != "anonymous" ]; then password=$(pass-askpass "$user's password ") - shares="""$(smbclient -L $IP --user $user --password $password)""" + shares="""$(smbclient -L "$IP" --user "$user" --password "$password")""" else notify-send "Attempting anonymous login..." "" - shares="""$(smbclient -L $IP -N)""" + shares="""$(smbclient -L "$IP" -N)""" fi - printf "Raw shares:\n$shares\n" + printf 'Raw shares:\n%s\n' "$shares" #shares=$(printf "$shares" | sed -e 's/^[[:space:]]*//') #printf "Cleaned shares:\n$shares\n" - share=$(printf "$shares" | "$DMENU" -i -l 16 -p "Which samba share to mount?") - printf "$share\n" + share=$(printf '%s' "$shares" | "$dmenu" -i -l 16 -p "Which samba share to mount?") + echo "$share" # leave only the share name, assuming no whitespace in share name - chosen=$(printf $share | cut -d' ' -f 1) - printf "Chosen: $chosen\n" + chosen=$(printf '%s' "$share" | cut -d' ' -f 1) + echo "Chosen: $chosen" share="//$IP/$chosen" getmount "/mnt -maxdepth 1 -type d" - sudo -A mount -t cifs -o user="$user",password="$password",iocharset=utf8,uid=$(id -u),gid=$(id -g),file_mode=0644,dir_mode=0755 "$share" "$mp" - if [ $? -ne 0 ]; then - notify-send "Samba mount failed" "$1 to $mp as $user" + + if ! sudo -A mount -t cifs -o user="$user",password="$password",iocharset=utf8,uid="$(id -u)",gid="$(id -g)",file_mode=0644,dir_mode=0755 "$share" "$mp"; then + notify-send "Samba mount failed" "$subnet to $mp as $user" exit 1 fi - notify-send "Samba share succeeded" "$1 mounted to $mp as $user" + notify-send "Samba share succeeded" "$subnet mounted to $mp as $user" } selectandroid() { \ @@ -166,23 +167,23 @@ selectandroid() { \ mounted=$(grep "jmtpfs" "/etc/mtab") list="Mounted:\n" - if [ ! -z "$mounted" ]; then + if [ -n "$mounted" ]; then list="$list$mounted" fi list="$list\nUnmounted:\n" - if [ ! -z "$unmounted" ]; then + if [ -n "$unmounted" ]; then list="$list$unmounted" fi - chosen=$(echo "$list" "$mounted" | "$DMENU" -i -l 16) + chosen=$(echo "$list" "$mounted" | "$dmenu" -i -l 16) #echo "CHOSEN" $chosen case $chosen in - *jmtpfs*) unmountdev $(echo $chosen | cut -d' ' -f 2) ;; - *) mountandroid $(echo $chosen | awk '{print $1 $2}' | cut -d, -f1-2) ;; + *jmtpfs*) unmountdev "$(echo "$chosen" | cut -d' ' -f 2)" ;; + *) mountandroid "$(echo "$chosen" | awk '{print $1 $2}' | cut -d, -f1-2)" ;; esac } -choice="$(printf "drive\\nSamba\\nAndroid" | "$DMENU" -i -p "(Un-)mount a")" || exit 1 +choice="$(printf 'drive\nSamba\nAndroid' | "$dmenu" -i -p "(Un-)mount a")" || exit 1 case $choice in drive) selectusb ;; Samba) mountsamba ;; |
