Page 1 of 1
Use rev command to get name of mounted share
Posted: Wed 14 Feb 2018, 19:07
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
_____________________________________________________________
Re: rev command use
Posted: Wed 14 Feb 2018, 20:06
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
to get the last field.
But it's good to know there are several solutions for this.
BFN.
Posted: Wed 14 Feb 2018, 21:19
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
_____________________________________
Posted: Fri 16 Feb 2018, 20:10
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.
.
Posted: Sat 17 Feb 2018, 16:07
by don570
When I tested
I discovered that it outputs a column of fields rather than the last field
The solution was to use the 'echo' command.
Now I get the share folder
/root/network/GOLD-XP-2016-SharedDocs
__________________________________________
Posted: Sat 17 Feb 2018, 17:25
by seaside
don570 wrote:When I tested
I discovered that it outputs a column of fields rather than the last field
The solution was to use the 'echo' command.
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
Posted: Sat 17 Feb 2018, 17:47
by musher0
don570 wrote:When I tested
I discovered that it outputs a column of fields rather than the last field
The solution was to use the 'echo' command.
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.
Posted: Sat 17 Feb 2018, 18:27
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.
Posted: Sat 17 Feb 2018, 19:10
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
___________
Posted: Sun 18 Feb 2018, 06:34
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:
Posted: Tue 20 Feb 2018, 17:59
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.
________________________________________
Posted: Tue 20 Feb 2018, 18:45
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.
________________________________________
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.