amarao: (Default)
[personal profile] amarao
Вот, я опять узнал что-то новое.

! command позволяет инвертировать код возврата. Казалось бы, тривиально, но с учётом мнения readline по поводу '!' у меня было табу насчёт восклицательных знаков в шелле.

Date: 2022-05-01 12:31 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi

Инвертироать - это как? (Сейчас удалось инвертировать 0 в 1, а остальные?)

Date: 2022-05-01 01:08 pm (UTC)
From: [personal profile] ex0_planet
Там не код возврата инвертируется, а результат сравнения ==0 / !=0

Date: 2022-05-01 01:12 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi

Вот...

> ! ls
> echo $?
1

Date: 2022-05-01 01:27 pm (UTC)
From: [personal profile] ex0_planet
$ function 42() { return 42; }
$ 42 ; echo $?
42
$ ! 42 ; echo $?
0
$ ! ! 42 ; echo $?
42


Несколько неожиданный даже для меня самого результат: я думал что в какой-то момент оно таки превратится в некий эквивалент bool, и там будет как в сишке — 0 => true => 0, x => false => 1 (для любого ненулевого x).

Date: 2022-05-01 02:16 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi

OMG. It's complicated, is not it? How do they keep 42? Seems like the script is compiled (and optimized) before running. Weird, weird. Not that I don't like it. Enjoying it.

Date: 2022-05-01 03:18 pm (UTC)
From: [personal profile] ex0_planet
Yep, it is more complicated than I thought it would be.

Compiled? Not necessarily, IMO they have some sort of expression context (kinda like Perl, after all it is from the same era :)). So, a '! ! 42' evaluates in some sort of numeric context (there is an implicit assignment to $!); in the context of 'if .. then' expression it is evaluated as boolean. Makes sense, huh!?

Date: 2022-05-01 06:35 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi

Yes, but one had to reduce ! ! to nop, otherwise how does it know that ! 0 should be 42?

Profile

amarao: (Default)
amarao

September 2025

S M T W T F S
 12345 6
78 910111213
14151617 181920
21222324252627
282930    

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 5th, 2025 04:54 pm
Powered by Dreamwidth Studios