How to print a Linux environment variable from the Powershell REPL console? - linux

How do I print the value of foo to the console?
nicholas#mordor:~/chimp$
nicholas#mordor:~/chimp$ VARNAME="foo"
nicholas#mordor:~/chimp$ export foo="bar"
nicholas#mordor:~/chimp$
nicholas#mordor:~/chimp$ echo $foo
bar
nicholas#mordor:~/chimp$
nicholas#mordor:~/chimp$ pwsh
PowerShell 7.1.0
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-Variable HOME -valueOnly
/home/nicholas
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-Variable foo -valueOnly
Get-Variable: Cannot find a variable with the name 'foo'.
PS /home/nicholas/chimp>
see also:
https://askubuntu.com/q/58814/847449
https://opensource.com/article/19/9/environment-variables-powershell

Certainly foo is listed below:
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-ChildItem env:
Name Value
---- -----
_ /snap/bin/pwsh
CLR_ICU_VERSION_OVERRIDE 60.2
COLORTERM truecolor
DBUS_SESSION_BUS_ADDRESS unix:path=/run/user/1000/bus
DESKTOP_SESSION ubuntu
DISPLAY :1
foo bar
GDMSESSION ubuntu
GJS_DEBUG_OUTPUT stderr
GJS_DEBUG_TOPICS JS ERROR;JS LOG
GNOME_DESKTOP_SESSION_ID this-is-deprecated
GNOME_SHELL_SESSION_MODE ubuntu
GNOME_TERMINAL_SCREEN /org/gnome/Terminal/screen/e541ff8a_1fb3_477b_a685_074b316b8bcc
GNOME_TERMINAL_SERVICE :1.78
GPG_AGENT_INFO /run/user/1000/gnupg/S.gpg-agent:0:1
GRADLE_HOME /home/nicholas/.sdkman/candidates/gradle/current
GTK_MODULES gail:atk-bridge
HOME /home/nicholas
IM_CONFIG_PHASE 1
INVOCATION_ID 8c2492a5c0464f27a979cad923c16937
JAVA_HOME /home/nicholas/.sdkman/candidates/java/current
JOURNAL_STREAM 9:260051
LANG en_US.utf8
LANGUAGE en_CA:en
LC_ALL en_US.UTF-8
LC_CTYPE en_US.UTF-8
LESSCLOSE /usr/bin/lesspipe %s %s
LESSOPEN | /usr/bin/lesspipe %s
LOGNAME nicholas
LS_COLORS rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:s…
MANAGERPID 14143
OLDPWD /home/nicholas
PATH /snap/powershell/149/opt/powershell:/home/nicholas/.sdkman/candidates/java/current/bin:/home/nichol…
POWERSHELL_DISTRIBUTION_CHANN… PSSnap
PSModulePath /home/nicholas/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/snap/powershell…
PWD /home/nicholas/chimp
QT_ACCESSIBILITY 1
QT_IM_MODULE ibus
SDKMAN_CANDIDATES_API https://api.sdkman.io/2
SDKMAN_CANDIDATES_DIR /home/nicholas/.sdkman/candidates
SDKMAN_DIR /home/nicholas/.sdkman
SDKMAN_PLATFORM Linux64
SDKMAN_VERSION 5.9.1+575
SESSION_MANAGER local/mordor:#/tmp/.ICE-unix/14354,unix/mordor:/tmp/.ICE-unix/14354
SHELL /bin/bash
SHLVL 1
SNAP /snap/powershell/149
SNAP_ARCH amd64
SNAP_COMMON /var/snap/powershell/common
SNAP_CONTEXT q-DbUBQMK4V-sXTO62RcZMXvkKfoVBRGFLw7wD8IiowIRR2JVsv3
SNAP_COOKIE q-DbUBQMK4V-sXTO62RcZMXvkKfoVBRGFLw7wD8IiowIRR2JVsv3
SNAP_DATA /var/snap/powershell/149
SNAP_INSTANCE_KEY
SNAP_INSTANCE_NAME powershell
SNAP_LIBRARY_PATH /var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void
SNAP_NAME powershell
SNAP_REAL_HOME /home/nicholas
SNAP_REEXEC
SNAP_REVISION 149
SNAP_USER_COMMON /home/nicholas/snap/powershell/common
SNAP_USER_DATA /home/nicholas/snap/powershell/149
SNAP_VERSION 7.1.0
SSH_AGENT_PID 14316
SSH_AUTH_SOCK /run/user/1000/keyring/ssh
TERM xterm-256color
USER nicholas
USERNAME nicholas
VTE_VERSION 6003
WINDOWPATH 4
XAUTHORITY /run/user/1000/gdm/Xauthority
XDG_CONFIG_DIRS /etc/xdg/xdg-ubuntu:/etc/xdg
XDG_CURRENT_DESKTOP ubuntu:GNOME
XDG_DATA_DIRS /usr/share/ubuntu:/home/nicholas/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:…
XDG_MENU_PREFIX gnome-
XDG_RUNTIME_DIR /run/user/1000
XDG_SESSION_CLASS user
XDG_SESSION_DESKTOP ubuntu
XDG_SESSION_TYPE x11
XMODIFIERS #im=ibus
PS /home/nicholas/chimp>
just not sure how to get that specific value:
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-ChildItem Env:foo
Name Value
---- -----
foo bar
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-ChildItem Env:foo -valueOnly
Get-ChildItem: A parameter cannot be found that matches parameter name 'valueOnly'.
PS /home/nicholas/chimp>
not sure why there's no valueOnly:
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-Variable HOME -valueOnly
/home/nicholas
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> Get-Variable env:HOME -valueOnly
Get-Variable: Cannot find a variable with the name 'env:HOME'.
PS /home/nicholas/chimp>
where HOME has similar behavior.

works:
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> $f=Get-ChildItem Env:foo
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> $f.Name
foo
PS /home/nicholas/chimp>
PS /home/nicholas/chimp> $f.Value
bar
PS /home/nicholas/chimp>
but perhaps a bit awkward

Set Environment Variable Like So:
$env:TESTENVVAR = 'TEST VALUE HERE'
List all Environment Variables Like So:
Get-ChildItem env:
Value of a specific environment variable can be retrieved like so:
(Get-ChildItem Env:TESTENVVAR).Value

Related

Which CODE is used to print thread name in PS?

$ ps -mo pid,spid,tid,%cpu,psr,ucmd -p 4444 // first command
PID SPID TID %CPU PSR CMD
4444 - - 1.4 - main-app
- 4444 4444 1.1 0 -
- 8675 8675 0.1 0 -
- 8676 8676 0.1 0 -
$ ps -T -p 4444
PID SPID TTY TIME CMD
4444 4444 ? 00:16:50 main-app
4444 8675 ? 00:02:44 thread-one
4444 8676 ? 00:02:38 thread-two
Question> The -T switch generates a CMD column that indicates the name for each thread within the same process(i.e. main-app). Which CODE I should use to print out the same CMD column in the first command? I have tried ucmd and cmd but none of them show the expected thread name.
Thank you
Just replace m with T.
ps -To pid,spid,tid,%cpu,psr,ucmd -p 4444

python3: can't restore the out on console to a file from the program beginning to end& pexpect.EOF issue

Below is my code about using pexpect module achieve SSH logon function.
#!/usr/bin/env python
import pexpect
import sys
#use ssh to logon server
user="inteuser" #username
host="146.11.85.xxx" #host ip
password="xxxx" #password
command="ls -l" #list file on home/user directory
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
child.expect('password:')
child.sendline(password)
childlog = open('prompt.log',"ab") # restore prompt log to file prompt.log
__console__ = sys.stdout # make a backup of system output to console
sys.stdout = childlog # print the system output to childlog
child.expect(pexpect.EOF)
childlog.close()
sys.stdout = __console__ # back to the original state of system output
print(child.before) # print the contents before match expect function
after I execute my script
[~/Liaohaifeng]$ python3 ssh_test.py
b' \r\ntotal 69636\r\n-rw-rw-r-- 1 inteuser inteuser 949 Nov 28 02:01
01_eITK_trtest01_CrNwid.log\r\n
[~/Liaohaifeng]$ cat prompt.log
total 69412
-rw-rw-r-- 1 inteuser inteuser 949 Nov 28 02:01 01_eITK_trtest01_CrNwid.log
I think this result is not my expected. when I remove the code child.expect(pexpect.EOF) in my script, the output about print(child.before) can be correct(it should print the content before matching password)
Below is the output after I remove child.expect(pexpect.EOF)
[~/Liaohaifeng]$ python3 ssh_test.py
b"\r\n-------------------------------------------------------------------------------\r\n...
These computer resources are provided for authorized users only. For legal,
\r\n
security and cost reasons, utilization and access of resources are sxx, in\r\n
accordance with approved internal procedures, at any time if IF YOU ARE NOT AN AUTHORIZED USER; PLEASE EXIT IMMEDIATELY...\r\n "
my purpose is print out all the output to a file after executing the script,but the log file still only contains the output of listing directory. So why this happen? could you please help update my script? thank you very much.
You can use the spawn().logfile_read.
[STEP 101] # cat example.py
import pexpect, sys
child = pexpect.spawn('bash --norc')
if sys.version_info[0] <= 2:
# python2
child.logfile_read = open('/tmp/pexpect.log', 'w')
else:
# python3
fp = open('/tmp/pexpect.log', 'w')
child.logfile_read = fp.buffer
child.expect('bash-[.0-9]+[$#] ')
child.sendline('echo hello world')
child.expect('bash-[.0-9]+[$#] ')
child.sendline('exit')
child.expect(pexpect.EOF)
child.logfile_read.close()
[STEP 102] # python3 example.py
[STEP 103] # cat /tmp/pexpect.log
bash-4.4# echo hello world
hello world
bash-4.4# exit
exit
[STEP 104] #
It is a simple question, just adjust code order is OK.
#!/usr/bin/env python
import pexpect
import sys
#use ssh to logon server
user="inteuser" #username
host="146.11.85.xxx" #host ip
password="xxxx" #password
command="ls -l" #list file on home/user directory
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
childlog = open('prompt.log',"ab")
child.logfile = childlog
child.expect('password:')
child.sendline(password)
child.expect(pexpect.EOF)
childlog.close()

Freebsd jail command execution error with no reason

I try to execute command:
# service jail start myjail
I debug the /etc/rc.d/jail and dump that really command is:
/usr/sbin/jail -l -U root -i -f /var/run/jail.myjail.conf -c myjail
The output is:
usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]
-[cmr] param=value ... [command=command ...]
jail [-dqv] [-f file] -[cmr] [jail]
jail [-qv] [-f file] -[rR] ['*' | jail ...]
jail [-dhilqv] [-J jid_file] [-u username] [-U username]
[-n jailname] [-s securelevel]
path hostname [ip[,...]] command ...
The file /var/run/jail.myjail.conf is autogenrated by rc jail script based on variables of previously worked jail from rc.conf
The content is:
myjail {
host.hostname = "myjail.example.com";
path = "/var/jail/myjail.root";
ip4.addr += "192.168.0.150/32";
allow.raw_sockets = 0;
exec.clean;
exec.system_user = "root";
exec.jail_user = "root";
exec.start += "/bin/sh /etc/rc";
exec.stop = "/bin/sh /etc/rc.shutdown";
exec.consolelog = "/var/log/jail_myjail_console.log";
mount.devfs;
allow.set_hostname = 0;
allow.sysvipc = 0;
}
What is wrong?
The problem solved by replace old style configuration variables in rc.conf by one line:
jail_myjail_conf="/var/run/jail.myjail.conf"

pgrep in linux only identify 15 bytes proc name

in my Linux , while I run shell : ps -ef | grep Speed , I got the following :
myid 143410 49092 0 10:21 pts/12 00:00:00 ./OutSpeedyOrderConnection
myid 145492 49053 0 10:35 pts/11 00:00:00 ./SpeedyOrderConnection
That means , the pid of these 2 process are 143410 and 145492 .
Then I run shell : pgrep -l Speed , I got the following :
143410 OutSpeedyOrderC
145492 SpeedyOrderConn
and I run shell : pgrep OutSpeedyOrderC , I got :
143410
pgrep OutSpeedyOrderCo will get nothing !!!!!
look like pgrep will only identify 15 bytes of processname ,
anything I can do to get the right answer while I run
pgrep OutSpeedyOrderConnection ?!

not running cron with casperjs

sh_run.sh file..
#!/bin/bash
PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs /usr/local/bin/casperjs /home/test/html/run/site_check.js
Setting in crontab..
# cat /etc/crontab
45 0 * * * root sh /home/test/html/run/sh_run.sh
but casperjs not running..
crontab status is Rl..
what is Rl ??
# ps ax|grep phantomjs
28155 ? Rl 0:18 /usr/local/bin/phantomjs /usr/local/casperjs/bin/bootstrap.js --casper-path=/usr/local/casperjs --cli /home/test/html/run/site_check.js
of course..
# casperjs site_check.js
is running..
add comment...
# sh sh_run.sh &
# ps ax|grep phantomjs
1625 pts/0 Sl 0:01 /usr/local/bin/phantomjs /usr/local/casperjs/bin/bootstrap.js --casper-path=/usr/local/casperjs --cli /home/test/html/run/site_check.js
is running...
if ps status is Sl, data change.. (namely running..)
but ps status is Rl, data not change.. run by cron, status is always Rl.
Rl status does not change.
what problem? plz..
help me..

Resources