1
I need to pass a file with extension . sh . I have tried it as follows and the value that is read from the file is wrong: . /program . /file.sh
How do I get this argument through? Do I need to add something to make? Thank you
1
I need to pass a file with extension . sh . I have tried it as follows and the value that is read from the file is wrong: . /program . /file.sh
How do I get this argument through? Do I need to add something to make? Thank you
4
You can use the arguments argc
and argv[]
of function main
.
The parameters argc
and argv
give the programmer access to the command line with which the program was called.
The argc
(argument Count) is an integer and has the number of arguments with which the function main()
was called on the command line.
The argv
(argument values) is an array of strings. Each string of this array is one of the command line parameters. It is to know how many elements we have in argv
that we have argc
.
See an example of a program that takes the path of a parameter file. h whichever:
#include <stdio.h>
int main(int argc, char * argv[])
{
char *caminho_arquivo;
if (argc == 2) /*Quantidade de parâmetros.*/
caminho_arquivo = argv[1];
printf("\nCaminho: %s\n\n", caminho_arquivo);
return 0;
}
Input, the program runs with two parameters, they are, executable name and file path. h: ./exemplo1 /home/user\ joao/arquivo.h
Exit: /home/user joao/arquivo.h
If you are using linux and if the path of your file. h contains whitespace, you should format them using the \
for example: /home/user joao/arquivo.h
should look like this /home/user\ joao/arquivo.h
.
0
Although your question has become a little vague (due to lack of code example), here is a possible solution.
Programs in C language have as argument for their main method main the amount of arguments received and an array with these arguments.
Follow a code example where you can access the arguments received from the command line:
#include <stdio.h>
int main ( int argc, char *argv[] )
{
printf( argv[1] );
}
Note: The zero-index argument is always the name of the program itself in C.
Browser other questions tagged c shell-script
You are not signed in. Login or sign up in order to post.
Ola bkira, what you want is to pass parameters to the command line executable?
– gato
The syntax is incorrect, try this: . /program < file.sh
– Celso Marigo Jr