You are not logged in.

#1 2008-04-20 13:37:18

INCSlayer
Member
From: Sweden
Registered: 2007-09-06
Posts: 296
Website

possibly simple bash if script

i want to have a script that mounts something then opens konqueror and then if run a second time checks if the mount is present in mtab simply opens it

so example:
mount -o loop /iso/test /home/user/tester
konqueror /home/user/tester

thats how far i know how to code (i know that aint really CODE code)
but i want it to run a check to see if /iso/test is mounted to /user/tester then dont try to remount it but simply open konqueror

i dont know how to make a if that checks mtab so if someone knows how i go about doing this or has a handy script i can butcher for code i would be very gratefull

//INCSlayer


dovie andi se tovya sagain

Offline

#2 2008-04-20 15:52:43

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: possibly simple bash if script

This is how I do it with my mount script in Openbox:

grep $1 /etc/mtab && umount /dev/$1 || mount /dev/$1

This allows you to run the script without options (except for the device parameter, $1); if it finds the device mounted, it will umount it, if not, it will attempt to mount it.

A few caveats come with this code:
* the device name has to be identical to the name of the mount point
* the mount point has to be present in /etc/fstab

The big advantage is these uniform conditions allow for a multi-purpose setup - I use this line of code for all my devices - removable hard disks, USB sticks, memory cards, and opticals. It does require consistent udev rules and mount points though smile.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#3 2008-04-20 22:30:52

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: possibly simple bash if script

B wrote:

This is how I do it with my mount script in Openbox:

grep $1 /etc/mtab && umount /dev/$1 || mount /dev/$1

This allows you to run the script without options (except for the device parameter, $1); if it finds the device mounted, it will umount it, if not, it will attempt to mount it.

Not quite... In addition to that it will try to mount the device if umount fails. Unfortunately, there are no ternary conditional statements in bash. You're better off splitting it up into a multi-line evaluation statement using $?:

grep "$1" /etc/mtab 2>&1
if [ "$1" = "0" ]; then
     mounted_procedures
else
    unmounted_procedures
fi

But I believe he is looking to simply open konqueror if it is already mounted:

grep "$1" /etc/mtab 2>&1 || mount "$1" /mnt/...
konqueror "$1"

That will mount the device if not done so already then open it up with konqeuror.

Offline

#4 2008-04-21 18:57:06

INCSlayer
Member
From: Sweden
Registered: 2007-09-06
Posts: 296
Website

Re: possibly simple bash if script

im afraid i dont understand how that last bit of code works and maybe i should be more precise its not actually a device im mounting but a folder on a ftp
im mounting it with curlftpfs
so this is the command:
curlftpfs incslayer.ath.cx/Music /home/bosse/.Musik -o user=username:password

and this is what shows in mtab (there is more in there of course but thats the important line):
curlftpfs /home/bosse/.Musik fuse.curlftpfs rw,nosuid,nodev,user=bosse 0 0
its on the tenth row if that helps
im not sure what the $1 means but if this helps or you need more information to help me with this il gladly provide it


dovie andi se tovya sagain

Offline

#5 2008-04-21 22:37:47

kumico
Member
Registered: 2007-09-28
Posts: 224
Website

Re: possibly simple bash if script

$1,$2 ... are special bash variables used for the command line arguments,, `command hello world`;; $1=hello ...

this might work

#!/bin/sh
grep "$1" /etc/mtab > /dev/null # try to find the device(server bits) in mtab;; send all stdout output to /dev/null
if [ $? -ne 0 ]; then # if the return code($?) to the last command is not 0, then it found nothing
    # err=$(mount $* 2>&1) # uncomment this (remove '#' from the start of the line to use mount)
    # err=$(curftpfs $* 2>&1) # uncomment this and comment out the above to use curftpfs
    if [ $? -ne 0 ]; then # again, it return code isn't 0 then something went wrong
       echo "Mount Failed!"
       echo "--------------------------"
       echo $err # tell you about the actual error
       echo "--------------------------"
       exit 1 # exit if the initial mount fails
    fi
fi
konqueror "$2" # if all went well, start k*

this of course assume you alway mount with said command, i.e different arguments are in the same position,,
(incslayer.ath.cx/Music and /home/bosse/.Musik equivalents are always the first and second arguments)

EDIT: added some comments to the code

with this script you can copy it to two files,,
one using the curftpfs line to mount ur server
and one using the mount line to mount any normal devices

read line 4&5

Last edited by kumico (2008-04-21 23:04:35)

Offline

#6 2008-04-22 00:13:39

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: possibly simple bash if script

T-Dawg wrote:
B wrote:

This is how I do it with my mount script in Openbox:

grep $1 /etc/mtab && umount /dev/$1 || mount /dev/$1

This allows you to run the script without options (except for the device parameter, $1); if it finds the device mounted, it will umount it, if not, it will attempt to mount it.

Not quite... In addition to that it will try to mount the device if umount fails. Unfortunately, there are no ternary conditional statements in bash.

Correct smile. I never thought of that, then again, it cannot mount it either if it's already mounted, so... tongue.

Never had problems with it anyway.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

Board footer

Powered by FluxBB