Bash array multi dimensional shell script for SSHFS mounting on Linux

Hello there folks!

I use the following to mount remote filesystems via a menu entry in jgmenu:

for u in $HOME/Remote/SSHFS/*
  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}"
		printf '%b\n' "mount ${right},sshfs -o workaround=rename $left@$right:/home/$left /home/schwim/Remote/SSHFS/${acct}"


Which, for a while, worked swimmingly but as I gained more remote filesystems to mount, began to fail due to differing remote file paths and changing enviromental variables.

So I’d like to create an array that I loop through to build these menu entries. Each array entry needs to contain:

user: schwim
local dir: /home/schwim/
remote dir: /var/www/client1
user: bob
local dir: /home/schwim/
remote dir: /home/bob/public_html
More sites here

I’m failing at making an array for this, however. I’ve only ever worked with arrays in scripting languages like php and I just can’t figure out how to do it in a bash script.

Any help in making this array and figuring out how to loop through it would be greatly appreciated.

Thanks for your time!

Welcome @Schwim to nixCraft forum.

Instead of bash array try something as follows which is easy too:

Create a text file

Say input.txt seprated by , as IFS (input field separator),schwim,/home/schwim/,/var/www/client1,bob,/home/schwim/,/home/bob/public_html

Bash while loop to read text file line-by-line

We can read those using a simple bash while loop:


while IFS=, read -r domain user ldir rdir
	echo "$domain $user $ldir $rdir"
done <"$db"

Now you can build your logic easily. Let us know if you need any more help.

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki