开发者

Building simple shell script which executes program passed as argument

I am building a program which helps in memory debugging of C programs. I call

execlp("gnome-terminal","gnome-terminal","-e",command,(char*)0);

to open a new terminal window where the program to be debugged runs. I do this to not have my debugging info intermixed with the users program output. Because I need to set up an environmental variable before running the users program, command var is actually the name of the shell script where I pass the users program as the first arg.

Here is my script:

#!/bin/bash

export LD_PRELOAD="./mylib.so"
$1

This works fine for programs with no arguments but what happens if the开发者_运维技巧 user also supplies args with his program?

For example I wish to call my script like that :

myScript.sh usersProgram arg1 arg2 etc

How can I correctly run the users program inside the script and pass all the arguments to it?

Thank you


Use "$@", which will handle all arguments properly.


Assuming that args to program always start from the 2nd arg, I'd suggest doing it like this:

#!/bin/bash

PROG=$1
shift
$PROG "$@"

Practically, just specifying "$@" instead of the three lines above will also work. But this way, you can easily do some manipulation based on $PROG before actually executing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜