Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 14 Nov 2018, 09:36
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Use rev command to get name of mounted share
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [12 Posts]  
Author Message
don570


Joined: 10 Mar 2010
Posts: 5170
Location: Ontario

PostPosted: Wed 14 Feb 2018, 15:07    Post subject:  Use rev command to get name of mounted share  

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/viewtopic.php?search_id=506564127&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:
# echo `df` | grep "/root/network/"|rev| cut -d " " -f1|rev
/root/network/GOLD-XP-2016-SharedDocs


_____________________________________________________________
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12959
Location: Gatineau (Qc), Canada

PostPosted: Wed 14 Feb 2018, 16:06    Post subject: Re: rev command use  

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/viewtopic.php?search_id=506564127&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:
# echo `df` | grep "/root/network/"|rev| cut -d " " -f1|rev
/root/network/GOLD-XP-2016-SharedDocs

_____________________________________________________________

HI don570.

I probably would have used
Code:
df | awk '{ print $NF }'
to get the last field.
But it's good to know there are several solutions for this.

BFN.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5170
Location: Ontario

PostPosted: Wed 14 Feb 2018, 17:19    Post subject:  

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/22727107/how-to-find-the-last-field-using-cut
_____________________________________
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 857

PostPosted: Fri 16 Feb 2018, 16:10    Post subject:  

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:
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.

.
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5170
Location: Ontario

PostPosted: Sat 17 Feb 2018, 12:07    Post subject:  

When I tested
Code:
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:
echo `df` | awk '{ print $NF }'



Now I get the share folder

/root/network/GOLD-XP-2016-SharedDocs
__________________________________________
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 919

PostPosted: Sat 17 Feb 2018, 13:25    Post subject:  

don570 wrote:
When I tested
Code:
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:
echo `df` | awk '{ print $NF }'



Now I get the share folder

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

You could also do this-

Code:
 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
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12959
Location: Gatineau (Qc), Canada

PostPosted: Sat 17 Feb 2018, 13:47    Post subject:  

don570 wrote:
When I tested
Code:
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:
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:
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
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
some1

Joined: 17 Jan 2013
Posts: 87

PostPosted: Sat 17 Feb 2018, 14:27    Post subject:  

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.
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5170
Location: Ontario

PostPosted: Sat 17 Feb 2018, 15:10    Post subject:  

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/viewtopic.php?p=983353#983353
___________
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1677
Location: Japan

PostPosted: Sun 18 Feb 2018, 02:34    Post subject:  

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:
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:
df | awk  '/\/root\/network\// {print $NF}'


Using grep should even be simpler:
Code:
df | grep -o '/root/network/.*'
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5170
Location: Ontario

PostPosted: Tue 20 Feb 2018, 13:59    Post subject:  

Quote:
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

__________________________________________

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


Interesting! Never saw that option before. Rolling Eyes
________________________________________
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 12959
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Feb 2018, 14:45    Post subject:  

don570 wrote:
Quote:
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
__________________________________________
Quote:
df | grep -o '/root/network/.*'
Interesting! Never saw that option before. Rolling Eyes
________________________________________
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:
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" Wink County, Yukon...)

TWYL.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [12 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0941s ][ Queries: 14 (0.0083s) ][ GZIP on ]