If the path is for a device that isn't mounted then we take the the left part of the realpath up until the path is one level deeper than the directory that is named "mnt" in the path. So for example if the following file isn't mounted:
Code: Select all
/initrd/mnt/dev_save/Tiny_Puduan/PreAlpha11.4/puppy_tascii.sfs
Code: Select all
/initrd/mnt/dev_save
Code: Select all
function realpath(p){
cmd="realpath '" p "'"
while ((cmd | getline )){
rp=$0
break
}
if (length(rp)>0){
return rp
} else {
print "Warning realpath not found for " p > "/dev/stderr"
return p
}
}
function get_dev_mp(path, mp,rp,count){
#cmd="cat /proc/mounts | grep \"$(realpath " dev ")\""
rp=realpath(path)
cmd="cat /proc/mounts" # | grep \"$(realpath '" rp "')\""
while ((cmd | getline )){
#dev=$1 #don't need this yet
print "cmd_out=" $0 > "/dev/stderr"
mp=$2
cmd2="echo " rp " | grep -c " mp
while ((cmd2 | getline )){
print "cmd2_out=" $0 > "/dev/stderr"
count=$0
break #this is uncessary
}
if ( count>0 ){
return mp
break
}
}
if (count==0){
print "Warning: dev not mounted! Inferring dev mount point from path" > "/dev/stderr"
mp=gensub(/^(.*[/]mnt[/][^/]*)[/].*/,"\\1",1,rp)
return mp
}
}
Here is the console output:
Code: Select all
path=/initrd/mnt/dev_save/Tiny_Puduan/PreAlpha11.4/puppy_tascii.sfs
cmd_out=/dev/sda2 /initrd/mnt/dev_save ext4 rw,noatime 0 0
cmd2_out=1
dev_mnt_pt=/initrd/mnt/dev_save
While it is the case that I am using nested while loops. The inner loop only loops once. The inner loop is necessary because in AWK, the only way (that I know of) to return a value from an external function (other than an exit code), is to pipe the shell command to AWKs getline command. You can read about the getline command at:
https://www.gnu.org/software/gawk/manua ... tline.html
P.S. the project "psandbox" which the above code is part of is not ready for testers even though some features of it appear to work.