I came across some cases where the shell was not the first child process of xterm. In these cases, this line doesn't work:
Code:
SH_PID=$(echo $(ps --ppid ${WPID} -o pid=) | cut -d' ' -f1)
I fixed that by making a function to pick the correct process from the ps --ppid output. Here's the updated version of the script:
Code:
#!/bin/bash
SHELL=bash
# From child PIDs, gets the one corresponding to the bash shell
function get_shell_pid ()
{
local WPID=$1
local SH_PID=""
PIDS=$(echo $(ps --ppid ${WPID} -o pid=))
for PID in $PIDS
do
CMD=$(echo $(ps --pid $PID) | cut -d' ' -f8)
if [ "$CMD" == "$SHELL" ]; then
SH_PID=$PID
break
fi
done
echo $SH_PID
}
W=$(xdotool getactivewindow)
WPID=$(xdotool getactivewindow getwindowpid)
S1=$(xprop -id ${W} | awk '/WM_CLASS/{print $4}')
if [ "$S1" == '"XTerm"' ]; then
SH_PID=$(get_shell_pid ${WPID})
if [ -n "$SH_PID" ]; then
DIR=$(readlink -e /proc/${SH_PID}/cwd/ | sed 's/ /\\ /g')
xterm -e "cd ${DIR} && /bin/bash"
fi
fi