Ignore the '<' and '>' in the comand line in c [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I made a program which takes numbers from the command line and adds them, the program is supposed to print an error if something else than a number is written, but when it reads > or < it creates text files with the results or rewrites on an already existing text file, if it doesn't exist it just stops without even running the code, is there a way to stop this from happening and read it just like another array?
Here is an example of the error
$ ./a +24 < 5
bash: 5: no such file or directory

That's not your program - that's the shell file I/O redirection. If you want your program to see the < or >, escape them appropriately:
./a +24 \<5

As ths others have said, it is not your program acting up, but the command line shell uses < and > as input/output redirection operators. You could escape them with backslashes.
But rather than forcing the users to escape each < and > (and possibly some other special characters like $ and the parentheses), you can quote the whole command line:
./a '1 < 24'
The whole command now is in argv[1]. You still have to parse it. As a bonus, there is no need to insert annoying spaces between the tokens anymore:
./a '1<24'

Related

Bash: different behaviour reading from file interactive/cron [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a script that writes about 20 numbers line by line to a file during processing.
When the script starts again, it reads from this file with this code
declare -a sedum
i=0
while read -r line
do
sedum[$i]=$line
i=$(( $i + 1 ))
done < $f_sday
f_sday contains the filename. When I call the script from comand line it always works fine and reads the complete content of the file.
But when the script is called in a cronjob it reads only two or three values
I know that from cron it might not be the same environment but I can't see any environmental dependency here.
I tried mapfile at first, but that read only two of the twenty values.
Any idea what I am missing here?
Stupid me.
I did not control the working path (cron starts in $HOME), so the script was working on the wrong file.
Thanks for the set -x hint. That led me on the right path!

C - execvp not working with valid input [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a program that takes in lines of commands from a file and tokenises them into char arrays:
Eg: the file contains:
ls -l -a -i
and then in my program, I end up with this:
char *cmd[10] = {"ls", "-l", "-a", "-i", NULL, NULL, NULL, NULL, NULL, NULL}
and I'm calling
execvp(cmd[0], cmd) within a program using fork().
But I'm getting a runtime error:
ls: invalid option -- '
'
Try 'ls --help' for more information.
from the command line.
I have tested this with a separate program that uses a hardcoded char *[] and it works fine.
Edit:
There is a newline character after the -i. I'll get rid of that. Thanks!
ls: invalid option -- '
'
The positioning of the apostrophes is suspicious. It seems as if the newline character somehow makes it into the array cmd, or perhaps into the last option. Maybe the string is not really "-i", but actually "-i\n". Check your parsing code.

How to test a C program with multiple inputs? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have Linux Ubuntu, and I want to test my program, for which someone gave me .txt file of multiple inputs. Now I want to run this program with inputs written in the .txt file. Theres a lot of inputs so I dont want to input them by hand. Is there some command in Linux Terminal to run a C with inputs written in a file?
thank you for your answers
I think you are suffering from the all too common misunderstanding that "standard input" == "a keyboard". Stop thinking that. If you've already written a program that reads from stdin, all you need to do is associate your text file with stdin. In the shell, you do that with a redirection operator:
./a.out < input.txt
If you have multiple inputs, you can easily invoke your program on each individually:
for file in *.txt; do
echo "Running on input: $file"
./a.out < "$file"
done
or you can run your program once on all the inputs:
cat *.txt | ./a.out
There are many, many ways to do what you want, and a lot of flexibility to do different things. You'll probably want to compare the output of your program with the expected output and then you're on your way to writing a full-fledged test suite. For example:
if ! ./a.out < input.txt | cmp expected-output -; then
echo "TEST FAILED" >&2
exit 1
fi

shell script “${array[#]}” expansion: first and last entry have extra characters [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a shell script that uses an array. The script cycle through the entries of the array but for some reason the first and last entry has a problem.
The array:
Queue_Names=( CLQueue DLQ ExpiryQueue )
The for Loop:
for i in “${Queue_Names[#]}”
do
#do stuff
done
I can see in the console and shows that for the first entry it shows: �CLQueue.
The last entry shows: ExpiryQueue�
I'm guessing these are markers to know the start and end of the array. Unfortunately it is interfering with the functionality of the script. I use these Queue names to search for something and it fails to find it because of the added character. How do I get rid of them or is there a code change I do to avoid the problem?
“${Queue_Names[#]}” is not "${Queue_Names[#]}", because “” is not "".
"Smart quotes" aren't recognized as quotes at all in bash; thus, the effect is the same as if the expansion had been unquoted -- string-splitting and glob-expansion on array contents -- with the literal "quotes" grafted around the start and end characters.
You need to use real quotes -- "" -- not opening/closing "smart quotes" created by some word processing software or corporate email tools.

Unix cat command with output redirection metacharacter >, create empty files [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am using following code to create a new file cat15 using cat command in UNIX
# cat > cat15
this command adds a new file cat15 in root directory and whatever I type after this command is being stored into the file created. But I am not able to exit from this editor.
In other word, I am not getting Shell prompt symbol #
The cat command reads from STDIN if you don't specify a filename. It continues to do this until it receives an EOF or is killed. You can send an EOF and get your terminal back by typing <ctrl>+d.
What people generally do is to either use
touch filename
or
echo -n > filename
to create an empty file. As Charles correctly notes below, "echo -n" is not always a good idea (though you can usually count on it under "popular" Linux distros); I'd strongly suggest just using touch.
If you just want to create an empty file, regardless of whether one existed or not, you can just use ">" like this:
> cat15
It will clobber anything that already exists by that name.

Resources