Use rev command to get name of mounted share

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

Use rev command to get name of mounted share

#1 Post by don570 »

Here's a tip I got from looking at the script 'mkplaylist' by Shinobar
available in ffconvert pet package....
http://www.murga-linux.com/puppy/viewto ... 27&t=54056


Explanation: I was having problems finding the correct field to cut
since the field was located at the end of a line.
Different operating systems were giving me different field locations.
However using 'rev' command makes the field number 1
and I can get the name of the mounted share folder consistently now.

Note that 'rev' command is used twice.

Code: Select all

# echo `df` | grep "/root/network/"|rev| cut -d " " -f1|rev
/root/network/GOLD-XP-2016-SharedDocs

_____________________________________________________________

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

Re: rev command use

#2 Post by musher0 »

don570 wrote:Here's a tip I got from looking at the script 'mkplaylist' by Shinobar
available in ffconvert pet package....
http://www.murga-linux.com/puppy/viewto ... 27&t=54056

Explanation: I was having problems finding the correct field to cut
since the field was located at the end of a line.
Different operating systems were giving me different field locations.
However using 'rev' command makes the field number 1
and I can get the name of the mounted share folder consistently now.

Note that 'rev' command is used twice.

Code: Select all

# echo `df` | grep "/root/network/"|rev| cut -d " " -f1|rev
/root/network/GOLD-XP-2016-SharedDocs
_____________________________________________________________
HI don570.

I probably would have used

Code: Select all

df | awk '{ print $NF }' 
to get the last field.
But it's good to know there are several solutions for this.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#3 Post by don570 »

Thanks for info.
I'll check if it works in a script I wrote to mount and unmount a computer share in fatdogarm.

___________________________________

Reference site shows various methods including awk
https://stackoverflow.com/questions/227 ... -using-cut
_____________________________________

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#4 Post by jafadmin »

When I write scripts that are dialogs I always grab the script name to put as the dialog title. This way if user wants to rename the script, it displays the name they give it:

Code: Select all

MyName=$(echo $0 | rev | cut -d '/' -f1 | rev| tr '-' ' ')
'$0' holds the script name including full path. This clips off the path and leaves the name.

.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#5 Post by don570 »

When I tested

Code: Select all

df | awk '{ print $NF }'

I discovered that it outputs a column of fields rather than the last field
The solution was to use the 'echo' command.

Code: Select all

echo `df` | awk '{ print $NF }'

Now I get the share folder

/root/network/GOLD-XP-2016-SharedDocs
__________________________________________

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

don570 wrote:When I tested

Code: Select all

df | awk '{ print $NF }'

I discovered that it outputs a column of fields rather than the last field
The solution was to use the 'echo' command.

Code: Select all

echo `df` | awk '{ print $NF }'

Now I get the share folder

/root/network/GOLD-XP-2016-SharedDocs
__________________________________________
You could also do this-

Code: Select all

 df | awk  '/GOLD-XP-2016-SharedDocs/ {print $NF}'
Look for the line containing "GOLD-XP-2016-SharedDocs" and then print the last field of that line.

Cheers,
s

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#7 Post by musher0 »

don570 wrote:When I tested

Code: Select all

df | awk '{ print $NF }'

I discovered that it outputs a column of fields rather than the last field
The solution was to use the 'echo' command.

Code: Select all

echo `df` | awk '{ print $NF }'

Now I get the share folder

/root/network/GOLD-XP-2016-SharedDocs
__________________________________________
Strange.

My < df | awk > command does not need to be echoed.

Code: Select all

df | awk '/mnt/ { print $NF }'
/mnt/sda1
/initrd/mnt/tmpfs
/mnt/sda5
/mnt/sda6
/mnt/sda7
/mnt/sda8
/mnt/sdb2
/mnt/sdb5
/mnt/sdb6
/mnt/sdb7
/mnt/sdc3
/mnt/sdc5
/mnt/sdc6
/mnt/sdc7
/mnt/sdc8
/mnt/ram1
What "model" of awk do you have? I use mawk.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

some1
Posts: 117
Joined: Thu 17 Jan 2013, 11:07

#8 Post by some1 »

don570:

Use seaside's suggestion/explanation

Your awk is ok.
--
What you think works is a sort of a happenstance:
By echoing the output of df - you get a space-delimited list/line - the last field of that list "happens" to be the item you want.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#9 Post by don570 »

some1 wrote: the last field of that list "happens" to be the item you want.
Samba shares always are always mounted last .
Is there any exceptions???

It's the folder that is mounted is what I need so i can unmount it
(with umount command)

echo `df` | awk '{ print $NF }' seems to work on my raspberry pi2.
http://www.murga-linux.com/puppy/viewto ... 353#983353
___________

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#10 Post by MochiMoppel »

don570 wrote:echo `df` | awk '{ print $NF }' seems to work on my raspberry pi2.
Maybe - if you are lucky. Removing another restriction (requiring df output to contain "/root/network/") makes it even less reliable than your original code.

Your original code should work fine, though not very efficiently, without the needless echo:

Code: Select all

df | grep "/root/network/"|rev| cut -d " " -f1|rev
If you don't remove echo the code will print the very last field of the def output, even if the field does not contain "/root/network/".

What's wrong with seaside's approach?

Code: Select all

df | awk  '/\/root\/network\// {print $NF}'
Using grep should even be simpler:

Code: Select all

df | grep -o '/root/network/.*'

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#11 Post by don570 »

Maybe - if you are lucky.
I checked with fatdogarm on a raspberry pi and I was wrong!!!

USB sticks that are inserted afterwards are placed at bottom of list.
So I removed the echo and used musher0 idea.

df then awk then grep

__________________________________________
df | grep -o '/root/network/.*'
Interesting! Never saw that option before. :roll:
________________________________________

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#12 Post by musher0 »

don570 wrote:
Maybe - if you are lucky.
I checked with fatdogarm on a raspberry pi and I was wrong!!!

USB sticks that are inserted afterwards are placed at bottom of list.
So I removed the echo and used musher0 idea.

df then awk then grep
__________________________________________
df | grep -o '/root/network/.*'
Interesting! Never saw that option before. :roll:
________________________________________
Hi don.

Thanks for the vote of confidence.

When you say:
> df then awk then grep
you do not need that grep at the end, because awk has one built-in.

You could simply type:

Code: Select all

df | awk '$NF ~ /GOLD/ { print $NF }'
The < $NF ~ /GOLD/ > part means "The last field contains 'GOLD'."
In other words, you are telling awk:
"If there is a field that contains 'GOLD' in this df listing, print it."

(Which reminds me: I once met an old man who spent an entire afternoon telling
me tall tales about such a field in "Hope" ;) County, Yukon...)

TWYL.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Post Reply