Home Link

Hello, my name is Brett Terpstra, and it’s nice to meet you. Elegant solutions to complex problems. Curious?

Posts Tagged ‘bash’

  • Jan
  • Feb
  • Mar
  • Apr
  • May
  • Jun
  • Jul
  • Aug
  • Sep
  • Oct
  • Nov
  • Dec

Mar 06
2010

Just a quick change to my post on the bash func­tion fk that I’ve been using. A small mod­i­fi­ca­tion has greatly improved its usabil­ity: make the can­cel option always be first in the menu. Just move “Can­cel” before the $(fp $1) bit. It’s a lit­tle odd that I didn’t do that to begin with…


fp () { #find and list processes matching a case-insensitive partial-match string
        ps Ao pid,comm|awk '{match($0,/[^\/]+$/); print substr($0,RSTART,RLENGTH)": "$1}'|grep -i $1|grep -v grep
}

fk () { 
    IFS=$'\n'
    PS3='Kill which process? (1 to cancel): '
    select OPT in "Cancel" $(fp $1); do
        if [ $OPT != "Cancel" ]; then
            kill $(echo $OPT|awk '{print $NF}')
        fi
        break
    done
    unset IFS
}

Nov 17
2009

I do a lot in Ter­mi­nal. Some­times, it’s eas­ier. Some­times it’s faster. Some­times I’d just rather type it out. What­ever the rea­son, I’ve never been able to stand look­ing at a bor­ing shell prompt. Bash is my pri­mary shell, mostly because I’ve never taken the time to learn much else. I’ll get there some­day. For now, here’s my cur­rent Bash shell prompt…

I’m using the PROMPT_COMMAND vari­able to run a few quick func­tions to gen­er­ate the prompt. It doesn’t do any­thing processor-intensive, so I haven’t seen any lag caused by this one (unlike some of my pre­vi­ous exper­i­ments). PROMPT_COMMAND is set to call a func­tion called, appro­pri­ately, prompt_command(). This, in turn, calls a few exter­nal func­tions defined in my .bash_profile. To use it, just stick all of the code below into your .bash_profile, and mod­ify it as you see fit. Be sure to replace any def­i­n­i­tions of PROMPT_COMMAND or PS1.

Con­tinue read­ing “My new favorite Bash prompt…”

Nov 14
2009

This is a func­tion from my OS X .bash_profile. ‘fk’ is short for Find and Kill, and it lets you do a quick search of your run­ning processes for a case-insensitive par­tial match of the first para­me­ter passed to it. It’s use­ful for quickly find­ing a process with­out wor­ry­ing about its cap­i­tal­iza­tion or full spelling, and with­out hav­ing to sift through (or man­u­ally grep) a long ps ax list.

Con­tinue read­ing “fk: a use­ful bash function…”

Entries (RSS) and Comments (RSS).

Switch to Mobile version