You are not logged in.

#1 2014-12-23 15:48:12

mshan
Member
Registered: 2013-05-05
Posts: 105

bash script to mount/umount usb ?

this thread https://bbs.archlinux.org/viewtopic.php?id=185712 gave me an idea and teach me.
I created a script to mount/umount usb. Save it in user's home dir, do

chmod

and click on it as a toggle button to mount/umount usb.
It only work for one usb plugged in. Please suggest me to get it work for two or more.

#! /bin/bash

# mr = mounted result
# ur = umounted result
# er = empty result
# cm = check mountpoint
# cup = check usb is plugged in
# usb = usb device (Example... /dev/sdb1)


cm=$(mountpoint /home/username/tmp)
cup=$(lsblk | grep "sd[b-z]1")
usb=$(ls /dev/sd[b-z]1)
er=""
ur="/home/username/tmp is not a mountpoint"
mr="/home/username/tmp is a mountpoint"


if [ "$cm" == "$ur" ] && [ "$cup" != "$er" ]; then
echo "password" | sudo -S mount -o uid=username,gid=users $usb /home/username/tmp
elif [ "$cm" == "$mr" ] && [ "$cup" != "$er" ]; then
echo "password" | sudo -S umount /home/username/tmp
else
exit
fi

Amazing !

Offline

#2 2014-12-24 01:32:50

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,532

Re: bash script to mount/umount usb ?

At the first I thought you may use a udev rule to automate your task. (see /usr/lib/udev/rules.d/80-udisks.rules for guidelines)
As you may know udev can call any kind of executable, hence one of yours too.

Last edited by TheSaint (2014-12-24 02:14:16)


do it good first, it will be faster than do it twice the saint wink

Offline

#3 2014-12-24 01:44:08

ajbibb
Member
Registered: 2012-02-12
Posts: 142

Re: bash script to mount/umount usb ?

You might want to check out the bashmount package in the AUR.  If you are looking for a quick solution to mount removable media that may be it.

If you want to improve your script, for instance your multiple device question, I found the bashmount code to be very well laid out and well commented. I would suggest using that as a model.

Offline

#4 2014-12-24 02:00:25

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: bash script to mount/umount usb ?

There are a number of issues with your script; the reason you can't mount more than one device is that if "${usb[@]}" is populated by more than one drive, you need to iterate over the array to mount them.

Other issues that you should consider are:

  • your variable names are brief to the point of unintelligble: it is very hard to follow what is going on with "$mr", "$ur", "$cr" etc

  • don't parse the output of `ls`

  • don't put messages in variables; `printf` them

  • relatedly; check exit status, not strings: `$?`

  • if you are using bash; see What is the difference between [ and [[?

  • rather than `echo`ing your password (see 3), add the script to `sudoers`

Hope (some of) that helps.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2014-12-24 07:05:39

mshan
Member
Registered: 2013-05-05
Posts: 105

Re: bash script to mount/umount usb ?

thanks all for advices and links. they are helpful to learn bash.


Amazing !

Offline

Board footer

Powered by FluxBB