Sponsored Content
Full Discussion: shell commands in c program
Top Forums Programming shell commands in c program Post 86533 by blowtorch on Saturday 15th of October 2005 12:14:04 AM
Old 10-15-2005
Go through the man pages of popen and pclose. These allow you to execute commands and read/write from/to them.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

combining unix commands and awk program

Dear Experts I am trying to find if it is possible to combine unix commands in awk program. For example if it is possible embed rm or ls or any unix command inside the awk program and while it is reading the file besides printing be able to do some unix commands. I am thinking may be just print... (2 Replies)
Discussion started by: Reza Nazarian
2 Replies

2. UNIX for Dummies Questions & Answers

How to use UNIX commands in C program

Hai friend, I am new to linux..i had gone through most of the commands used in unix but i dont know how to use this commands in my C program..U know about this plz send the example program(C prog with unix command)... Thank... (3 Replies)
Discussion started by: sundar.lsr
3 Replies

3. Shell Programming and Scripting

HELP: Need to send commands to Program

Hi, I am an absolute beginner in Unix and I need to send a commands to a program I have created a script that will let me login to the program, however, i cannot get the script to send commands to this program. When the cript logs into the program, the display/ prompt is as follows: 1>... (1 Reply)
Discussion started by: the_m4ch1ne
1 Replies

4. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

5. UNIX for Dummies Questions & Answers

Can anyone help? I have to Write a program in C that recognizes the following commands and translate

I have to Write a program in C that recognizes the following commands and translates them into much simpler ones Commands to recognize shorter command list L cd dir C - dir_length - dir get file_name G - file_name_length - file_name Long commands are read from the standard input and... (1 Reply)
Discussion started by: aintour
1 Replies

6. Homework & Coursework Questions

Can anyone help? I have to Write a program in C that recognizes the following commands and translate

I have to Write a program in C that recognizes the following commands and translates them into much simpler ones Commands to recognize shorter command list L cd dir C - dir_length - dir get file_name G - file_name_length - file_name Long commands are read from the standard input... (1 Reply)
Discussion started by: aintour
1 Replies

7. UNIX for Dummies Questions & Answers

How to run two commands from a exec call in a c program

Hi, I have to run two commands one after another from a c program. How can i do this with exec system calls. i tried giving them as argument to execv but it is not working.please help thanks (3 Replies)
Discussion started by: suryashikha
3 Replies

8. Shell Programming and Scripting

How to enter commands to the command prompt in a program

Hey, So I'm trying to write a program in unix to automate a process for my astrophysics research. Basically I want the program to prompt the user for some information and store the entered string of text as a variable. I know how to do this. This is where I need help: Now lets say I have a... (4 Replies)
Discussion started by: freemoniez
4 Replies

9. Shell Programming and Scripting

Automating execution of commands inside a program

I have a program dnapars I execute the program from command line as following: ./dnapars The program then prompts me some message as a user menu from where I have to select a series of options in the order R U Y R. And then I copy the output file (outfile) in another result file. I wrote the... (3 Replies)
Discussion started by: deeptisjains
3 Replies

10. Shell Programming and Scripting

[Solved] Usage of shell commands inside a C program

Hi I have a program int main(int srgc, char *argv) { for(int i=1; i<50; i++) { system("dd if=/dev/zero of=file$i bs=1024 count=$i"); } return 0; } My doubt is how to use the "$i" value inside C code Please help (2 Replies)
Discussion started by: Priya Amaresh
2 Replies
POPEN(3)						     Linux Programmer's Manual							  POPEN(3)

NAME
popen, pclose - pipe stream to or from a process SYNOPSIS
#include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FILE *stream); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only. The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. The type argument is a pointer to a null-terminated string which must contain either the letter 'r' for reading or the letter 'w' for writing. Since glibc 2.9, this argument can additionally include the letter 'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying file descriptor; see the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful. The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called popen(), unless this is altered by the command itself. Conversely, reading from a "popened" stream reads the command's standard output, and the command's standard input is the same as that of the process that called popen(). Note that output popen() streams are fully buffered by default. The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2). RETURN VALUE
The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The pclose() function returns -1 if wait4(2) returns an error, or some other error is detected. ERRORS
The popen() function does not set errno if memory allocation fails. If the underlying fork(2) or pipe(2) fails, errno is set appropri- ately. If the type argument is invalid, and this condition is detected, errno is set to EINVAL. If pclose() cannot obtain the child status, errno is set to ECHILD. CONFORMING TO
POSIX.1-2001. The 'e' value for type is a Linux extension. BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before popen(). Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. SEE ALSO
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2010-02-03 POPEN(3)
All times are GMT -4. The time now is 01:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy