D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] shell scripts



On Sun, 15 Jul 2001, Paul Sutton wrote:

> I am starting to learn shell scripts and have a basic script which just
> mounts a cdrom drive,
>
> #!/bin/bash
> mount /dev/cdrom /mnt/cdrom
> echo cd mounted
>
> I need to add some more error checking but it works,

The $? variable contains the return code of the last command run. 0
usually being "ran without error" so:

mount /dev/cdrom /mnt/cdrom
if [ $? -eq 0 ]; then
	echo "CD mounted OK"
else
	echo "An error occoured"
fi

Would do some of that for you.

> I was thinking su <password may work,  but would like something more secure,
> like getting the details from /etc/passwd and passing this on,  save a user
> having to do it.

I think you'll find that trying to stick the password into stdin in a
script will have su complaining about "not a tty" or something.

As long as you're careful about what you put in your script, you'd be
better off making it suid root:

chown root:root mountscript.sh
chmod 4755 mountscript.sh

Also, for scripting you're better off with #!/bin/sh than #!/bin/bash as
running bash by the sh symlink puts it into borne shell compatability
mode, which is better for scripting.  I think Simon ran into problems with
that some time back.

Alex.

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.


Lynx friendly