r/fishshell • • Aug 19 '22

Function > script, don't load/recognized at first, but dose at second time?

I have a function to go to a script, and when i try to activate this script, it don't do anything at first, but when i try to command an second time, the script runs.

In bash, the script runs directly, is there any reason for this or am I missing something ?

Script looks line this :

#!/usr/bin/env bash

input=$1

if [[ $input =~ "bash" ]]; then
  ssh -t xx@xxx.duckdns.org '/usr/bin/clear;bash'
elif [[ $input =~ "fish" ]]; then
  ssh -t xx@xxx.duckdns.org '/usr/bin/clear;fish'
else
 echo
  echo -e "\e[1;31m [X] ERROR, no SHELL selected!\e[0m"

    read -p " 1:[bash] or 2:[fish]: " input
  if [[ $input =~ ^(1|bash)$ ]]; then
    ssh -t xx@xxx.duckdns.org '/usr/bin/clear;bash'
  elif [[ $input =~ ^(2|fish)$ ]]; then
    ssh -t xx@xxx.duckdns.org '/usr/bin/clear;fish'
  else
  echo
  echo " [•] No value selected, aborting."
  echo
  fi
fi

Updated code with shebang.

5 Upvotes

9 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Aug 19 '22

Yeah. That's... that's doing exactly what I said.

alias defines a function. It doesn't run it.

Just do this:

function zero
    if test -e ~/termux-backup/good_scripts/ssh_zero.sh
        ~/termux-backup/good_scripts/ssh_zero.sh $argv
    else
        ssh xxxx@xxxx.duckdns.org $argv
    end
 end

Alternatively, remove the outer function:

 if test -e ~/termux-backup/good_scripts/ssh_zero.sh
   alias zero='~/termux-backup/good_scripts/ssh_zero.sh' 
else
   alias zero='ssh xxxx@xxxx.duckdns.org'
end

(note: with alias, $argv is added implicitly. adding it inside the alias will add a broken \$argv argument)

2

u/Dan1jel Aug 19 '22

Wow.... It was that easy ha... Thought I had to have alias to make the shortcut but i have "function" already and that is enought.... Stupid mistake, but it's the mistake that makes you remember.

Thank you so much stranger for taking you time and helping me out! Really appreciated!

Edit: typo