Find the answer to your Linux question:
Results 1 to 9 of 9
hi i am executing the tar command via a C program using the system command Code: #define CLIDUMP "/usr/AmiNas/clidump" memset (command, 0, sizeof(command)); strcpy(command,"tar -cvzf debugdump.tar.gz "); strcat (command,CLIDUMP); system(command); ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141

    suppress warning messages & normal messages on commadn execution


    hi

    i am executing the tar command via a C program using the system command
    Code:
    #define CLIDUMP "/usr/AmiNas/clidump"
    memset (command, 0, sizeof(command));
    strcpy(command,"tar -cvzf debugdump.tar.gz ");
    strcat (command,CLIDUMP);
    system(command);
    When i execute the program i am getting output printed on the screen
    in this manner

    usr/AmiNas/clidump/
    usr/AmiNas/clidump/meminfo.txt
    usr/AmiNas/clidump/bringup.log
    usr/AmiNas/clidump/messages
    usr/AmiNas/clidump/psx.txt
    usr/AmiNas/clidump/volumeoutput.txt
    usr/AmiNas/clidump/procdvm/
    usr/AmiNas/clidump/procdvm/dvm/
    usr/AmiNas/clidump/procdvm/dvm/containers
    usr/AmiNas/clidump/procdvm/dvm/raidstat
    usr/AmiNas/clidump/procdvm/dvm/compacter
    usr/AmiNas/clidump/procdvm/dvm/svt
    usr/AmiNas/clidump/procdvm/dvm/lock
    usr/AmiNas/clidump/procdvm/dvm/dvmstat
    usr/AmiNas/clidump/procdvm/dvm/debug
    usr/AmiNas/clidump/procdvm/dvm/termap
    usr/AmiNas/clidump/procdvm/dvm/dvmprof
    usr/AmiNas/clidump/snapshotoutput.txt
    usr/AmiNas/clidump/df.txt
    usr/AmiNas/clidump/ldoutput.txt
    usr/AmiNas/clidump/loadavg.txt
    usr/AmiNas/clidump/containeroutput.txt
    usr/AmiNas/clidump/lsmod.txt

    I would like to know how to suppress that output. or even redirect it from the output screen.

    One more thing is i tried executing the cp command for a directory
    but got the output printed as

    cp :- termcap : No such file or directory

    how do i suppress warning messages like this also via a C program?

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    ok i managed to suppress the first set of messages and bring it to this level

    [root@ITX0030482c8d82 AmiNas]# cli create dump

    cp: reading `/proc/dvm/termap': No such file or directory
    cp: reading `/proc/dvm/svt': No such file or directory
    Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
    tar: Removing leading `/' from member names

    Success.
    [root@ITX0030482c8d82 AmiNas]#

    I removed the -v option from the tar command.

    Now i need to suppress the rest of the messages ...
    any idea would be appreciated

  3. #3
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    ok i was able to remove the tar message also which was coming by using the -P option.


    -P
    --absolute-names
    Do not strip leading slashes from file names.


    I am kinda trying out the remaining three messages. still open to ideas

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    ok i am able to remove the messages which came from the cp command by redirecting it to /dev/null
    Code:
    cp sourcedir destdir -rf 2> /dev/null
    Now i only have one more line to suppress. that is the output of "ps- x" command is giving that last line

  6. #5
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    ok i used the same logic for the ps -x output by redirecting it to 2> /dev/null

    hope this helps someone

  7. #6
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    A better apporach is to avoid system().

    You can do the same thing with popen() - in which case all of the output goes to the FILE *fp you used to call popen.

    see
    Code:
    man popen

  8. #7
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I was using popen at first for executing all the commands and then writing it to a file.

    But then since i thought opening a pipe and then writing each line into a file till the end of file will take more time and even make the code lengthy i changed to system as i have a about 8 commands to execute and for each i have to write the output to a seperate file.

    why is popen better than system? I read the man page it did not say anything about which is better?

    Could you explain?

  9. #8
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    I used this code for popen

    Code:
    char 	command[256];
    char 	buffer[256];
    FILE*	cmdPtr=NULL;
    FILE*	fp =NULL;
    
    
    //getting the output of df command
    memset (command, 0, sizeof(command));
    
    strcpy(command,"df");
    
    cmdPtr = popen (command, "r");
    if(cmdPtr == NULL )
    {
      errorCode = ERR_FILE_OPEN_ERROR;
      return errorCode;
    }
    
    fp=fopen(DFOUTPUT,"w");
    
    while(fgets(buffer, 255, cmdPtr) != NULL)
    {
       fwrite(buffer, sizeof(char), strlen(buffer), fp);
       memset (buffer, 0, sizeof(buffer));
    }
    
    fclose(fp);
    fp = NULL;
    Is there any way i can reduce this code. Since i am repeating the same code for 8 commands.

    I thought about wrting this in a function and passing the file name and command each time i execute to reduce the length of the code

    but then again i thought about the pipe opening and writing to a file like i told before

    Is there anyway i can improve this code?

  10. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    If you're intent on using system(), you might as well write the program as a shellscript and save yourself the bother of compiling it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •