23

I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters.

Meaning somethings like this:

special_execute()
{
    # Some code

    # Here's the point where the command gets executed
    $@

    # More code
}

special_execute echo "abc"

I already tried I $@, "$@", $*, "$*" how could I do that?

BrainStone
  • 3,534
  • 12
  • 32
  • 53
  • `$@` works for me.. `special() { $@; }` ... `special echo "foo"` gives `foo` – Drav Sloan Aug 28 '13 at 00:54
  • It did not work for me with: `perl -MTime::HiRes=sleep -le 'for(1..100) { print; sleep 0.05; }'` and `tar -cvf "backups/test.tar" -P "backups/uncompressed_server_backup_(DO NOT TOUCH!)/server/"` – BrainStone Aug 28 '13 at 00:55
  • 3
    use quotes around `"$@"`, then you will have more success :) – Drav Sloan Aug 28 '13 at 00:59
  • Ok. Now it works. it seems like I messed up the code arround the actual call. – BrainStone Aug 28 '13 at 01:04
  • 1
    no problem, we all have those moments :) – Drav Sloan Aug 28 '13 at 01:06
  • Would you say it's simple to detect if there's a flag? Meaning calling it like `special_execute -123 echo "ABC"` Would still execute the command and safe the flag in a value (without the `-`). if this flag is not supplied the variable has a default value. How easy can you implement it`? – BrainStone Aug 28 '13 at 01:09
  • @BrainStone - you'd probably want to use the shift command once you've looked at arg `$1`, which would contain the flag. – slm Aug 28 '13 at 01:11
  • @BrainStone - see my updates to the answer, they include a basic parsing example. – slm Aug 28 '13 at 01:18

1 Answers1

17

I think it's just a quoting issue when you're passing the arguments into the function.

Try calling it like so:

$ special_execute "echo 'abc'"
'abc'

If you don't want the single quotes around abc then change the quoting like this:

$ special_execute "echo abc"
abc

Debugging

You can wrap the internals of the function so that it echoes out with more verbosity.

$ function special_execute() { set -x; "$@"; set +x; }

Then when you run commands through the function, special_execute you can see what's going on.

ps example:

$ special_execute ps -eaf
+ ps -eaf
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Aug21 ?        00:00:01 /sbin/init
root         2     0  0 Aug21 ?        00:00:00 [kthreadd]
...

perl example:

$ special_execute perl -MTime::HiRes=sleep -le 'for(1..10) { print; sleep 0.05; }'
+ perl -MTime::HiRes=sleep -le 'for(1..10) { print; sleep 0.05; }'
1
2
3
4
5
6
7
8
9
10
+ set +x

Parsing argument $1

You could do something like this to parse any arguments passed in as $1.

$ function special_execute() { 
    [ "$1" -eq "-123" ] && echo "flagY" || echo "flagN"; 
    shift; 
    set -x; "$@"; set +x; 
  }

Example

with debugging enabled:

$ special_execute -123 perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
flagY
+ perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
1
2
3
4
5
+ set +x

with debugging off - -123:

$ special_execute -123 perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
flagY
1
2
3
4
5

with debugging off - -456:

$ special_execute -456 perl -MTime::HiRes=sleep -le 'for(1..5) { print; sleep 0.05; }'
flagN
1
2
3
4
5
slm
  • 363,520
  • 117
  • 767
  • 871