Execute command supplied by function parameters

27,777

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
Share:
27,777

Related videos on Youtube

priyanka -
Author by

priyanka -

Updated on September 18, 2022

Comments

  • priyanka -
    priyanka - over 1 year

    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?

    • Admin
      Admin over 10 years
      $@ works for me.. special() { $@; } ... special echo "foo" gives foo
    • Admin
      Admin over 10 years
      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/"
    • Admin
      Admin over 10 years
      use quotes around "$@", then you will have more success :)
    • Admin
      Admin over 10 years
      Ok. Now it works. it seems like I messed up the code arround the actual call.
    • Admin
      Admin over 10 years
      no problem, we all have those moments :)
    • Admin
      Admin over 10 years
      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`?
    • Admin
      Admin over 10 years
      @BrainStone - you'd probably want to use the shift command once you've looked at arg $1, which would contain the flag.
    • Admin
      Admin over 10 years
      @BrainStone - see my updates to the answer, they include a basic parsing example.
  • Elie G.
    Elie G. almost 5 years
    Why do I get an error saying 'command not found' when using aliases?
  • slm
    slm almost 5 years
    @DrunkenPoney - no such cmd, it's alias.