Reputation: 1
Is it even possible to transfer command line arguments with execl from C to bash? cuz with C to C file i was getting exect formatt error. so decided to try C main, and bash son script, everything is running fine, except that i can't figure out how to transfer that argv with execl and than use it with son..
FATHER
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
main( int argc, char *argv[])
{
int pid;
if (argc==4)
{
printf ("Need to enter 3 arguments");
printf ("\tOne Process yet, creating second...\n");
pid = fork();
}
else
{
printf ("Need to give me 3 arguments\n");
exit(EXIT_FAILURE);
}
if(pid==0)
{
printf("\tChild process launched...\n");
execl("./testB", "testB", argv[1], argv[2], argv[3], NULL);
perror("execl dissapointed us");
}
else if(pid>0)
{
printf ("\twaiting for my child to finish...\n");
wait((int *)0);
printf("\t Child finished, time for father...\n");
printf("Main Father\n");
}
else
{
printf("We've got an error, boss\n");
}
printf("Two proccesses?\n");
printf("Number of arguments %d", argc);
}
SON
#!/bin/bash
printf "im da testB\n"
echo "Iveskite norima kieki konvertacijai"
read litai
while [ "$litai" -le 0 ]
do
echo "Iveskite norima kieki konvertacijai"
read litai
done
SUM=$( echo "scale=4;$litai*3.4528" | bc )
printf "Jusu pasirinktas litu kiekis %d atitinka %s eurus\n" "$litai" "$SUM"
printf "asd %s\n" "$argv[1]"
Upvotes: 0
Views: 99
Reputation: 70951
Change this
printf "asd %s\n" "$argv[1]"
to be
printf "asd %s\n" "$1"
to print the 1st argument passed to testB
.
In bash
arguments to the script are referred to by using $n
with n
starting with 1
.
Also: The last parameter passed to execl()
shall be (char*) NULL
.
From man exec
:
The list of arguments must be terminated by a null pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
Upvotes: 1