The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mount points JamesByars UNIX for Advanced & Expert Users 9 08-16-2008 07:36 AM
NFS Mount Points davidra UNIX for Dummies Questions & Answers 1 11-28-2007 04:49 PM
tar using mount points legato UNIX for Dummies Questions & Answers 3 03-30-2005 11:54 PM
mount points manderson19 UNIX for Advanced & Expert Users 3 04-29-2002 03:11 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-13-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Lightbulb test a script about mount points

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
  #2 (permalink)  
Old 11-13-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
You know, the -xdev command in find will tell find not to traverse file-system boundaries. You can also use -P to ensure symbolic links are never followed. Those tips might help those with more basic needs.
  #3 (permalink)  
Old 11-18-2008
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Thanks otheus!
This will be terribly helpfull. Actually I still need this function to use the mirror function in lftp. I need to exclude directories that are mount points like this:
Code:
path=/home/
lftp x.x.x.x -u user,password -e "mirror -R $(qmtpt "$path" | awk '{ ORS = " " } { print "-x", $0 }') $path $path; quit"
Closed Thread

Bookmarks

Tags
bind, grep, mount, script, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0