1

I'm trying to alias my php command by executing it with an argument depending on if a file with my remote host IP is existant, without any additional argument otherwise.

I got it working with this version

#!/bin/bash
alias php="[ -f path/to/hostip ] && php -dxdebug.remote_host=\"$( < path/to/hostip )\" || php

Two issues

  1. it's too long (luxury problem)
  2. when calling php path/to/any/script my further arguments aren't passed to the php call

I tried a function, which solves issue #2

#!/bin/bash
function phpx {
    if [ -f path/to/hostip ]; then
        php -dxdebug.remote_host="$( < path/to/hostip )" "$@"
    else
        php "$@"
    fi
}

But I still have issues

  1. it's much more longer (luxury problem)
  2. I can't use php as the alias for the function name

What I'm looking for is an alias for php like

alias php='php ( condition | true > pass file content as argument | false > pass nothing )'

And I want to be able to pass my further arguments to the alias.

2
  • 1
    use command php instead of pure php in your script. Dec 12, 2017 at 12:47
  • Thx. I didn't knew about it.
    – codekandis
    Dec 12, 2017 at 12:56

3 Answers 3

0

help command:

command: command [-pVv] command [arg ...]
    Execute a simple command or display information about commands.

So, run command php "$@" inside the function to not have it call the function again. Or /usr/bin/php, but then you need to know the correct path.

You could still use the [ condition ] && php args.. || php other args trick within the function, but note that it will run the latter php command also if the first php command fails, so the if .. then .. else is more correct. Of course you could write that as a one-liner too, but assuming you're saving the function to your .bashrc or such, the length shouldn't matter much .

1
  • +1 for mentioning the fallback if the first php call fails. Haven't thought about it. So the function is the only solution while if else is working correctly. Thx for the command example. Tried to dig into it and didn't got it in the first try.
    – codekandis
    Dec 12, 2017 at 12:55
0

(This is really a comment, but needs formatting. Made it community wiki)

Note that (1)

A && B || C

is fundamentally different from (2)

if A; then B; else C; fi

in this situation: when A succeeds but B fails. When that happens, C will run in (1) but not in (2).

4
  • I got this with the answere of @ilkkachu. But nice you made examples. May you can be more specific with these examples - especially the logical order of (1)? I would edit your answere if you don't want to.
    – codekandis
    Dec 12, 2017 at 17:04
  • From the Lists section of the bash manual: "Of these list operators, && and || have equal precedence", so A && B || C is strictly left-to-right: ((A) && B) || C. Adding the parentheses aids understanding B's effect on running C. Dec 12, 2017 at 17:18
  • I know, but just didn't realized C will be invoked in two cases. Just wanted to mention, that you could enhance your answere to clarify all possibilities.
    – codekandis
    Dec 12, 2017 at 18:00
  • Community wikis are meant to gather the community's knowledge, so edit away. Dec 12, 2017 at 18:08
0

You could make your alias look like this:

alias php='_php_opts=(); [[ -f /path/to/hostip ]] && _php_opts+=( -dxdebug.remote_host="$( < path/to/hostip )" ); php "${_php_opts[@]}"`

but why would you when you could write something readable that does not pollute the global variable namespace.

php() {
    local _php_opts=()
    [[ -f /path/to/hostip ]] && _php_opts+=( -dxdebug.remote_host="$( < path/to/hostip )" )
    command php "${_php_opts[@]}" "$@"
}
2
  • This is a nice one, too. I didn't see local until now. Should be in the alias, too.
    – codekandis
    Dec 12, 2017 at 17:03
  • 1
    local is strictly for functions. Dec 12, 2017 at 17:08

You must log in to answer this question.

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