简体   繁体   English

构建简单的shell脚本,该脚本执行作为参数传递的程序

[英]Building simple shell script which executes program passed as argument

I am building a program which helps in memory debugging of C programs. 我正在构建一个有助于C程序的内存调试的程序。 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. 因为我需要在运行用户程序之前设置环境变量,所以命令var实际上是我将用户程序作为第一个arg传递到的shell脚本的名称。

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? 这对于没有参数的程序很好用,但是如果用户还向其程序提供args会发生什么呢?

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 谢谢

使用"$@" ,它将正确处理所有参数。

Assuming that args to program always start from the 2nd arg, I'd suggest doing it like this: 假设要编程的args 总是从第二个arg开始,我建议这样做:

#!/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. 但是通过这种方式,您可以在实际执行$ PROG之前轻松地进行一些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM