c - Calling execve bash on bash scripts can't find arguments -
i have 2 (ubuntu linux) bash scripts take input arguments. need run simultaneously. tried execve
arguments e.g.
char *argv[10] = { "/mnt/hgfs/f/working/script.sh", "file1", "file2", null };
execve(argv[0], argv, null)
but bash script can't seem find arguments @ e.g. $0
, $1
, $2
.
printf "gcc -c ./%s.c -o ./%s.o\n" $1 $1; gcc -c ./$1.c -o ./$1.o -g exit 0;
output gcc -c ./main.c -o ./main.o
, lot of errors /usr/include/libio.h:53:21: error: stdarg.h: no such file or directory
what's missing?
does script start hashbang line? think that's must, like:
#!/bin/bash
for example, see next c program:
#include <stdio.h> #include <unistd.h> char *argv[10] = { "./qq.sh", "file1", null }; int main (void) { int rc = execve (argv[0], argv, null); printf ("rc = %d\n", rc); homecoming 0; }
when compiled , run next qq.sh
file, outputs rc = -1
:
echo $1
when alter file to:
#!/bin/bash echo $1
it outputs:
file1
as expected.
the other thing need watch out using these vmware shared folders, evidenced /mnt/hgfs
. if file created windows-type editor, may have "dos" line endings of carriage-return/line-feed
- may causing problems execution of scripts.
you can check running:
od -xcb /mnt/hgfs/f/working/script.sh
and seeing if \r
characters appear.
for example, if utilize shell script hashbang line in (but appen carriage homecoming line), also rc = -1
output, meaning couldn't find shell.
and, now, based on edits, script has no problem interpreting arguments @ all. fact outputs:
gcc -c ./main.c -o ./main.o
is proof positive of since it's seeing $1
main
.
the problem have compiler is working cannot find strdarg.h
included libio.h
file - has nil whether bash
can see arguments.
my suggestion seek , compile manually command , see if same errors. if so, it's problem you're trying compile rather bash
or exec
issue.
if compile okay, may because of destruction of environment variables in execve
call.
c linux bash
No comments:
Post a Comment