Sponsored Content
Top Forums Programming How to Run a Linux Command and Redirect its output to a socket in C Post 302256048 by fpmurphy on Friday 7th of November 2008 06:55:15 PM
Old 11-07-2008
No, there is no way of directly outputting to the client socket. You need to modify your code to loop until no more data is available i.e. fgets() returns NULL. Using ls as an example, here is some pseudo code which should point you in the right direction:

Code:
FILE *fp;
char path[PATH_MAX];
...
fp = popen("ls", "r");
while (fgets(path, PATH_MAX, fp) != NULL) {
    # fprintf(stderr, "%s", path);
    write(sockfd, path, strlen(path));   
}

pclose(fp);   
...

You could also look at the source code for rexecd for inspiration.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

redirect command output to variable

Hi, I am looking for a way to redirect the result from a command into a variable. This is the scenario. Using the find command I will be getting multiple records/lines back. Here is the command I am using: find /”path”/ -name nohup.out -print This now is giving me the paths and file... (1 Reply)
Discussion started by: hugow
1 Replies

2. Shell Programming and Scripting

redirect output of dos2unix command

hi I want to suppress the output of dos2unix command in my shell script. I'm using follwing command in my script dos2unix somefile >/dev/null But it's still showing output while executing the script.Please help me to sort this out Thanks (4 Replies)
Discussion started by: nrbhole
4 Replies

3. Filesystems, Disks and Memory

in Oracle Enterprise Linux not able to redirect pvscan output

hi, In Oracle Enterprise Linux I'm not able to redirect output of commands pvscan and vgscan into a file. File is coming blank Please suggest something Thanx. (1 Reply)
Discussion started by: discover
1 Replies

4. Linux

In Oracle Enterprise Linux, not able redirect pvscan output

Hi, I'm not able to redirect output of ovscan and vgscan commands to a file in Oracle Enterprise Linux. Please suggest something. Thanks Mayank (1 Reply)
Discussion started by: discover
1 Replies

5. UNIX and Linux Applications

How to redirect grep command output to same file

Hi Everyone, Can anyone please tell me, how can I redirect the grep command output to same file. I am trying with below command but my original file contains no data after executing the command. $grep pattern file1 > file1 Kind Regards, Eswar (5 Replies)
Discussion started by: picheswa
5 Replies

6. Shell Programming and Scripting

Receiving 'ambiguous redirect' when trying to run command against multiple files

I came across the command string on https://www.unix.com/shell-programming-scripting/141885-awk-removing-data-before-after-pattern.html which was what I was looking for to be able to remove data before a certain pattern. However, outputting the result to a file seems to work on an individual basis... (4 Replies)
Discussion started by: HLee1981
4 Replies

7. Solaris

Script redirect command output failed, why?

Hi, I put a for loop in a script to eject backup tapes from the robot. The command echo' output goes to the log file without problem, but command vmchange's output does not go to the log file although it's working fine. It still displays on the screen. I've tried '2>&1 1>$log', but nothing changed.... (5 Replies)
Discussion started by: aixlover
5 Replies

8. AIX

Not able to redirect output of command

Hi All,. We are using AIX as the OS to host the Oracle ERP. We have a command FNDLOAD which is used to load setups. When this command is run, it outputs names of log files and any errors to the screen. I am trying to redirect this output to a file because we have large number of these... (4 Replies)
Discussion started by: mansmaan
4 Replies

9. Shell Programming and Scripting

Redirect output of command line to for loop

I would like to redirect output of command line in for loop as $line. Output should be processed as line but instead it throw whole output. Could somebody help me on how to redirect output of command line and process it line by line without sending output to any file. below is my code ... (1 Reply)
Discussion started by: tapia
1 Replies

10. Shell Programming and Scripting

How to redirect the output of a command inside ftp block?

hi, i am using ftp to get files from remote server. inside the ftp i want to us ls -ltr command and send the output of it to a file. ftp -n remote_server <<_FTP quote USER username quote PASS password prompt noprompt pwd ls -ltr get s1.txt bye _FTP i... (4 Replies)
Discussion started by: Little
4 Replies
DIRNAME(3P)						     POSIX Programmer's Manual						       DIRNAME(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
dirname - report the parent directory name of a file pathname SYNOPSIS
#include <libgen.h> char *dirname(char *path); DESCRIPTION
The dirname() function shall take a pointer to a character string that contains a pathname, and return a pointer to a string that is a pathname of the parent directory of that file. Trailing '/' characters in the path are not counted as part of the path. If path does not contain a '/', then dirname() shall return a pointer to the string "." . If path is a null pointer or points to an empty string, dirname() shall return a pointer to the string "." . The dirname() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe. RETURN VALUE
The dirname() function shall return a pointer to a string that is the parent directory of path. If path is a null pointer or points to an empty string, a pointer to a string "." is returned. The dirname() function may modify the string pointed to by path, and may return a pointer to static storage that may then be overwritten by subsequent calls to dirname(). ERRORS
No errors are defined. The following sections are informative. EXAMPLES
The following code fragment reads a pathname, changes the current working directory to the parent directory, and opens the file. char path[PATH_MAX], *pathcopy; int fd; fgets(path, PATH_MAX, stdin); pathcopy = strdup(path); chdir(dirname(pathcopy)); fd = open(basename(path), O_RDONLY); Sample Input and Output Strings for dirname() In the following table, the input string is the value pointed to by path, and the output string is the return value of the dirname() func- tion. Input String Output String "/usr/lib" "/usr" "/usr/" "/" "usr" "." "/" "/" "." "." ".." "." Changing the Current Directory to the Parent Directory The following program fragment reads a pathname, changes the current working directory to the parent directory, and opens the file. #include <unistd.h> #include <limits.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <libgen.h> ... char path[PATH_MAX], *pathcopy; int fd; ... fgets(path, PATH_MAX, stdin); pathcopy = strdup(path); chdir(dirname(pathcopy)); fd = open(basename(path), O_RDONLY); APPLICATION USAGE
The dirname() and basename() functions together yield a complete pathname. The expression dirname(path) obtains the pathname of the direc- tory where basename(path) is found. Since the meaning of the leading "//" is implementation-defined, dirname(" //foo) may return either "//" or '/' (but nothing else). RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
basename(), the Base Definitions volume of IEEE Std 1003.1-2001, <libgen.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 DIRNAME(3P)
All times are GMT -4. The time now is 06:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy