get a bash's cwd - linux

If I open up 2 terminals in Ubuntu, via ps u I can see 2 bash's pid.
My problem is from one terminal, how can I know the bash's cwd in the other terminal?

You can check
/proc/<pid>/cwd

The pwdx command prints the current working directory of each given process ID.
Example from Solaris (believe Ubuntu also has the command):-
$ cd /tmp
$ pwdx $$
22281: /tmp

Related

WSL, Running linux commands with "wsl --exec <cmd>" or "wsl -- <cmd>"

wsl -h shows the following:
--exec, -e <CommandLine> Execute the specified command without using the default Linux shell.
-- Pass the remaining command line as is.
What does "without using the default Linux shell" mean (i.e. what else is it going to use, if not the default shell!?)?.
Additionally, by way of an example, I now have three possible ways to run Linux ls from my PowerShell prompt (i.e. this will not be Get-ChildItem aliased to ls, but instead a Linux command via WSL):
PS C:\> wsl -e ls # Alternatively, wsl --exec ls
PS C:\> wsl -- ls
PS C:\> wsl ls
But all outputs appear to be the same. How would you explain the differences between these three ways of running a WSL Linux command from a PowerShell prompt?
I think it means wsl runs the command directly, instead of spawning a shell process to run the command.
For example, if I run :
wsl -e sleep 10
From another terminal, I have :
root 1482 1 0 11:32 tty3 00:00:00 /init
ubuntu 1483 1482 0 11:32 tty3 00:00:00 sleep 10
We can see /init is the parent of sleep 10, without a shell in between.
A cool trick is using this to set the X11 $DISPLAY variable, letting you use windows terminal to get remote shells using WSLG.
# in microsoft terminal or powershell use this command line
wsl.exe -- ssh -a -X -Y $hostname
then on the remote system
# DISPLAY will show something like localhost:10.0 on the remote system
echo $DISPLAY
# use a program like xeyes to test
xeyes

strange adb behaviour if script is piped into bash

I stumpled upon a strange behaviour that I can not explain. I tried to narrow down the problem. I have the following test testscript.sh script:
echo before
adb shell ls
echo after
If I run the script with bash -x testscript.sh, everything works as expected and I get the following output:
+ echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor
+ echo before
before
But if I run the script as piped script with cat testscript.sh | bash -sx, I get the following output:
+ echo before
before
+ adb shell ls
acct
bin
bugreports
...
sdcard
storage
sys
system
ueventd.rc
vendor
The last echo after is not executed, and I can not figure out why. The script is running on an Ubuntu server 18.04. The adb is the one from the official Ubuntu package.
$ adb --version
Android Debug Bridge version 1.0.39
Version 1:8.1.0+r23-5~18.04
Installed as /usr/lib/android-sdk/platform-tools/adb
$ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
Please could someone enlight me, what is happening here.
When you run a script with bash scriptname, standard input of all the commands it runs is still connected to the terminal. So adb will read its standard input from the terminal.
When you redirect the input of bash, this redirection is inherited by adb. Unless you use the -n option to adb shell, it will read additional input from standard input and send it to the remote system as possible input for the command you run (it doesn't know that ls doesn't read standard input).
Change it to
adb shell -n ls

linux - command line to get version of terminal

I use a terminal which I don't know the name. By which command can I find the name and version of that?
(I use android operating system)
echo $TERM tells you kind of terminal you are using, eg. xterm
Use the ps command with no arguments to get the processes running under the current shell.
Example:
% ps
PID TTY TIME CMD
1917 pts/0 00:00:00 zsh
13659 pts/0 00:00:00 ps

getting the pid of a process in zsh

I am coding on a Red Hat Machine and I want to get the process id of a process in the interactive mode as well as the in a script.
In bash 'pidof' works but not in zsh.
What would be the equivalent of pidof in zsh ?
Thank you in advance.
You may wish to which pidof in bash, to determine the location of pidof and then try running zsh with the absolute path.
If the above works, you just have a $PATH issue (as I have never seen pidof as a bash builtin.)
If that doesn't work try the following:
This is probably not a simple as you want, but it works for me:
pgrep -U $USER some_program
Where 'some_program' is the name you would normally supply pidof.
On a RHEL (Red Hat Enterprise Linux) machine pidof is located at /sbin/pidof. Just add /sbin to your path.

ps command output on AIX, HPUX and Solaris

I am writing a portable shell script to get system process information, I need process id, command, pwdx (linux). On linux I am able to get this information as follows.. but it fails on all other unix flavours.
$ ps -awwwwwww -u <userid> -o pid,cmd|grep -i <filter_term> | egrep -v grep
$ pwdx <pid>
what I should use on AIX, HPUX and Solaris to get the similar information, or there any cross platform command
On Solaris I have tried /usr/ucb/ps but that support formatted output and lsof for pwdx equivalent but that also doesn't show what I need
On Solaris I have tried /usr/ucb/ps but that support formatted output:
What is wrong with formatted output ?
and lsof for pwdx equivalent but that also doesn't show what I need.
That doesn't make sense. pwdx is a Solaris native command and was even originally implemented on that OS.
Linux != Unix. And in the same hand, the commands are not always going to be the same, for instance GNU ps is not like Solaris ps or HP-UX ps etc. In some cases the Vendor Unix flavors offer a "compatibility binary" like those stashed in /usr/ucb on solaris. But ultimately you need to look at the man page for each version and review the output format options.
Edit. That is for in general all commands. Including grep, egrep etc.
To show the full command name, use this
ps -eo comm
This will show the command that was run. (ps is from /usr/bin on my Solaris system 5.11)

Resources