LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 05-10-2017, 08:09 AM   #16
montagdude
Senior Member
 
Registered: Apr 2016
Distribution: Slackware
Posts: 2,011

Original Poster
Rep: Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619Reputation: 1619

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

Last edited by montagdude; 05-13-2017 at 10:18 PM. Reason: Make CMD= line work more reliably
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
xterm - howto lauch an xterm into a specific directory jobano Linux - Software 11 01-30-2023 04:45 AM
[SOLVED] st (simple terminal) - set window title to current directory dluco Linux - General 2 07-01-2014 12:11 PM
Apps open under current window rjs1943 Linux - Newbie 3 01-30-2014 08:44 AM
[SOLVED] perl script tp open an xterm window and run a command. casperdaghost Linux - Newbie 2 07-25-2010 02:49 AM
Window Maker key binding question. jhigz Linux - Newbie 3 12-12-2005 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration