test a script about mount points


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test a script about mount points
# 1  
Old 11-13-2008
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  
Old 11-13-2008
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  
Old 11-18-2008
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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Mount Points usage check with shell script

Hi Everyone, Just in need of your help again, I have managed to get a script to check the linux disk usage stuff. But wanted to tweak it little more to get the desired output. Requirement: Now its querying only one mount point, As its not saving in array instead calling it as variables. So... (1 Reply)
Discussion started by: thiyagoo
1 Replies

2. Red Hat

Mount Points? How?

Hi folks, I have been asked to performed the following: Add the following new moint points systemA:/avp and SystemB:/usr/sap/trans to be the new linux server ZZZ How can I add those mount points and how those mount points can become another linuz server?:wall::wall::wall: (2 Replies)
Discussion started by: 300zxmuro
2 Replies

3. Shell Programming and Scripting

script to monitor mount points on linux box

Hi, the following shell script is not working on linux box sugeestions appreciated. FILE1=/tmp/check1.txt FILE2=/tmp/check2.txt df -k | awk '{if (NR != 1) print $5 $6}' > ${FILE1} awk -F"%" '{if ($1 > 90) print "Warning: Filesystem " $2 " over 90% threshold (" $1 "%)"}' ${FILE1} >... (7 Replies)
Discussion started by: prakash.gr
7 Replies

4. Shell Programming and Scripting

Shell Script to see the mount points.

Hi all, First of all I dont even know the ABC of scripting .. But now I want a Script to see the mount points of the file systems Can any body help plsssssssss :o (1 Reply)
Discussion started by: priky
1 Replies

5. UNIX for Advanced & Expert Users

mount points

hi, I believe a mount point does not have to be a physical disk, but rather a logical one? Is this correct? if so, how can I find out if my mount points are on different physical disks? thanks (9 Replies)
Discussion started by: JamesByars
9 Replies

6. UNIX for Dummies Questions & Answers

mount points are already mounted

Hi, I have some issue with the mounting/unmounting on my sun solaris box. Actually their is one script that mount the file system take the backup of databases and unmount the file system.Last week this script failed to mount the file system with the below error message: + echo fs_check.sh:... (1 Reply)
Discussion started by: biju.mp
1 Replies

7. HP-UX

Cannot unmount mount points??

When taking a snap, I have a script that stops any active snap. When running the script, I'm getting a message that u02 and u04 are already mounted. How can I find out what process(es) is/are latching on the these mount points? Thank you for your time. (1 Reply)
Discussion started by: genzbeat
1 Replies

8. UNIX for Dummies Questions & Answers

NFS Mount Points

Hi Solaris 10 On server A, there is a directory called data with 10 files. This data directory has a further 3 subdirectories, gl, pay, contract (for example) On server B, I want to see the server A data directory commands used: on server A, share -F nfs -o ro -d "<description">... (1 Reply)
Discussion started by: davidra
1 Replies

9. UNIX for Dummies Questions & Answers

Mount Points at reboot

How do I make a mount point reconnect at boot without editing /etc/fstab? Is there an option (or switch) to make this persistent when issuing the mount command from a client? (1 Reply)
Discussion started by: AIXdumb455
1 Replies

10. UNIX for Advanced & Expert Users

mount points

sometimes in Solaris 8 when I go to mount filesystems using either the mount command or by editing the /etc/vfstab, i get a nice little error message saying the the number of allowable mount points has been exceeded. I have read man pages until I am blue in the face and no where can I find what the... (3 Replies)
Discussion started by: manderson19
3 Replies
Login or Register to Ask a Question