Register
It is currently Fri Dec 19, 2014 12:44 am

test a script about mount points


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Thu Nov 13, 2008 2:57 am   

Joined: Thu Nov 13, 2008 2:53 am
Posts: 1
Location: france
Hi there,
I would like people to test a script on as many situations as possible so we can find out errors and lacks.
I wrote the script to help me work around mount points, especially when doing batch job on files (like backup) and to avoid duplicate operations through mount points.
For example, consider the following mount points :
Code:
/home/santiago/folder       ->  /var/www/folder
/home/santiago/folder1      ->  /var/www/otherfolder

And the following files :
Code:
/home/santiago/file
/home/santiago/folder/file
/home/santiago/folder1/touch
/home/santiago/test/file2

If I backup /home and /var, I would like to be able to know what are the mount points under /home because it's not necesary to backup them. The scripts does it like this :
Code:
$ qmtpt /home
/home/santiago/folder
/home/santiago/folder1

Then I might wonder if a specific path is under a mount point or not. The scripts does it like this :
Code:
$ qmtpt /home/santiago/folder/file > /dev/null; echo $?
1
$ qmtpt /home/santiago/test > /dev/null; echo $?
0

Now, here is the script. It is supposed to work with extremely unusual paths (with all the weird character you can imagine). If you care about trying it, please report me any bug or necessary improvement. By the way, if it works let me know as well!

Thanks in advance
Santiago

Code:
#!/bin/bash

# Syntax
syntax () {
cat << EOF
Usage: $(basename "$0") PATH
Display mount points under PATH to the  standard output and returns a value that
indicates if PATH is itself under a mount point. Return values are:
  0     PATH is NOT under a mount point
  1     PATH IS under a mount point
  2     invalid command line arguments
  3     PATH does NOT exist
  4     Cannot read from /etc/mtab
EOF
exit 0
}

# Error functions
error () {
    p=$(basename "$0")
    h='\nTry '\'$p' -h'\'' for more information.'
    case $1 in
        2) m='invalid option '$2$h; n=2;;
        3) m='option '$2' requires an argument'$h; n=2;;
        4) m='invalid command line arguments'$h; n=2;;
        5) m='missing arguments'$h; n=2;;
        6) m='too many arguments'$h; n=2;;
        7) m=$2': no such file or directory'; n=3;;
        8) m='Cannot read from '$2': file does NOT exist'; n=4;;
        9) m='Cannot read from '$2': file has NO read permission'; n=4;;
        *) m='unexpected error'; n=255;;
    esac
    echo -e "$p"': '"$m" 1>&2
    exit $n
}

# Read options one by one
while getopts ":h" flag; do
    case $flag in
        'h') syntax;;
        '?') error 2 $OPTARG;;
        ':') error 3 $OPTARG;;
        *)   error 4;;
    esac
done

# Read other arguments
[[ $# -lt $OPTIND ]] && error 5
[[ $# -gt $OPTIND ]] && error 6
path=${!#}
mtab='/etc/mtab'

# Check path
[[ -e $path ]] || error 7 $path
[[ -f $mtab ]] || error 8 $mtab
[[ -r $mtab ]] || error 9 $mtab

# Build mount points list
mtpt=$(awk '$3 == "none" { print $2 }' $mtab)
# Build absolute path
[[ ${path:0:1} != '/' ]] && path=$(pwd)'/'$path
# Remove trailing slash
[[ ${path:${#path}-1} = '/' ]] && path=${path:0:${#path}-1}
# Replace spaces and backslashes
path=${path//\\/\\\\134}
path=${path// /\\\\040}
# Escape dollars, asterisks, periods and brackets
path=${path//$/\\$}
path=${path//\*/\\*}
path=${path//\./\\.}
path=${path//[/\\[}
path=${path//]/\\]}
# Display mount points under path
echo "$mtpt" | grep -E '^'$path'(/|$)' | sed 's/\\040/ /g; s/\\134/\\/g'
# Check if path is under a mount point
for p in $(echo "$mtpt"); do
    echo "$path" | grep -E '^'$p'(/|$)' > /dev/null
    [[ $? -eq 0 ]] && exit 1
done
exit 0


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP