You are not logged in.

#181 2021-03-12 19:10:23

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

Re: Handy command-line stuff for terminals or scripts

fd probably stands for file descriptor.
The most common are stdin, stdout, stderr.
It pays to get aquainted with this.

Offline

#182 2021-03-13 02:34:28

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

Re: Handy command-line stuff for terminals or scripts

^True that this stuff is worth reading up on.

But, the "fd" in this case is just a confusing choice of variable name.
Just confirmed - the code snippet works just as well with {file_desc} + "$file_desc" or, presumably, anything else.
The key is the special use of the { } syntax in this situation.

---
Also, just confirmed that the queue order is not determined by the order the scripts are called:

john@lithium:~$ t1='This is a longish sentence, extended by a few words'
john@lithium:~$ t2='This is another long sentence, extended by a few more words'
john@lithium:~$ t3='This is yet a third long sentence, extended by even  more words'
john@lithium:~$ say "$t1" & say "$t2" & say "$t3" &

Spoke t2 then t1 then t3.
This probably doesn't matter in many cases, but if it did it would need more research...

Last edited by johnraff (2021-03-13 02:38:58)


...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

Online

#183 2021-03-14 05:11:46

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

Re: Handy command-line stuff for terminals or scripts

misko_2083 wrote:

That's new. Appeared in bash 4.2

Which came out in 2011. What's ten years! These young hip coders, they can never leave a good thing alone... monkey

Offline

#184 2021-05-17 06:56:21

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

Re: Handy command-line stuff for terminals or scripts

Wait for a process to end, even if it's not a child of your script.
'wait' (see 'help wait') is a handy way to pause a script if you want to wait for any forked off processes ('process &') to close before continuing, but it only works if the process is a child of your script.

But, say, you want to kill some external process (eg 'pkill -x conky') and pause until you're sure it's dead? Tricky, but I eventually stumbled on this. If you already know the PID:

tail -f /dev/null --pid="$pid"

So tail is looking at /dev/null, but will stop when $pid exits.
You can find $pid with pgrep, but be careful to make your conditions tight enough so that you don't pull out the wrong process! (If there are multiple instances you'll need to match the whole command line with the -f option.)
eg in this safeKill function:

# Kill only with exact match, and only if process belongs to executor.
# Returns success even if process is not running.
# Usage: safeKill <processname>
safeKill() {
    local procname pid
    procname=$1
    pid=$( pgrep -x -u "$USER" "$procname" ) || return 0
    kill "$pid" || return 1 # something went wrong
    tail -f /dev/null --pid="$pid" # wait for process to terminate
}

...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

Online

#185 2021-06-12 06:55:28

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

Re: Handy command-line stuff for terminals or scripts

Youtube-dl a whole playlist, numbered tracks, in its own subfolder.

Throwing this in here because it's not worth a whole thread, and there doesn't seem to be a dedicated "media tweaks" topic.

I put this in ~/.bash_aliases but use it how you wish. (Of course replace $HOME/Downloads/youtube/ with your youtube downloads directory):

downloadpl() {
    youtube-dl "$@" -i -x -o "$HOME/Downloads/youtube/%(artist)s-%(playlist)s/%(playlist_index)s-%(title)s.%(ext)s" "$(xsel)"
}

If you've got xsel installed you just have to select the youtube url in the browser address bar before running downloadpl in a terminal.
(The "$@" is just so you can throw in any extra youtube-dl options - I seldom do though.)

NOTE: The main point of this post is the -o option formatting, which gives you nicely organized files from a playlist download. The other youtube-dl options are my own personal preferences and users should adjust them to suit their needs.

See this discussion on audio-only downloads: https://forums.bunsenlabs.org/viewtopic.php?id=7637

Last edited by johnraff (2021-06-22 04:28:51)


...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

Online

#186 2021-06-12 13:49:19

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

Re: Handy command-line stuff for terminals or scripts

Botsay (Randomly generated ascii robots)

    _ _,_,_ _        .--------------------------------------------.
    \( 6=6 )/        | # Install me with                          |
      \_E_/       --<| go get -u github.com/xyproto/botsay        |
  .-._/___\_.-.      '--------------------------------------------'
  ;   \___/   ;
      [] []
     /:] [:\

Last edited by brontosaurusrex (2021-06-12 18:26:55)

Offline

#187 2021-06-15 04:58:20

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

Re: Handy command-line stuff for terminals or scripts

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

Offline

#188 2021-06-15 12:14:10

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

Re: Handy command-line stuff for terminals or scripts

Go get considered harmful?

Offline

#189 2021-06-16 00:01:44

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

Re: Handy command-line stuff for terminals or scripts

I do not even know what "go get" is.


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

Offline

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

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
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

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

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

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

Offline

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

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

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

Offline

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

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 8,958
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

Online

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

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

Re: Handy command-line stuff for terminals or scripts

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

Offline

#195 2022-03-26 12:14:26

sleekmason
zoom
Registered: 2018-05-22
Posts: 871
Website

Re: Handy command-line stuff for terminals or scripts

^ Holy Moly! Nice find:)

This works well, but how do you kill it? (or, can I also get the initial random script working as well?  I do believe I may have other uses for that script:)

Last edited by sleekmason (2022-03-26 12:48:27)

Offline

Board footer

Powered by FluxBB