Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying out the fish shell 2.0 to replace bash. I'm trying to convert the following bash function to a fish function, but its giving me hell. I have a several similar functions so if someone could point me in the right direction with an example I would be very grateful

connect() {
    if [ "$2" != "" ]
    then
            ssh -ti ~/.ssh/work/id_rsa user@host$1.domain.com "sudo ssh $2"
    fi
    if [ "$2" == "" ]
    then
            ssh -ti ~/.ssh/work/id_rsa user@host$1.domain.com "sudo -i"
    fi
}

Tried just replacing $1 and $2 with $argv[1] and $argv[2] but it complains about not being able to locate the end of block. I know its because the scripting synxtax is quite different in fish. I tried starting from a simpler function and had troubles with figuring out how to get it to do one thing if 1 variable is passed, and another thing if 2 variables are passed:

function testvarz
    switch "$argv[2]";
            case ""
                    echo "no second variable passed"
            case != ""
                    echo "second variable passed"
    end

end

but that doesn't work, if you pass 2 variable it does nothing, if you pass 1 it complains about

share|improve this question

migrated from serverfault.com Jun 8 '13 at 0:45

This question came from our site for professional system and network administrators.

1 Answer 1

up vote 4 down vote accepted

Here is a working function that's similar to your test function.

 function testvarz
      switch (count $argv)
          case 0
              echo 'zero'
          case 1
              echo "one $argv[1]"
          case 2
              echo "two $argv[1] $argv[2]"
          case '*'
              echo 'else'
      end
  end

You can use a similar structure for your connect function.

By the way, you should similarly use $# in your Bash function instead of trying to rely on $2 being null. You can also use a case statement in Bash.

share|improve this answer
    
Thanks for the example and tips –  MrSilver AG Jun 8 '13 at 2:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.