最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

cygwin - sending shell script arguments to C wrapper - Stack Overflow

programmeradmin1浏览0评论

I am trying to wrap a shell script so that it may be called from a Windows program inside Cygwin. Following up on a previous question, I thought it was enough to pass the arguments of the script to the wrapper as follows:

#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char *argv[])
  {

     /* WARNING: Only use an absolute path to the script to execute,
      *          a malicious user might fool the binary and execute
      *          arbitary commands if not.
      * */

     system ("~/bin/script.sh \"$*\"");

     return 0;
   }

The script runs, but somehow cannot pass the arguments to the wrapper (I guess?). Coming from a DOS/Win background, this shell scripting thing is somewhat mysterious to me. What is wrong with this approach?

I am trying to wrap a shell script so that it may be called from a Windows program inside Cygwin. Following up on a previous question, I thought it was enough to pass the arguments of the script to the wrapper as follows:

#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char *argv[])
  {

     /* WARNING: Only use an absolute path to the script to execute,
      *          a malicious user might fool the binary and execute
      *          arbitary commands if not.
      * */

     system ("~/bin/script.sh \"$*\"");

     return 0;
   }

The script runs, but somehow cannot pass the arguments to the wrapper (I guess?). Coming from a DOS/Win background, this shell scripting thing is somewhat mysterious to me. What is wrong with this approach?

Share Improve this question edited Feb 2 at 19:52 jarnowic asked Jan 31 at 0:20 jarnowicjarnowic 2651 silver badge19 bronze badges 4
  • Are you hoping the "$*" will expand to your parameters? That doesn't work in a C program, only in shell scripts. In C you'll have to loop over the argv array that was passed to main, and add each parameter from there (separated by spaces) to the string you pass to system(). – Chad Commented Jan 31 at 0:41
  • indeed; i figured: if you pass control to the system, the shell would do the right thing... – jarnowic Commented Jan 31 at 0:54
  • now I see I was wrong: the system() function simply passes the argument to the OS, missing some capabilites of the shell, if any... – jarnowic Commented Jan 31 at 23:44
  • @Chad, you were right; if you turn your comment into an answer, with a little bit of background, I'll accept it... – jarnowic Commented Feb 1 at 14:44
Add a comment  | 

1 Answer 1

Reset to default 1

Firstly, "$*" doesn't work in C programs. You'll need to iterate through argv, and append all the arguments to a string.

Secondly, "$*" isn't even that great in shell, you should usually use "$@" because it preserves arguments separation.

Thirdly, what system() does might depend what compiler and library you are using. If it's a windows compiler, I'm not sure its libraries would know how to execute either "~" tilde, or the shell script. I know you said you're using cygwin, but you didn't say if you were using the cywin compiler or a windows compiler.

Fourthly, after all, why would you want to to write a C program to wrap cygwin? I don't know what a "windows process" is... but windows can execute c:/cygwin/bin/bash script.sh arg arg arg without wrapping it. If I really needed a windowsy thing to call cygwin, I'd probably write a .bat script calling bash.

发布评论

评论列表(0)

  1. 暂无评论