Find the answer to your Linux question:
Results 1 to 5 of 5
hi all, can someone tell me a command which can be used to run some other command on a few lines from a file or an o/p of some file. ...
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4

    command to run another command on a few lines

    hi all,
    can someone tell me a command which can be used to run some other command on a few lines from a file or an o/p of some file.
    (the kind of role that -exec option does for the find command)
    (i have solved the purpose using a bash loop but would like to know if there exists a command)
    Thanks

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by pareshverma91 View Post
    hi all,
    can someone tell me a command which can be used to run some other command on a few lines from a file or an o/p of some file.
    (the kind of role that -exec option does for the find command)
    (i have solved the purpose using a bash loop but would like to know if there exists a command)
    Thanks
    To run commands from a file:

    Code:
    sh file
    from the output of a command:
    Code:
    command | sh

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    4
    I think i didnt explain my problem clearly.
    what i meant was that suppose a file contains lines as:
    234
    3245

    now i want to kill the processes with above id
    so how do i do it....
    ( i have found a way as "kill -9 `cat filename`" but i remember reading out somewhere that using "`" is not desirable)

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    I should be careful with kill -9 but you can do something like:
    Code:
    awk '{system("kill -9 " $0)}' file

  5. #5
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    I think what you are trying to ask is how to iteratively take input from a file, and use the new data as part of a command for each iteration. To use your example, it can be done something like this (untested):
    Code:
    #
    #  file 'pidfile' contains a list of process IDs to kill
    #
    while read pid; do
        echo "Killing $pid"
       # kill -9 $pid
    done < pidfile
    This script loops, reading a new record from pdifile on each iteration, and uses the value read as part of the command(s) executed within the loop.

    There is nothing generically wrong with using backticks, although your example is a poor way to use them, as it will give an arbitrarily long argument list to the kill command. In this case, I think that is okay, but many commands like their argument list to be a single item, in which case your example will fail. Modern bash versions now use (prefer) the notation, $( conmmand ), to replace the use of backticks (which would otherwise be `command`).

    --- rod.
    Last edited by theNbomr; 07-25-2010 at 07:55 PM.
    Stuff happens. Then stays happened.

Posting Permissions

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