Menu for managing SSH ZSH sessions

The script is in the original post.

I cant wait… xD

So this is your whole script?

#!/usr/bin/zsh

PS3="Enter session nr.: "

select sshs in a@a b@a b@a b@b "Quit Menu"
do
    case $sshs in
        "a@a")
           ssh a@a -p 224 && break;;
        "b@a")
           ssh b@a -p 224 && break;;
        "a@b")
           ssh a@b -p 225 && break;;
        "b@b")
           ssh b@b -p 225 && break;;
        "Quit Menu")
           break;;
        *)
           echo "Invalid input";;
    esac
done

exit 0

???

If so, then I only have one question.
When you say ā€œit works when I do it manuallyā€ā€¦ THAT LINE… Hardcode that line directly in the cases instead.
Edit
I guess it wasn’t a question, but rather a questioning statement. xD

I mean the current version, with the source lines.

Well, I actually updated the commands to simplify the script and it is now pulling from my .ssh/config like @megavolt’s comment suggested. I liked his approach. Maybe a bit redundant now.

@dmt this is what throws the aforementioned errors:

#!/usr/bin/zsh

source /home/username/.zshenv
source /home/username/.zshrc

PS3="Enter session nr.: "

select sshs in username@pc1 root@pc1 username@pc2 root@pc2 "Quit Menu"
do
    case $sshs in
        "username@pc1")
           ssh username-pc1 && break;;
        "root@pc1")
           ssh root-pc1 && break;;
        " username@pc2")
           ssh username-pc2 && break;;
        "root@pc2")
           ssh root-pc2 && break;;
        "Quit Menu")
           break;;
        *)
           echo "Invalid input";;
    esac
done

It throws this error when I write it with squiggly and the same error with complete path when I write it with a complete path.

~/.zshenv:source:3: no such file or directory: ~/.zshenv
~/.zshrc:source:4: no such file or directory: ~/.zshrc

If this here is true, then I think we are on the right path, but the behavior of ā€œsourceā€ here is not normal, because the files do exist.

.ssh/config is what I use, and if I want to change shell, I do it on the other side.

For what it’s worth.

I dont know what level you are at, but when I started to get really into bash scripting I asked here on the forum about stuff and somebody threw this at me.

I keep coming back reminding myself sometimes, it’s a REALLY good read.
You have indentation of 4, there should be 2. xD
(strange name but read it!)

For general purpose stuff, I agree, but this is something that I do for myself. I am going to spend a lot of time in terminals and I plan on installing zsh in all of them and making things look the way I want automatically. I don’t want to work with terminals that are dull and not intuitive enough for me. This is not a script for Google or public use. I am just trying to personalize my linux experience, so it doesn’t suck. In this case, I think it’s alright if I use ZSH and install it everywhere automatically.

Also, I think IT moves forward: slowly, but it does. As far as I understand macOS has ZSH installed by default in all machines? And it is the default shell? I don’t like Apple, but I think sometimes they have good ideas. Why shouldn’t all machines ship with ZSH in them if it makes our lives easier. Fish shell makes things even more intuitive, but again, I think IT moves slowly. Backwards compatibility is important, but sometimes, some people want to push.

You CLEARLY took that link the wrong way, its a guideline, EXTREMELY WELL written with more or less descritptions to everything you need to know to make very complex scripts. Functions are in there. pretty sure source is too.
They even use bash as the example script for everything.

If you want to automate zsh on the newly installed machines, write a script for that with that knowledge was what I was going for.

No ill intent, but I seriously think you would gain a lot by reading that.

With that, I beg you both good night.

No offense taken. I read a few paragraphs. I guess it is too late for a read like that :laughing: thanks for the tip though :heart_decoration: Nighty!

Those errors are from when you did source "~/.zshrc" etc.

% cat ~/.zsh_test
basename $0  # prints name of this file

% cat ~/.local/bin/testy
#!/usr/bin/zsh
source "~/.zsh_test"

% testy
~/.zsh_test:source:2: no such file or directory: ~/.zsh_test

The errors don’t match your code, and I’m pretty sure your username isn’t username.

It’s difficult when we’re not sure what the code is, and when the errors don’t match. Maybe there’s no real difference, but we can’t tell and the missing info is often important info especially when things don’t make sense.

All I can say right now is that it should work:

% cat ~/.local/bin/testy
#!/usr/bin/zsh
source ~/.zsh_test

% testy
.zsh_test

As, I said:

it’s full path, when the script contains the full path, and it is with the squiggly when the script contains the squiggly.

/home/username/.zshrc:9: command not found: z4h
/home/username/.zshrc:64: command not found: z4h

And no, my username is not username, but I am 100% sure that is not the problem.

What’s interesting though, now the error seems to be only for .zshrc, but both files are there. I just checked one more time. And what it isn’t finding now is ā€œz4hā€. The export function still doesn’t work.

I just changed to the squiggly again, and now the error is consistent. It’s always this one, no matter whether it is the squiggly or the full path.

Looks like it’s sourcing ~/.zshrc, but either ~/.zshenv hasn’t been sourced or we’re missing something.

which z4h
z4h () {
        if (( ${+functions[-z4h-cmd-${1-}]} ))
        then
                -z4h-cmd-"$1" "${@:2}"
        else
                -z4h-cmd-help >&2
                return 1
        fi
}

This is the function for z4h, maybe I can set it as a variable? Or maybe there is another file, which defines it and we haven’t sourced, but should also source?

Assuming ~/.zshenv is being sourced, then yes.

It’s getting late, so I’m off for the night.

1 Like

So, I scanned my system for Z4H files and these three jumped at me:

  • ~/.cache/zsh4humans/v5/z4h.zsh
  • ~/.cache/zsh4humans/v5/zsh4humans/main.zsh
  • ~/.cache/zsh4humans/v5/zsh4humans/z4h.zsh

I tried permutations with them, the second one always breaks the script. The first one and the third one threw errors like this:

~/.zshrc:9: command not found: z4h
~/.zshrc:64: command not found: z4h
z4h: missing required parameter: Z4H

It must be set in ~/.zshenv:

  : "${Z4H:=${XDG_CACHE_HOME:-$HOME/.cache}/zsh4humans/v5}"

Note: The leading colon (:) is necessary.

z4h: command failed: . "$Z4H"/z4h.zsh
z4h: enabling recovery mode

See error messages above to identify the culprit.

Edit Zsh configuration:

  /usr/bin/micro ~/.zshrc

Retry Zsh initialization:

  exec zsh

Give up and start over:

  sh -c "$(curl -fsSL https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"

Where they are also missing the Z4H command, which is set as a function for me. I just don’t know which file contains it, so I can source it.

Since you refuse to share what you have installed there is not much we can do.

But in making a script calling something on your system, you should not have to source anything, it does not make sense.

If you on the other hand are changing the stuff you have installed, we need to know about that.

Is this what you have installed? (ie, how did you install the stuff?)

I think I need to do more shell coding, I’m forgetting everything.

How about we just put it in a function instead?

Put this in ~/.zshrc:

myssh() {

PS3="Enter session nr.: "

select sshs in username@pc1 root@pc1 username@pc2 root@pc2 "Quit Menu"
do
    case $sshs in
        "username@pc1")
           ssh username-pc1 && break;;
        "root@pc1")
           ssh root-pc1 && break;;
        " username@pc2")
           ssh username-pc2 && break;;
        "root@pc2")
           ssh root-pc2 && break;;
        "Quit Menu")
           break;;
        *)
           echo "Invalid input";;
    esac
done
}

Then open a new terminal and run myssh.

EDIT:

Clarified the previously vague instructions.

@bedna, this is what I installed

And as I said the changes I carried out are just powerline settings and sourcing the file that I shared with you. They are all part of the thread already.

I tried playing around with it, but it didn’t do anything.

Like drawing blood from a stone. :grin:

What does ā€œdidn’t do anythingā€ mean?

Code? Output?

% cat ~/.local/bin/testy
myssh() {

PS3="Enter session nr.: "

select sshs in username@pc1 root@pc1 username@pc2 root@pc2 "Quit Menu"
do
    case $sshs in
        "username@pc1")
           echo ssh username-pc1 && break;;
        "root@pc1")
           echo ssh root-pc1 && break;;
        "username@pc2")
           echo ssh username-pc2 && break;;
        "root@pc2")
           echo ssh root-pc2 && break;;
        "Quit Menu")
           break;;
        *)
           echo "Invalid input";;
    esac
done
}

% . ~/.local/bin/testy

% myssh                                                                                                                                                                                                                  
1) username@pc1   2) root@pc1       3) username@pc2   4) root@pc2       5) Quit Menu      
Enter session nr.: 1
ssh username-pc1

So you used this to install?

if command -v curl >/dev/null 2>&1; then
  sh -c "$(curl -fsSL https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"
else
  sh -c "$(wget -O- https://raw.githubusercontent.com/romkatv/zsh4humans/v5/install)"
fi

That means it’s the V5 branch that I linked to.
Now we at least know what version you are using and we can read the files.
Ie GitHub - romkatv/zsh4humans at v5

One thing I still wonder though, why is it working when you type it in console and not when issuing the exact same commands in a shell scripts. That to me is a mystery.

1 Like

That includes functions, I forgot that for the first couple of posts.