C - execvp not working with valid input [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 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.

Related

pass a linux command line into two executables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a C program which reads its input from the command line, I would like to feed the executable with the output of ls | wc -m command, as I need to call two instances of the executable (./a.out1 , ./a.out2) using that same input and make them running in parallel (pipes ?).
Thank you for your help in advance!
You say you want to use a pipe, so first of all you need to adapt your program to read the input from stdin instead of argc and argv. Input passed via a pipe is not added to the command line argument list.
To pipe stdout of a process to multiple other processes, you can use tee and process substitutions:
ls | wc -m | tee >(./a.out1) >(./a.out2) >/dev/null
However, the reason why you require it be piped (as opposed to passed as an argument) isn't clear to me, so storing the output in a variable as suggested in the comments would work just as well for the example you present.

fgetc reads wrong characters from file [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 3 years ago.
Improve this question
I use fgetc to read characters from a file and it reads something strange.
I have this in the txt file:
But when i look at the file info it shows this:
and fgetc actually reads the { as first character of the file instead of S.
I tried to change the file extension to rtf,txt, no extension but same result.
Any ideas how to fix this?
Looks like you wrote the file in some sort of editor that stores its metadata at the beginning of the file. Try recreating the file with a more basic editor that does just text editing. Or from the command line echo "my file contents" > myfilename.txt.

Ignore the '<' and '>' in the comand line in c [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 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'

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