Get unexpected argc when use redirection - c

Here is the code.
#include<stdio.h>
int main(int argc,char* argv[])
{
fprintf(stderr,"arg count:%d\n",argc);
return 0;
}
When type the following cmd:
./a.out x 3 >1.txt //case1
I got argc 3. (in this case, a.out x and 3)
But when type the following cmd:
./a.out x 3>1.txt //case2
I got argc 2. It seems 3>1 was ignored.
I expected the argc is 3 like case1.
(Clarification: There is a space between 3 and > in case1)
Why I got 2nd case? And how to solve it?

First of all you have to remember that all redirection is handled by the shell itself, it's not passed on to the program.
The command
./a.out x 3 >1.txt
have three arguments to a.out: The command itself, x and 3.
With
./a.out x 3>1.txt
then 3>1 is all part of the redirection handled by the shell. It will redirect descriptor 3 to the file 1.txt.

Related

Passing file contents to a c executable doesn't seem to work

I have this c executable called testFile file containing this code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
printf("Number of arguments : %d\n Arguments : ", argc);
for(int i = 0; i<argc-1;i++){
printf("%s\t", argv[i]);
}
}
and along with it a file called test1 containing just the number 1 (echo 1 > test1)
When I call this line on the command line (zsh) :
./test < test1
the output I get is this :
Number of arguments : 1
Arguments : ./testFile
Shouldn't this show 2 arguments ? Along with the character 1 ? I want to find a way to make this 1 appear in the arguments list, is it possible ? Or is it just the way my shell handles arguments passed like that ? (I find it weird as cat < test1 prints 1)
You're conflating standard input with command arguments.
main's argc and argv are used for passing command line arguments.
here, for example, the shell invokes echo with 1 command line argument (the 1 character), and with its standard output attached to a newly opened and truncated file test.
echo 1 > test1
here, the shell running test with 0 arguments and its standard input attached to a newly opened test1 file.
./test < test1
If you want to turn the contents of ./test1 into command line parameters for test, you can do it with xargs.
xargs test < test1
unrelated to your question:
for(int i = 0; i<argc+1;i++){
The condition for that should be i<argc. Like all arrays in C and just about every other language, the minimum valid index of argv is 0 and the maximum valid index is 1 less than its length. As commenters pointed out, argv[argc] is required to be NULL, so technically, argc is one less than the length of argv.
If you want the contents of test1 to be available in argv, you can do:
./test $(cat test1)
Note that this is very different than:
./test "$(cat test1)"
which is also different from:
./test '$(cat test1)'. # Does not do at all what you want!
The first option will present each "word" of the file as a distinct argument, while the second will present the contents of the file as a single argument. The single quotes version doesn't look inside the file at all, and is merely included here to give you something to experiment with.
argv[0] is always the name of the executable itself. With your way of invoking the command, you did not pass any command at all. You would have to invoke it from zsh as ./testFile test1 if you want your program to see the name of the data file, or ./testFile $(<test1) if you want it to see the content of the data file.

Provide input parameters to C code from Linux shell

The concept of my code is like:
#include <stdio.h>
int main(int argc, char *argv[])
{
int num;
FILE *fp;
getint("num",&num); /* This line is pseudo-code. The first argument is key for argument, the second is the variable storing the input value */
fp = inputfile("input"); /* This line is pseudo-code. The argument is key for argument, fp stores the return file pointer */
...
...
exit(0);
}
Usually, after compiling the code and generating the executable main, in the command line we write this to run the code:
./main num=1 input="data.bin"
However, if there's too many arguments, type in the command line each time we run the code is not convenient. So I'm thinking about writing the arguments and run in Linux shell. At first I wrote this:
#! /bin/sh
num = 1
input="data.bin"
./main $(num) $(input)
But error returns:
bash: adj: command not found
bash: input: command not found
bash: adj: command not found
bash: input: command not found
Can anybody help to see and fix it.
There are three main problems with your code:
You can't use spaces around the = when assigning values
You have to use ${var} and not $(var) when expanding values.
The way your code is written, you are passing the string 1 instead of your required string num=1 as the parameter.
Use an array instead:
#!/bin/bash
parameters=(
num=1
input="data.bin"
)
./main "${parameters[#]}"
num=1 is here just an array element string with an equals sign in it, and is not related to shell variable assignments.

run linux command using execlp with more than one argument as string in c

I am trying to run ls using system calls in C with more than one argument, for example -l -a. The arguments and their number is changing depending on the user input. The input is concatenated "-l" + "-a" == "-l -a". The code I'm using is:
execlp("ls","ls",arguments,NULL) //arguments = "-l -a"
The user input is from Terminal:
-l
-a
if you want to executes more than one argument , then you should use execvp() instead of execlp.
#include<stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
execvp(argv[1],argv+1);// argv+1 means whatever arguments after argv[1] it will take & executes it
return 0;
}
for e.g your input like that
xyz#xyz-PC:~$ ./a.out ps -el
I hope it helps.

Read an integer from stdin (in C)

I want to write a C program that takes as input an integer, and outputs its square. Here is what I have tried.
However,
./a.out < 3 outputs 32768, and
./a.out < 4 also outputs 32768.
Where am I wrong? Thanks.
#include <stdio.h>
int main(){
int myInt;
scanf("%d", &myInt);
printf("%d\n",myInt*myInt);
}
It looks like what you're trying to do is
echo 4 | ./a.out
the syntax for < is
program < input_file
whereas | is
command_with_output | program
./a.out < 4
This tries to read the file named 4 and use it's content as input to a.out You can do it whichever way, but understand the < operator isn't for inputting the character you type quite literally.
One way to do this would be:
echo "4" > 4
./a.out < 4
I just run your program and its works great.
If you want the program receive an integer input you should use argc , argv as folowed and not use scanf.
*The code for argc argv: *
#include <stdio.h>
#include <stdlib.h>
int main(int argc , char** argv)
{
int myInt;
myInt = atoi(argv[1]);
printf("%d\n",myInt*myInt);
}
atoi - convert char* to integer.
If you want to run the program and then insert an integer, you did it right!
you can read about atoi
To run this program you should comile and run from terminal:
gcc a.c -o a
./a 3
and you will receive:
9
On the right hand side of "<", there should be a file containing the input.
try this thing:
$ echo "3" > foo
$ ./a.out < foo
Read this for more information (Specially section 5.1.2.2):
http://www.tldp.org/LDP/intro-linux/html/chap_05.html

How to use command line arguments in c (OS: Windows)?

I'm unable to make a program that asks the user to input three arguments on the command line:
1) an operator (+, -, *, /);
2) an integer (n);
3) another integer (m).
The program should thus act as a basic calculator producing the output in this format: .
e.g.
operator='+'
n=5
m=6
output: 5+6
= 11
If you want to take the argument from the command line while the user executes the program you can use the argv vector to fetch the values
int main(int argc, char** argv){
}
So that if you execute your program as follows,
./prog + 1 2
argv will contain the follwing,
argv[0] = 'prog',
argv[1] = '+',
argv[2] = '1',
argv[3] = '2',
So that you can fetch each value from argv and implement your logic.
Read this tutorial for better understanding.
If you are interested in giving arguments while executing the file, i.e. ./executable_name arg1 arg2 arg3 then use
int main(int argc, char *argv[])
to get argument from command line. argc gives the count of argument(s) passed including the executable name and argv is the argument array preserving the order of the input.
You can then compile and run code in command prompt:
cl filename.c
executablename arg1 arg2 arg3

Resources