You are not logged in.

#101 2020-02-14 06:47:29

ohnonot
...again
Registered: 2015-09-29
Posts: 4,410
Website

Re: Handy command-line stuff for terminals or scripts

^ Nice one about PIPESTATUS.

johnraff wrote:

Brackets unnecessary.

Those were only for explanation, not actual usage.

Tell me, how does this work:

A || B && C

???

Offline

#102 2020-02-14 06:57:28

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,543
Website

Re: Handy command-line stuff for terminals or scripts

^
A returns 0:
    B not run
    C runs (the last command ran returned 0)

A returns 1:
    B runs
    B returns 0:
        C runs
    B returns 1:
        C not run

---
About brackets: I meant that they were not necessary (or even helpful) for understanding the logic flow.


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

#103 2020-02-22 03:57:33

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,543
Website

Re: Handy command-line stuff for terminals or scripts

Forking

An ever-popular subject. The net is full of people asking how to fork off processes in the background from a shell script. As everyone knows, basically you just add a & after the command. It gets more complicated if you want the process to go on running after your terminal has closed, or the calling script has exited.

Google, and you'll find:

nohup command &

command & 
disown

setsid command &

# and subshells etc
( nohup command & )
( nohup command & ) &
( setsid command & )

in increasingly desperate attempts to get that pesky process sent off entirely by itself. Take a few minutes to try them in a terminal, eg

mousepad &

of course, close the terminal and mousepad dies too.

mousepad &
disown

This time mousepad survives closing the terminal. This is strong too:

setsid mousepad

But in scripts, things are different.
This is where I was stuck, and almost giving up. In a script, nohup, disown and setsid no longer work the way they did from a terminal. If a script contains eg

nohup tint2 &

tint2 might stay running if you trigger the script from a launcher or menu and it then exits, but if you run it in a terminal and hit Ctrl+C to close the script while its still running, the SIGINT or SIGTERM signal will be passed on to tint2 which stops even though it's supposed to be backgrounded. neutral

It's because (I just learned) Job Control is not enabled in scripts by default - it's mainly intended for interactive shells. But, you can enable it by putting

set -m

at the top of your script, or - maybe safer - just around the part where you launch the background process:

set -m
do launch-in-background stuff
set +m

Now disown and friends will work the same way they do in a terminal.

Some pages that came up:
https://unix.stackexchange.com/question … ses-within
https://unix.stackexchange.com/question … set-m-does
https://medium.com/@copyconstruct/bash- … 36da3e4aa7
https://mywiki.wooledge.org/BashGuide/JobControl


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

#104 2020-02-22 09:31:59

rbh
Member
From: Sweden/Vasterbotten/Rusfors
Registered: 2016-08-11
Posts: 326

Re: Handy command-line stuff for terminals or scripts

But aint it easier to use tools like screen or tmux?


// Regards rbh

Offline

#105 2020-02-22 09:33:26

malm
jgmenu developer
Registered: 2016-10-13
Posts: 587
Website

Re: Handy command-line stuff for terminals or scripts

Awesome. Didn’t know that.

Offline

#106 2020-02-22 09:46:52

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,543
Website

Re: Handy command-line stuff for terminals or scripts

rbh wrote:

But aint it easier to use tools like screen or tmux?

Not necessarily, depending on how familiar you are with those tools, and how complex the task is. If you just want to make sure conky isn't killed or something like that, the shell solution is more appropriate IMO.


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

#107 2020-02-22 10:39:30

clusterF
Member
Registered: 2019-05-07
Posts: 539

Re: Handy command-line stuff for terminals or scripts

Nice tips there johnraff, i have used disown for quite awhile when testing things out, especially conky.

Offline

#108 2020-02-24 07:14:46

ohnonot
...again
Registered: 2015-09-29
Posts: 4,410
Website

Re: Handy command-line stuff for terminals or scripts

johnraff wrote:

Job Control is not enabled in scripts by default - it's mainly intended for interactive shells. But, you can enable it by putting

set -m

at the top of your script, or - maybe safer - just around the part where you launch the background process:

set -m
do launch-in-background stuff
set +m

Now disown and friends will work the same way they do in a terminal.

Thank you so much!
I, too, stumbled around that.
Next time I need it, I know what to do.

Offline

#109 2020-03-16 05:11:14

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,543
Website

Re: Handy command-line stuff for terminals or scripts

Make that $i local

This bit me last week. There was a loop, like

for (( i=1; i<10; i++ ))
do
    something with $i
    function somefile
done

And somefile was being sent to a function like:

function(){
    for i in "$@"
    do some file thing with "$i"
done

So after the first call to function() the loop got all messed up, because $i had been changed from an integer to a filepath. neutral
Easy fix, at the top of function()

local i

so the i inside function is kept separate from the outside loop. If the loop was a function too, best to do the same there.

Variables inside functions are global by default in Bash, so any you don't want to share elsewhere should be declared local. Well known, but it's easy to forget those little i's inside loops.  yikes


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

#110 2020-03-16 07:00:45

ohnonot
...again
Registered: 2015-09-29
Posts: 4,410
Website

Re: Handy command-line stuff for terminals or scripts

Wouldn't it be easier to (always try to) use unique variable names?
for file in "$@"
etc.

Offline

#111 2020-03-16 08:04:34

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 6,543
Website

Re: Handy command-line stuff for terminals or scripts

^I think it's always advisable to make any variables local inside functions that won't be referred to anywhere else. "File" is a pretty generic name too, that might be used somewhere else. In a short script where you can easily remember all the variable names it's less important of course, but in anything big, where maybe other snippets are being sourced from files... it's good to keep the environment as clean as possible.


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

Board footer

Powered by FluxBB