0

I have two alias' (obviously obscured):

alias ki='kinit -kt ~/.keytab alexdodd@box'
alias pt='function _pritunl() {/Applications/Pritunl.app/Contents/Resources/pritunl-client start 0e7e534335bsdfds6eaeb8e023a5ce4 --password XXXX$1; echo attempting pritunl login };_pritunl'

that I naively thought I can chain them together so they run one after another like so:

alias ptki='function _ptki() {
      pt $1
      ki
      if [ "$DEV_STACK_NAME" -ne 1 ]; then
         echo "Starting devstack"
         devstack start
      fi
         }'

However the arg $1 never gets passed through to the alias pt (get a parse error)`, I'm being an idiot I know, but what am I missing here, tried searching but i feel we're so fundamental i'm not sure what i'm search for.

Clearly i never touch bash script =/ Adds to long list of things i need to learn

I would like to, with on alias 'ptki', run three alias one after another, ptki should take one arg and pass it through.

The first takes an arg and passes it to the alias. Second needs to wait 10 seconds after the first. Third runs only if DEV_STACK_NAME exists.

4
  • 2
    It's not what you're asking about, but using shell functions instead of aliases would make this so much easier.
    – Kusalananda
    Commented Feb 7, 2022 at 22:53
  • If you can explain what you're trying to achieve (as distinct from how you're trying to achieve it), we may be able to suggest a workable solution for you Commented Feb 8, 2022 at 0:06
  • clarified what i want to do at the bottom, hope that helps Commented Feb 8, 2022 at 0:27
  • 1
    Not really. You've said how you want to implement something (with aliases). Forget that. The starting point and end goal aren't clear. What do you want to achieve? Commented Feb 8, 2022 at 7:32

1 Answer 1

0

First, you are better off not using any aliases as "shortcuts" inside a function definition, for reasons of readability and maintainability, and also because you are better off not encumbering yourself with aliases defined elsewhere and that may change without you remembering or even knowing that they were originally implemented with a different intent in mind in a function or script.

Second, if you must insist on using aliases inside a script / function, the bash manpage tells you that aliases are not expanded in non-interactive shells (such as scripts). If you want them expanded in there, you need to add shopt -s expand_aliases in the script and source the file where those aliases are defined (generally ~/.bashrc or ~/.bash_aliases) for them to kick in inside the script.
For me sourcing ~/.bashrc from inside a script is far from optimal in any situation, primarily because I don't always know what is in ~/.bashrc on a given host's system.
If you want to be able to use calling-shell aliases from inside a script by default, you will need to either define your BASH_ENV environment variable in your calling bash shell environment, or use #!/usr/bin/bash -i as shebang inside the script. See man bash and look for the section on BASH_ENV and on how to make a script shell interactive.

But (again) this goes against good practices, meaning you or someone else will get in trouble sooner or later because of that spurious use of aliases inside a script. Do use functions instead.

So, not addressing the content of the little code you show, the way you should do this in a script called my_script is basically:

#!/usr/bin/bash

function _pritunl() {
    /Applications/Pritunl.app/Contents/Resources/pritunl-client start \ 
    0e7e534335bsdfds6eaeb8e023a5ce4 --password XXXX"$1"
    echo "attempting pritunl login" 
}

function _ptki() {
    _pritunl "$1"
    kinit -kt ~/.keytab alexdodd@box

    if [ "$DEV_STACK_NAME" -ne 1 ]; then
        echo "Starting devstack"
        devstack start
    fi
}

_ptki "$1"

.. then make the script (my_script) executable and run it, or just run it as:

$ bash my_script first_arg

where first_arg corresponds to $1, the first positional argument inside your script.

There are issues with the script above, the first one on the list being what @roaima points out in OP's comments: this may not be the right way to go about solving your problem. But if it were remember you need to:

  • double-quote your variables, so they're properly expanded by the shell,
  • ensure that all variables are (made) available or appropriately defined in your script (think $DEV_STACK_NAME). For that you probably need to pass variable(s) from the calling environment to your script. Several ways to do this are documented here and on other sites.
  • use printf instead of echo, and learn why it is better.
  • ...
1
  • Thank you, that answer is mucho appreciated. I shall take this on board and go away and do it sensibly I promise. :) Commented Feb 9, 2022 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .