Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about:
So I'm using another function int execute(char ** args) to do the actual work. Its return type is int because I wan to know whether the command exits successfully. But I'm not sure here whether the parent process can get the return value from the child since they're two different processes.
Or should I decide whether to execute the second command in the child process, by forking another process to run it? Thanks a lot.
Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about:
So I'm using another function int execute(char ** args) to do the actual work. Its return type is int because I wan to know whether the command exits successfully. But I'm not sure here whether the parent process can get the return value from the child since they're two different processes.
Or should I decide whether to execute the second command in the child process, by forking another process to run it? Thanks a lot.
If this isn't homework, please tell us what book you're using as a study guide.
This is a very strange mix of pseudo-code and real code. I can't tell by reading it which parts you think are complete and which parts are placeholders for future work. It is, however, clear that this code will not compile.
The first thing you need to do is be sure you #include all of the headers needed for the standard functions you're using.
Then, concerning your question about getting the exit status of a child:
The code:
is strange. Please look at the execvp() (or exec) man page again. There is no return from a successful call to execvp().
Then look at the waitpid() man page. You should see that waitpid(), as you are using it, saves the exit status of the child into the variable status. The way you have your code written:
does not set a return value for your execute() function if waitpid() succeeds. You probably want to change the line above to something more like:
and change:
to something like:
Hello Don, thanks so much for your reply. I'm using Advanced Programming in the Unix Environment and Modern Operating Systems as my reference.
As for the question, I just want the parent to get the command execution result(successful or not) from the child, because I want to do &&(conditional execution). If execvp successfully executes in the child, how does the parent know it because it does not return any value. Thanks.
The first and second command in an && chain aren't parent/child as much as siblings. The parent controlling them gets the value, and can tell whether to launch the next one.
Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
2. Relevant commands, code, scripts, algorithms:
#! /bin/ksh
v="ORG_ID"
... (2 Replies)
Hi All,
Do you have any sample script,
- auto get file from SFTP remote server and delete file in remove server after downloaded.
- only download specify filename
- auto upload file from local to SFTP remote server and delete local folder file after uploaded
- only upload specify filename
... (3 Replies)
How to use MAN to find information about read() and write() function ?
The command "man read" show some rubbish, for example "man open" show great information about function I need. (2 Replies)
Hi, I have text file abc.txt. In this file, I have the following data.
Input:
Mr Smith & Mrs Smith
Mr Smith &apos Mrs Smith
Mr Smith & Mrs Smith
Mr Smith& Mrs Smith
Mr Smith &Mrs Smith
Output:
Mr Smith & Mrs Smith
Mr Smith &apos Mrs Smith
Mr Smith & Mrs Smith
Mr Smith&... (4 Replies)
Hello All
I have a xml file with many sets of records
like this
<mytag>mydata</mytag>
<tag2>data&</tag2>
also same file can be like this
<mytag>mydata</mytag>
<tag2>data&</tag2>
<tag3>data2&data3</tag3>
Now i can grep & and replace with & for whole file but it will replace all... (4 Replies)
Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP.
I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Hello to all,
I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
I am going to take up a position in Data & Network Security.
I would need to write network shell scripts doing the following task:
Going to around 2000 servers and findout which groups has access to each servers and which ids are there in each group that has access.
I need to implement... (1 Reply)
Hi,
If this is the array that is being returned to me:
How would I get the values for each of the 3 records?
This works for 1 Record:
foreach $item (@results)
{
($id, $id2, $name, $date, $email) = split(/\|/, $item, 5);
print "$name<br>";
} (2 Replies)
Hello all,
Can someone explain to me the advantage between using subshell over a function call in scripts? To me these are the same. Am I wrong to think this? (4 Replies)