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.
...
- 07-23-2010 #1Just 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
- 07-25-2010 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 07-25-2010 #3Just 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)
- 07-25-2010 #4Linux 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
- 07-25-2010 #5
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):
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.Code:# # file 'pidfile' contains a list of process IDs to kill # while read pid; do echo "Killing $pid" # kill -9 $pid done < pidfile
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.


Reply With Quote
