Skip to content

Instantly share code, notes, and snippets.

@Dalrik
Created September 7, 2014 11:31
Show Gist options
  • Save Dalrik/20b41ca274a9da47138a to your computer and use it in GitHub Desktop.
Save Dalrik/20b41ca274a9da47138a to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check whether there are child zsh processes to pull cwd from
# Sets $BEST_PID to the PID of the deepest zsh process
#
# Usage: checkchildren <PID>
function checkchildren() {
if [[ $# -eq 1 ]]; then
psdata=(`ps ho pid,comm --ppid=$1`)
if [[ ${#psdata[@]} -ge 2 ]]; then
if [[ "${psdata[1]}" == "zsh" ]]; then
BEST_PID=${psdata[0]}
fi
checkchildren ${psdata[0]}
fi
fi
}
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <PID>"
exit
fi
ps $1 > /dev/null
if [[ $? -ne 0 ]]; then
echo "Process not found"
exit
fi
BEST_PID=$1
checkchildren $1
dir=`pwdx $BEST_PID | sed 's/.*: //'`
cd "$dir"
urxvt &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment