You are not logged in.

#1 2020-11-21 03:22:05

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Can you help me make a pipemenu to add to menu?

Hello there everyone!

I mount client web sites via SSHFS.  I use two simple bash files to mount and dismount these filesystems.  I do this for quite a few domains and I mount and dismount in the terminal.

cd .scripts
./mount_website1.com
unmount_website2.com

What I started thinking after playing around in /usr/bin, however, is that I might be able to do this via a pipemenu file for jgmenu.  What I'm hoping is that the pipemenu can detect whether a file system is mounted, if it isn't, offer me the mount option for that website and if it is, offer the unmount option.  I would like to do this for all required websites.

My thinking is that I might be able to look into a /home/schwim/remote/website1.com and if there are no files, the remote filesystem has not been mounted and should show the menu option to run /home/schwim/.scripts/mount_website1.com.  If files are found, show the menu option to unmount it.

I figured I could just duplicate these ifs from website1.com and ctrl+v them for each additional website.

The menu entries I'd like added would be:

- Remote FS
----Website1.com - Mount
----Website2.com - Unmount
----Website3.com - Mount
ETC.

Could someone help me with this?  I know nearly nothing about bash and the pipemenu files I'm looking at are a bit confusing to me, I'm unable to decipher anything to a point to be able to muddle my way through this on my own.

Thanks for your time!


Schw.im! A social site with an identity crisis.

Offline

#2 2020-11-21 16:02:28

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,152
Website

Re: Can you help me make a pipemenu to add to menu?

An example test.csv could be

woot, geany
waat, thunar
weet, galculator

and one could run this as

cat test.csv | jgmenu --simple

In your case you would replace geany and thunar and galculator with links to your scripts.

/me is by no means jgmenu expert, but this example^ should be simple enough to construct furter built upon with some additional reading.

Last edited by brontosaurusrex (2020-11-21 16:08:49)

Offline

#3 2020-11-21 17:06:14

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

Is there a command to test if a website is mounted?

If so, writing a shell script for a menu will be easy.

Offline

#4 2020-11-21 19:10:31

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

malm wrote:

Is there a command to test if a website is mounted?

If so, writing a shell script for a menu will be easy.

Would this be sufficient to work in the script?

schwim@schwim-vm-bll:~/Remote$ mountpoint ~/Remote/bhmbackflow.com ; echo $?
/home/schwim/Remote/bhmbackflow.com is not a mountpoint
1

schwim@schwim-vm-bll:~$ cd .scripts
schwim@schwim-vm-bll:~/.scripts$ ./bhm_mount

schwim@schwim-vm-bll:~/Remote$ mountpoint ~/Remote/bhmbackflow.com ; echo $?
/home/schwim/Remote/bhmbackflow.com is a mountpoint
0
schwim@schwim-vm-bll:~/Remote$


Schw.im! A social site with an identity crisis.

Offline

#5 2020-11-21 21:00:39

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

Would this work?

#!/bin/sh

urls="foo.com bar.com"

for u in ${urls}; do
	if mountpoint "${HOME}/Remote/${u}"; then
		printf '%b\n' "unmount ${u}"
	elif
		printf '%b\n' "mount ${u}"
	fi
done

Just run it in the terminal first to check the output.

When happy do

./script.sh | jgmenu --simple

As a final step, you could wrap it like this to avoid having to do the piping in the terminal

#!/bin/sh
(
    <code>
) | jgmenu --simple

Offline

#6 2020-11-22 13:40:29

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

Ok, so I've got the following in sshfs-pipemenu which is saved in ~/bin/X11:

#!/bin/bash

urls="air@aircooledaddiction.com bhm@bhmbackflow.com"

for u in ${urls}; do
	IFS=@ read -r left right <<< "$u"
	if mountpoint "${HOME}/Remote/${right}"; then
		#printf '%b\n' "unmount ${u}"
		menuItem "Unmount $right" "fusermount -u /home/schwim/Remote/$right"
		

	else
		#printf '%b\n' "mount ${u}"
		menuItem "Mount $right" "sshfs -o workaround=rename $u:/home/bhm /home/schwim/Remote/$right"

	fi
done

Then have called it in jgmenu:

Remote File Systems,^pipe(jgmenu_run ob --cmd="sshfs-pipemenu" --tag="bl-sshfs-pipemenu")

I can see remote file systems in jgmenu now but nothing shows when hovering over it.  Can I save the file in ~/bin/Xll and have it picked up or do I need to save it to /usr/bin/X11 to get it to work?  If I run the script in the terminal, although it doesn't understand the menuitem line, it does work properly in reading the directories and telling whether a filesystem is mounted or not.

Last edited by schwim (2020-11-22 13:45:27)


Schw.im! A social site with an identity crisis.

Offline

#7 2020-11-22 14:20:40

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

Ah. No need to produce XML output only to convert it back using "jgmenu_run ob". Just stick with the much simpler jgmenu CSV syntax.

So, there is no need to use 'menuItem'

Also, when you call the pipemenu, just do "Remote File Systems,^pipe(sshfs-pipemenu)"

Offline

#8 2020-11-22 14:31:10

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

malm wrote:

Ah. No need to produce XML output only to convert it back using "jgmenu_run ob". Just stick with the much simpler jgmenu CSV syntax.

So, there is no need to use 'menuItem'

I don't understand what you mean by this, I'm looking at the existing bl pipe files and borrowing from that code for my needs.  Could you provide an example based off of my attempt at this?

Current code:

#!/bin/bash

urls="air@aircooledaddiction.com bhm@bhmbackflow.com"

for u in ${urls}; do
	IFS=@ read -r left right <<< "$u"
	if mountpoint "${HOME}/Remote/${right}"; then
		#printf '%b\n' "unmount ${u}"
		menuItem "Unmount ${right}" "fusermount -u /home/schwim/Remote/${right}"
		

	else
		#printf '%b\n' "mount ${u}"
		menuItem "Mount ${right}" "sshfs -o workaround=rename $u:/home/bhm /home/schwim/Remote/${right}"

	fi
done

Schw.im! A social site with an identity crisis.

Offline

#9 2020-11-22 14:54:43

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

I think it would be like this.

Before running it as a pipemenu, just run "sshfs-pipemenu" from the terminal and make sure the output looks okay (paste here if unsure).

#!/bin/bash

urls="air@aircooledaddiction.com bhm@bhmbackflow.com"

for u in ${urls}; do
	IFS=@ read -r left right <<< "$u"
	if mountpoint "${HOME}/Remote/${right}"; then
		printf '%b\n' "unmount ${right},fusermount -u /home/schwim/Remote/${right}"
	else
		printf '%b\n' "mount ${right},sshfs -o workaround=rename $u:/home/bhm /home/schwim/Remote/${right}"
	fi
done

Offline

#10 2020-11-22 14:57:08

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

PS. Have a look at https://jgmenu.github.io/jgmenututorial.7.html#lesson4 to get your head around the jgmenu syntax. Lesson 4 should explain the printf lines in the script above.

Offline

#11 2020-11-22 15:31:31

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

{EDIT}

Ok, got the file working in jgmenu via the following entry:

Remote File Systems,^pipe(/home/schwim/bin/X11/sshfs-pipemenu)

But there's a problem:

6ACmCoQ.png?1

Is there a way for me to suppress the output of mountpoint "${HOME}/Remote/${right} so that it doesn't show in the menu?  If not, si there another way for me to check that won't provide an output?

Last edited by schwim (2020-11-22 15:36:03)


Schw.im! A social site with an identity crisis.

Offline

#12 2020-11-22 15:35:19

damo
....moderator....
Registered: 2015-08-20
Posts: 6,404

Re: Can you help me make a pipemenu to add to menu?

"menuItem" is a variable used by BL and sourced from /usr/lib/bunsen/common/bl-includes.

You should have seen this in the existing BL pipemenus.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#13 2020-11-22 15:49:35

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

damo wrote:

You should have seen this in the existing BL pipemenus.

I did, which was why I was trying to use it.  The printf method is working for me as well, however.

The only issue I'm experiencing right now, however is that mountpoint is providing text that is showing up in the menu.  The mount and unmount portion is working correctly, I just need to find a way to get the mountpoint check to not provide any text in the menu:

6ACmCoQ.png?1


Schw.im! A social site with an identity crisis.

Offline

#14 2020-11-22 15:54:45

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

Add >/dev/null after the mountpoint command (just before the semicolon)

Offline

#15 2020-11-22 16:27:01

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

ok, everything's working fantastic so i'd like to make some changes smile

The way it's set up right now, I'll have to edit the script to reflect changes in the mounts I've got set up.  I'm wondering if I can handle this automatically by the script.

If I named the directories in the Remote dir like air-aircooledaddiction.com which is the mount user and url separated by a dash then split that in the script, I would have all the info necessary to mount the drive.

To make it work though, I'd need to scan the Remote directory and have it build an array of existing directories found inside.  Is there a way for me to do this in the bash script?  Can I have it scan the remote dir and build an array of the dirs found inside?


Schw.im! A social site with an identity crisis.

Offline

#16 2020-11-22 16:54:40

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

Ok, I found this but it prints the entire path and not just the directory name and it also includes the parent dir with no subdir.  Will continue googling:

for d in $(find /home/schwim/Remote -maxdepth 1 -type d)
do
  #Do something, the directory is accessible with $d:
  echo $d
done

schwim@schwim-vm-bll:~/bin/X11$ ./sshfs-pipemenu
/home/schwim/Remote
/home/schwim/Remote/bhmbackflow.com
/home/schwim/Remote/schwimserver3.com
/home/schwim/Remote/MEGA
/home/schwim/Remote/aircooledaddiction.com
/home/schwim/Remote/schwimserver2.com
schwim@schwim-vm-bll:~/bin/X11$

Last edited by schwim (2020-11-22 16:56:32)


Schw.im! A social site with an identity crisis.

Offline

#17 2020-11-22 17:57:23

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

The best shell approach is

for u in $HOME/Remote/*
do
  # do stuff
done

If you only want to process directories, could you do a test like this:

for u in $HOME/Remote/*
do
  if [ -d $u ]; then
    # do stuff
  fi
done

See `man test` for other options

Last edited by malm (2020-11-22 17:59:32)

Offline

#18 2020-11-22 18:03:07

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

The [ ] construct is just short hand for the command test. So you could do `test -d $u` instead.

You could also avoid the if-then-fi by simply doing

[ -d $u ] || continue

`A && B` means if command A is successful, execute command B
`A || B` means if command A is NOT successful, then do B

Offline

#19 2020-11-22 18:13:48

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

malm wrote:

The best shell approach is

If you only want to process directories, could you do a test like this:

for u in $HOME/Remote/*
do
  if [ -d $u ]; then
    # do stuff
  fi
done

Is there a way to get $u to only contain the dir name and not the path to the directory?

For instance, aircooledaddiction.com instead of /home/schwim/remote/aircooledaddiction.com?


Schw.im! A social site with an identity crisis.

Offline

#20 2020-11-22 18:28:22

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

Use the command basename

Offline

#21 2020-11-22 18:32:08

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

These two sites by Dylan Araps can be quite useful:

pure bash bible
pure sh bible

Offline

#22 2020-11-22 18:58:19

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

Ok, so I've outsmarted myself.  This works conditionally:

#!/bin/bash

for u in $HOME/Remote/SSHFS/*
do
  if [ -d $u ]; then
    basename "$u" >/dev/null
	acct="$(basename -- $u)"
	
	IFS=- read -r left right <<< "$acct"
	if mountpoint "${HOME}/Remote/SSHFS/${acct}" >/dev/null; then
		printf '%b\n' "unmount ${right},fusermount -u /home/schwim/Remote/${right}"
	else
		printf '%b\n' "mount ${right},sshfs -o workaround=rename $left@$right:/home/$left /home/schwim/Remote/SSHFS/${acct}"
	fi

  fi
done

The condition being, as long as the remote mount point is /home/$acctname for:

sshfs -o workaround=rename $left@$right:/home/$left /home/schwim/Remote/SSHFS

The issue is that not all of my mount points follow that logical account path.  Some are /var/www/acct, etc.

So I'm back to hardcoding the entries so I can pass the remote path to the mount command.

I could do this with a config file in the Remote directory that would contain all the necessary information needed for the mount command.  Is there a smoother way of handling this that I'm missing?  If not,  what would be the best way of passing this info to the bash script?


Schw.im! A social site with an identity crisis.

Offline

#23 2020-11-22 19:23:37

malm
jgmenu developer
Registered: 2016-10-13
Posts: 643
Website

Re: Can you help me make a pipemenu to add to menu?

If you’re okay, I’ll step away from this now. It’s starting to get a bit complex smile

You’ll have to strike a balance between how many cases you want to handle vs how much time you spend on it.

Copy+paste the for loop to deal with each case would be dirty, but simple.

Might be able to wrap current for loop in an another for loop which covers the different paths/accounts.

Or write all paths/accounts to a cache file and the read it line by line.

Try to really thing it through and aim for a simple solution. Clean and simple code where you can.

Offline

#24 2020-11-22 19:26:51

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,152
Website

Re: Can you help me make a pipemenu to add to menu?

malm wrote:

Or write all paths/accounts to a cache file and the read it line by line.

example reader

#!/bin/bash

while read -r p; do
  echo "$p"
done <data.txt

example data.txt

some/info/here
some other info here
and here

Last edited by brontosaurusrex (2020-11-22 19:27:21)

Offline

#25 2020-11-22 20:21:13

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 325
Website

Re: Can you help me make a pipemenu to add to menu?

brontosaurusrex wrote:
malm wrote:

Or write all paths/accounts to a cache file and the read it line by line.

example reader

#!/bin/bash

while read -r p; do
  echo "$p"
done <data.txt

example data.txt

some/info/here
some other info here
and here

I apologize for my density but could you explain how this would pertain to my needs?  I don't quite understand how I would make this example work for me.

{EDIT}

It seems it would make some sense to have the bash script read a file with the important information for each file system, then apply those variables during the menu entry and mount command building.

Should I be creating a single file with each domain's information in it as variables inside an array of some sort?

Last edited by schwim (2020-11-22 21:34:30)


Schw.im! A social site with an identity crisis.

Offline

Board footer

Powered by FluxBB