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
$@
works for me..special() { $@; }
...special echo "foo"
givesfoo
perl -MTime::HiRes=sleep -le 'for(1..100) { print; sleep 0.05; }'
andtar -cvf "backups/test.tar" -P "backups/uncompressed_server_backup_(DO NOT TOUCH!)/server/"
"$@"
, then you will have more success :)