简体   繁体   中英

Command line arguments with spaces to a C program through shell-wrapper script

what does it take to make my program accept command-line arguments with spaces?

Yet-another EDIT : I have just recognized that the program is started from a shell-script that sets up the environment for the execution of the program. As there are some external libraries, LD_LIBRARY_PATH is set to the current working directory:

#!/bin/sh

ARCH=`uname -m`
export LD_LIBRARY_PATH=".:lib/magic/linux-${ARCH}"

./data_sniffer.bin $*

exit $?

The issue is definitely related to $* . Is there any solution to correctly forward the command-line parameters?

Here is a code-snippet from main() :

if (stat(argv[1], &lStat) != 0)
{   
    fprintf(stderr, "Cannot stat(2) given file: %s. \n", argv[1]);
    return EXIT_FAILURE;
}   

I am starting my program with the following parameters:

./data_sniffer /mnt/pod/movies/some\ test\ movie\ file.avi

The resulting error message looks like this: Cannot stat(2) given file: /mnt/pod/movies/some.

Anyone an idea what's wrong here? I think that I am not the first one with this problem (though, I could not find a related question here).

Replace the use of ./data_sniffer.bin $* with ./data_sniffer.bin "$@" in your wrapper script and the arguments should be forwarded in a correct manner.

More regarding the difference between $* , $@ and "$@" can be found here .


the author of this question changed it completely and came forward with more information regarding the matter, I will let everything already written stand but please remember that this was written before his/her last edit..


Regarding argv ..

It doesn't require much from you as a developer, I'm tempted (and it's much more truthful) to say that it requires nothing of you.

Arguments passed to the application is handled by the shell executing the binary, doing this below should definitely work the way you want it to (even though I find it odd that you are claiming that the current shell doesn't handle '\\ ' correctely):

./data_sniffer '/mnt/pod/movies/some test movie file.avi'
./data_sniffer /mnt/pod/movies/some\ test\ movie\ file.avi

# there should be no difference between the two

My recommendation(s)

  • Have you tried doing printf ("argv[1]: %s\\n", argv[1]); in the beginning of main to validate the contents of it?

  • Are you sure that you are invoking the correct binary with the correct command-line arguments?

Sadly the only reasonable thing to write is that you are doing something wrong. We are however unable to answer what without further information regarding the issue.

I find it very hard to believe that there is a bug in your shell , even though that is of course possible - I doubt it.


Parsing the command-line into argv isn't something that you as a developer should worry about, there are no enforced functionality that you have to implement for the binary itself to handle spaces in it's argument(s).

In my CentOS 5.3, I made a test.

#include <stdio.h>
int main(int argc, char** argv)
{
        printf("argc=%d\n", argc);
        int i = 0;
        for(i = 0; i<argc; i++)
        {   
                printf("argv[%d]=%s\n", i, argv[i]);
        }   
        return 0;
}

And then run it with different parameters:

[root@dw examples]# ./a.out a b c
argc=4
argv[0]=./a.out
argv[1]=a
argv[2]=b
argv[3]=c
[root@dw examples]# ./a.out 'a b c'
argc=2
argv[0]=./a.out
argv[1]=a b c
[root@dw examples]# ./a.out a\ b\ c
argc=2
argv[0]=./a.out
argv[1]=a b c
[root@dw examples]# ./a.out "a b c"
argc=2
argv[0]=./a.out
argv[1]=a b c

So looks like (1) 'abc' ; (2) a\\ b\\ c; (3) "abc" all are working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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