How to execute a bash script using "system ()" when there are spaces in the file path?

I created a simple bash script called "myscript.h". I gave it the .h extension for reasons that I will not disclose here. This bash script lives in "/ var / ftp / something with spaces".

From the terminal, I can enter "/ var / ftp / something with spaces / myscript.h", and the script works fine.

However, from my C program, I print

system("/var/ftp/something with spaces/myscript.h")

and he complains that "/ var / ftp / something" was not found. I changed my system call to the following slashes:

system("/var/ftp/something\ with\ spaces/myscript.h")

However, he still complains that "/ var / ftp / something" was not found. Assuming I can't change the directory names, how can I get around this?

Thank!

+4
4

script, (3) ( , fork (2), execve (2), waitpid (2)... system (3)), script system (3).

. Linux.

Linux (3) /bin/sh -c. . sh (1p). POSIX . . , (, , ) , system (3) (, asprintf (3) snprintf (3) ). , C () .

, (, a; rm -rf $HOME &; "directory" name, system (3) weird
"/var/ftp/a; rm -rf $HOME &;/myscript.h")

fork (2), execve (2) (, exec (3)), waitpid (2)... . , /bin/sh, ( ).

Unix; , bash ( , POSIX sh), globbing. . glob (7)

+7

, . . , system:

system("\"/var/ftp/something with spaces/myscript.h\"")
0

This should work with gcc version 5.4.0

system("\'\'/var/ftp/something\\ with\\ spaces/myscript.h\'\'");
0
source

Just enter the file name in single quotes

system("rm '/var/ftp/something with spaces/myscript.h'")
-1
source

Source: https://habr.com/ru/post/1679383/


All Articles