简体   繁体   中英

How to use in-line expansion with execvp

I am interested in, how can one accomplish in-line linux expansion with execvp in C. I tried doing so with a backslashed apostrophe inside the one that indicates, that the following is a string, but failed. The code, that I run is as it follows:

static const char *datecmd[] = { "xsetroot", "-name", "$(date +'%T')", NULL };
execvp(((char **)arg->v)[0], (char **)arg->v);

In-line expansion is a function of the shell, so you would need to run your command from inside of a shell, eg:

static const char *datecmd[] = { "bash", "-c",
                                 "xsetroot -name $(date +'%T')", NULL };
execvp(((char **)arg->v)[0], (char **)arg->v);

It looks like you are interested in globbing , see glob(7) . Then you could also use wordexp(3) to expand your thing, and later call execvp(3) on its result.

BTW, for the particular expansion of date +%T you should read time(7) and simply use a usual combination of time(2) , localtime(3) , strftime(3) . You don't need to run any date process (and you might avoid any globbing)

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