help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] pass command to script


From: Christof Warlich
Subject: [Help-bash] pass command to script
Date: Thu, 25 Apr 2019 17:47:06 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

Hi,

I want my script to execute any command being passed as command-line
arguments. As a non-trivial example, consider the following command:

$ var="Hello cruel" bash -c 'echo "${var} world!"; sleep 2; echo
"Goodbye Worlrld!"'
Hello cruel world!
Goodbye World!

Thus, I would like that

$ ./myscript var="Hello cruel" bash -c 'echo "${var} world!"; sleep 2;
echo "Goodbye World!"'
initial work
Hello cruel world!
Goodbye World!
final work

does some initial work, runs the command beingg passed and then again
does some final work.

After a few unsuccessful attempts, I was close to giving up, as it did
not seem to be possible to e.g. get the quoting and the handling of
environment variables right in any generic way. But then I stumbled over
bash's "time" builtin command, which does exactly what I want:

$ time var="Hello cruel" bash -c 'echo "${var} world!"; sleep 2; echo
"Goodbye World!"'
Hello cruel world!
Goodbye World!

real    0m2,007s
user    0m0,001s
sys    0m0,006s

Is there any chance to achive something similar with a script?

Thanks for any ideas,

Chris




reply via email to

[Prev in Thread] Current Thread [Next in Thread]