You are not logged in.

#201 2021-06-16 07:30:31

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,435
Website

Re: Handy command-line stuff for terminals or scripts

^ It 'gets' and builds stuff written in golang.
https://golang.org/pkg/cmd/go/internal/get/

Offline

#202 2021-06-16 11:43:44

Sector11
Conky 1.9er Mod Squid
From: Upstairs
Registered: 2015-08-20
Posts: 6,864

Re: Handy command-line stuff for terminals or scripts

OK Thank you


The sun will never set if you keep walking towards it. - my son
Being positive doesn't understand physics.
_______________________________
Debian 10 Buster = SharpBang ♯!

Offline

#203 2021-06-16 16:07:04

twoion
一期一会
Registered: 2015-08-10
Posts: 3,436

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

^ If you implicitly trust go and any random github coder.
For me this is equivalent to `curl {something} | sudo bash -`.

Hihi, how about

kubectl apply -f https://...

of course I trust the internet!

Offline

#204 2021-06-17 19:52:24

Sector11
Conky 1.9er Mod Squid
From: Upstairs
Registered: 2015-08-20
Posts: 6,864

Re: Handy command-line stuff for terminals or scripts

twoion wrote:

of course I trust the internet!

Yes of course, as do I!

roll roll


The sun will never set if you keep walking towards it. - my son
Being positive doesn't understand physics.
_______________________________
Debian 10 Buster = SharpBang ♯!

Offline

#205 2021-06-22 05:28:03

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,615
Website

Re: Handy command-line stuff for terminals or scripts

Parse the command line of a process from its PID.

I've used this a couple of times in the last few days so thought I should post it here.

You can get the command line(s) of a process with:

pgrep -ax <name>

which will give you something like:

1660 conky -c /home/john/.config/conky/two words/BL-Button-conky.conf

This is fine if you just want to look at the command, but if you want to do something with the arguments you get problems with spaces or special characters. eg the filepath above , or things like '--name "this and that"' Even if the filepath is quoted you can't just copy it and use it. You'd have to do some complicated parsing to imitate what the shell does...

Luckily there's a better way™. /proc/<pid>/cmdline is a file with the full command line, all the arguments separated by NULL bytes. NULL bytes can't exist in a shell string because they're the string end marker, but they can be in a file and we can use mapfile to get the arguments and put them in an array, where they'll be safe even if they contain spaces, line breaks or whatever. Like this (for that conky above):

mapfile -d '' -t cmdarray < "/proc/1660/cmdline"

(mapfile's -d delimiter option can be set to the empty string '' in which case mapfile interprets it as meaning to use NULL.)

Now all the arguments are in $cmdarray and you can access them any way you want, eg:

john@lithium:~$ for i in "${cmdarray[@]}"; do echo "ARG> $i"; done
ARG> conky
ARG> -c
ARG> /home/john/.config/conky/two words/BL-Button-conky.conf

# or just run the command again, arguments properly quoted:
"${cmdarray[@]}"

You'll still need pgrep or pidof to get the PID, but you don't have to mess with the -a option now.

There's more about the stuff in /proc/ than you likely want to read any time soon, but it's here: https://man7.org/linux/man-pages/man5/proc.5.html

Last edited by johnraff (2021-06-30 05:46:36)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#206 2021-06-30 04:39:59

ohnonot
...again
Registered: 2015-09-29
Posts: 5,568

Re: Handy command-line stuff for terminals or scripts

^ This is precious. Will remember & use it evtl.


Give to COVAX! Here or here. (explanation)
My Repos: notabug | framagit

Offline

#207 2021-12-30 15:28:50

misko_2083
Member
Registered: 2016-05-24
Posts: 552

Re: Handy command-line stuff for terminals or scripts

Execute multiple statements in a line delimited with comma

echo $(( n=20, n=n+30 ))

Won't work in dash. Works in bash, posh, ksh, zsh.

This in an alternative to if statement:

n=20
var1=10
var2=0
echo $(( n>=20 ? var1 : var2 ))

Works in all shells.
Condition comes first followed by "?".
If the condition returns true then it will execute the statement that is defined before ‘:’
otherwise it will execute the statement that is defined after ‘:’.

Last edited by misko_2083 (2021-12-30 15:42:16)


They use force, to make you do, what the deciders, have decided you must do!

Offline

#208 2021-12-30 16:22:51

twoion
一期一会
Registered: 2015-08-10
Posts: 3,436

Re: Handy command-line stuff for terminals or scripts

?: is called the ternary operator btw <https://en.wikipedia.org/wiki/%3F:> it is ubiquitous.

Offline

#209 2021-12-31 16:08:47

misko_2083
Member
Registered: 2016-05-24
Posts: 552

Re: Handy command-line stuff for terminals or scripts

twoion wrote:

?: is called the ternary operator btw <https://en.wikipedia.org/wiki/%3F:> it is ubiquitous.

I know about ternary, however "ubiquitous" will be have to be searched in the dictionary.


They use force, to make you do, what the deciders, have decided you must do!

Offline

Board footer

Powered by FluxBB