[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Using sudo in a script.



On Wed, 03 Aug 2016, Ken Heard wrote:

> I would like to create a short script to mount and unmount SCXC cards.
>  For these exfat cards to do either in Jessie has to be done as root.
> 
> I have set up sudo so that my user can run any command as root.  the
> following command will mount the card without asking for my password.
> 
> sudo mount -U 6238-3434 /media/xca
> 
> (My file /etc/fstab contains the following line:
> 
> UUID-6238-3434  /media/xca      exfat   rw,users,noauto,noatime 0     0)

This should probably be

UUID=6238-3434 /media/xca exfat rw,users.noauto,noatime 0 0

and then in theory you should be able to go

mount /media/xca

as a user. If not, this sounds like a bug in mount.exfat.

> In order to automate the process I wrote the following script named mxca
> :
> 
> #!/bin/bash
> # Script to mount xca from any directory.
> CURPWD=$PWD
> cd /home/ken
> sudo mount -U 6238-3434 /media/xca
> cd $CURPWD

First off, the cd in the script won't change what the directory is once
the subshell started for the script exits.

Second, if you need to change directories temporarily in a shell script,
use a subshell:

#!/bin/bash
(cd /home/ken;
 sudo mount -U 6238-3434 /media/xca;
)

> Is there a way to make sudo do its thing in these scripts so that I
> can mount and unmount the SDXC card by running the script commands
> mxca and uxca by themselves?

It's possible that sudo doesn't have a tty, and can't ask for a
password. adding set -x; early on in the script may help you see what is
actually happening.

-- 
Don Armstrong                      https://www.donarmstrong.com

Those who begin coercive elimination of dissent soon find themselves
exterminating dissenters. Compulsory unification of opinion achieves
only the unanimity of the graveyard.
 -- Justice Roberts in 319 U.S. 624 (1943)


Reply to: