[lug] Regex from shell
Tkil
tkil at scrye.com
Fri Nov 19 11:46:08 MST 2004
>>>>> "Budyanto" == Budyanto Himawan <budyanto_himawan at yahoo.com> writes:
Budyanto> Is there a way to specify a shell command mixed with regular
Budyanto> expression?
First minor twiddle: the shell wildcard system is usually called
"globbing"; while it took some ideas and parts of notation from
typical regular expression systems, it is not regular expressions.
Something I wrote up on this a few years back:
http://groups.google.com/groups?selm=glnqtgszo.fsf%40scrye.com&output=gplain
Budyanto> for example I want to kill all processes with pids 1110 to
Budyanto> 1119
Budyanto> I want to do something like "kill 111[0-9]".
Budyanto> I tired a few different combo of the above and none seems to
Budyanto> work. I know there must be something like that cause I used
Budyanto> to know it but I just can't seem to recall.
This fails because the shell does globbing (at least with "*", "?",
and bracket expressions as above) only against filenames in the
current working directory.
To clarify: the shell does the globbing, not the command (on unix; on
dos, the command is responsible for interpreting it's arguments!).
Generally, that globbing is done by searching for matches from a
particular set of strings. By default, that set of strings is the
list of filenames in the current working directory. You are asking
the shell to search over the set of strings comprising the active
processes on your system -- which it might very well be able to do,
but I don't know how to ask it to do so. :)
There is another form of globbing that actually generates values, the
braces version. You could do:
kill 111{0,1,2,3,4,5,6,7,8,9}
For sequences like that, consider using 'seq' to generate the needed
numbers. As I mentioned in a different reply:
kill $( seq 1110 1119 )
Finally, if these are all the same process, and the only instances of
that process on your system, take a look at 'killall'.
A few other cheap tricks... Since Linux (in most distributions, at
least) has an entry in /proc for every process-id that is currently
active, you actually can use globbing on it:
kill $( cd /proc; ls 111? )
If you don't have the /proc filesystem, you can use true regular
expressions (with 'grep', 'awk', or 'perl') to grovel through the
output of 'ps':
kill $( ps -A -o pid | grep '^111.$' )
t.
More information about the LUG
mailing list