Showing posts with label cli. Show all posts
Showing posts with label cli. Show all posts

Thursday, April 1, 2010

Echo and Process from cmdline

Here is a quick one for the sys admin tasks. I want to execute a command, and based on the output, not the exit code, execute something else. At the same time, I want to see the output of the original program displayed on the screen. The following does not work because grep swallows the input.

$ cmd | grep 'pattern to find' && doSomething

My current solution is not optimal, me thinks, but at least it lets me see what the original output is. Here is what I have:

$ cmd | tee /dev/stderr | grep -q 'pattern to find' && doSomething

Got any other way to do this?

P.D. I hate blogger's formatting...

Thursday, March 12, 2009

My new cmdline friend

While I still profess my love for xargs, I must admit I've been using something else quite a bit lately. In my search for a way to more easily do lambda style operations from the command line, I was directed to while. while has not only allowed me to use the variable I'm iterating over more easier, it has also allowed me to have multi-statement commands in my loops.

Here is a sample of what I'm now able to easily do:

find -maxdepth 1 -type d | while read x; do echo '>>> Doing ' $x; ( cd "$x"; printf "ID: "; hg id; hg purge --all; ); done

Wednesday, October 8, 2008

Bits from today

More "sed" powers:
sqlcmd somecommand | sed '1,2d; /^$/ d; /^\(.* rows affected\)/ d; s/ *$//'
That'll delete the first two lines, delete empty lines, delete "rows affected" lines, and trim out ending whitespace! And from vim:
:sort u
Sort and keep only unique lines. Beautimus!