Hi Members,
newbie. help pls. I need a script/command for the following:
scenario:
check if /dev/sdc is available in /proc/mounts
if available , echo “already mounted”
if not, execute the mount command
I need the command on how to check if /dev/sdc is available in /proc/mounts. I tried something like this but doesn’t work
if cat /proc/mounts | grep -i “/dev/sdc” == /dev/sdc /u01 …] then
echo “already mounted”
else
mount -t ext3 /dev/sdc /u01
fi
I do not know how to write the if with cat command.
I will be adding this script to /etc/profile.local, so that everytime i invoke the terminal or bash, it has to check this and act accordingly. I googled this but in vain.
I would also request the forum members to enlighten me on the usage of different options on the if command
for example:
if -s /proc/mounts ]
if -x /proc/mounts ]
if -f /proc/mounts ]
if -d /proc/mounts ]
where do i find the man pages for these and other options as well.
ayyappa1 wrote:
> Hi Members,
> newbie. help pls. I need a script/command for the following:
>
> scenario:
> check if /dev/sdc is available in /proc/mounts
Why not use the mount command? It gives the same output as /proc/mounts.
> if available , echo “already mounted”
> if not, execute the mount command
>
> I need the command on how to check if /dev/sdc is available in
> /proc/mounts. I tried something like this but doesn’t work
> if cat /proc/mounts | grep -i “/dev/sdc” == /dev/sdc /u01 …] then
> echo “already mounted”
> else
> mount -t ext3 /dev/sdc /u01
> fi
A short(er) version would be:
mount |grep -q sdb && echo already mounted || mount -t ext3 /dev/sdc /u01
> I do not know how to write the if with cat command.
No need for cat anyway, grep reads from a file or from stdin.
> I will be adding this script to /etc/profile.local, so that everytime i
> invoke the terminal or bash, it has to check this and act accordingly. I
> googled this but in vain.
Then why not put /dev/sdb in /etc/fstab and let it be automounted?
ayyappa1 wrote:
> I would also request the forum members to enlighten me on the usage of
> different options on the if command
> for example:
> if -s /proc/mounts ]
> if -x /proc/mounts ]
> if -f /proc/mounts ]
> if -d /proc/mounts ]
>
> where do i find the man pages for these and other options as well.
help: help -s] [pattern …]
Display helpful information about builtin commands. If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
otherwise a list of the builtins is printed. The -s option
restricts the output for each builtin command matching PATTERN to
a short usage synopsis.
Return codes in shell are 0 = true, everything else is false.
You can use || (two vertical bars) to signify do the next
command if the prior command returned false and you can use
&& (two ampersands) to signify do the next command if the
prior command returned true.
So below, I check the status of /dev/sdc with regards to df.
It will fail if /dev/sdc isn’t mounted… in which case we
attempt to mount it, and if that fails we say so and exit.
Otherwise, we assume that /dev/sdc was mounted.
It’s not a perfect script… just nice doing it all
in a couple of lines… and no if’s.
In a script:
df /dev/sdc >/dev/null 2>&1 || mount -t ext3 /dev/sdc /u01 ||
echo "Mount of /dev/sdc failed" && exit 1
echo "I think /dev/sdc is mounted"
If you want, I can do this using if’s and such… but you
can probably do likewise now that you see what I’m doing
above.
Many Thanks to microchip8, LittleRedRooster and cjcox for helping me out.
@littleredrooster:
I knew mount and /proc/mounts are the same but no real reason behind using /proc/mounts(mount command gets data from /proc/mounts so i used the source). Atleast the good thing is that now i know how to use grep -q from your code.
I’m using iscsi initiator and the logical volumes are detected after the fstab contents are mounted. So i will have to mount it during login. But to my knowledge, the option for me is to either put this code on to /etc/bash.bashrc.local or /etc/profile.local
@cjcox:
could you please explain what this particular code does >/dev/null 2>&1
On Thu, 2009-05-07 at 16:16 +0000, ayyappa1 wrote:
> Thanks microchip8. The document abt /dev/null was very helpful.
>
> I tried the following but does not work. The problem is because the
> /etc/profile.local execution is unsuccessful i’m unable to login as
> root.
>
> df -h /dev/sdc | grep -E ‘39G|/u01/opt/test’ > /dev/null 2>&1 || mount
> -t ext3 /dev/sdc /u01/opt/test || echo “mount failed” && exit 1
> echo “already mounted” && exit 1
You should put the commands into a script and call it from the
profile. Alternatively, you can rewrite things so that the exit’s
aren’t called.
However, I think now it might be a good time to figure out exactly
what you’re trying to do… my guess is there’s a better solution
to whatever problem you’re trying to solve.