command line * linux [duplicate] - c

This question already has answers here:
Stop shell wildcard character expansion?
(4 answers)
Closed 6 years ago.
i'm trying to write code in C,which implements a simple calculator.
the input should come from the command line, so for example i if i run
./calculator 5 * 2
the result should be 10
the problem is that when i write * it shows all the files in the current directory and the program doesnt behave well.
there is anyway to overcome this problem?
i tried to find here or in other sites solutions,without success.
i need that * will Be interpreted as a char and not as a linux command.
thanks.

In linux shell, the * has special meaning. It is meant for globbing unless it is quoted like below
./calculator 5 '*' 2
You may also escape the asterisk to strip the special meaning from it
./calculator 5 \* 2

Related

How can I display a mouse's walking process on linux terminal using c? [duplicate]

This question already has answers here:
How can I print a string to the console at specific coordinates in C++?
(7 answers)
Closed 5 years ago.
Now, I can get a direction txt file from my code:
down
down
right
right
up
.
.
.
I want to ask how can I display the walking process on linux terminal.
For instance, I want to use a dot representing a mouse which can execute the direction above.
nCurses is the best solution. You can het help with the built-in manual:
man -s 3 ncurses
A simpler way to do so is to use ANSI CSI escape sequences:
printf("\x1B[A"); // Up
printf("\x1B[B"); // Down
printf("\x1B[C"); // Left
printf("\x1B[D"); // Right
To move up and print a dot:
printf("\x1B[A.");

Looking for an example which shows the difference between # and * in bash array [duplicate]

This question already has answers here:
Accessing bash command line args $# vs $*
(5 answers)
Closed 6 years ago.
x=('hello world' "HELLO")
Both ${#x[*]} and ${#x[#]} print the same output.
I understand the difference between $# and $* but I am interested to see the difference without command line arguments.
Always use # expansion unless you have reason to use *. # was added to work around a problem.
The two don't ALWAYS expand the same. The troubles involving* start with spaces and other shell metacharacters (quotes in particular, but $ and more as well).
The * leaves the metacharacters open for the shell to process them again, which is usually bad if you went out of your way to get them into the array. The # protects them by expanding each array element as if it was a separately quoted value, leaving all metacharacters intact.

Unexpected result from argv [duplicate]

I was working on an example in the K&R C book where it asks you to essentially build an RPN calculator that takes input through command line arguments. My solution essentially iterates through the given arguments and spits out the answer, but I noticed something:
If I were to give the multiplication character (an asterisk) '*' without single quotes, gcc assumes that to be a wildcard input, so my input of
$./rpn 5 10 *
gives me an output of
read 5
read 10
read rpn
read rpn.c
= 0
Wrapping the asterisk with single quotes remedies the issue
$./rpn 5 10 '*'
read 5
read 10
read *
= 50
My question is would there be a way to sanitize input so that my program does not require the asterisk to be wrapped in single quotes, or is this behavior caused by something more fundamental (e.g. Linux/POSIX/UNIX binary execution and argument handling)?
The shell is expanding the glob before executing the program. You quote the glob not because of GCC, but because of the shell. If you don't want this behavior then use a shell that does not honor globs.
Give input as
$./rpn "5 10 *"
All argument in "" and in program you will get all argument under argv[1] then parse that string by space separation.
By this way you do need to handle any wildcard/special character at special way.

How to capture the output of a bash script from a C program? [duplicate]

This question already has answers here:
How can I run an external program from C and parse its output?
(8 answers)
Closed 7 years ago.
I want to save the output from a bash script which is invoked from a C program to a variable declared in the C program. I searched and tried successfully calling a script using system, and I tried this, but it didn't work:
char* a;
system("a=`ls`");
printf("%s",a);
Use popen() system call. You can pass the cmd as the parameter. You will get the command output as text when the function returns. Hope this helps.

output redirection produces empty file [duplicate]

This question already has answers here:
Problem redirecting a C program output in bash
(5 answers)
Closed 9 years ago.
So this is probably a stupid question, but I can't see what I'm doing wrong.
I am running a program that produces output when called like ./ar. The output looks like:
-0.00781 0.02344 0.98828
-0.01172 0.02734 0.98828
-0.01562 0.02344 0.98047
-0.00781 0.02344 1.00000
-0.00391 0.02344 0.98438
A new line of output is written every second.
When I call the code like this ./ar > log and kill the program using ctrl-c after a few seconds, the file log is empty.
I am running this code on an embedded system. The system has a writeable partition which is the partition that I am running in, and I have write access as I am logged in as root.
The reason is the lazy writing concept of UNIX system.
Are you sure you are looking at standard output in you call ./ar? It might be standard error.
So, try ./ar >log 2>err to have 2 files, one for stdout and one for stderr.
Or use ./ar 2>&1 >log to get one file for both streams.

Resources