Есть ли сигналы EXIT, DEBUG, RETURN и ERR?

Вы можете сохранить предыдущую строку в удержании space:

sed '
 /^pattern/{
   x
   /./ {
     x
     s/^/\
/
     x
   }
   x
 }
 h'

Это было бы более разборчиво с awk , хотя:

awk '!previous_empty && /pattern/ {print ""}
     {previous_empty = $0 == ""; print}'

Как и GNU-реализации , sed имеет параметр -i для in-place редактирования, GNU-реализация awk как -i inplace для этого.

14
10.08.2017, 09:03
2 ответа

Эти имена имеют особое значение в bash, использование которых объясняется в руководстве:

If a sigspec is 0 or EXIT, arg is executed when the shell exits. If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function...

If a sigspec is ERR, the command arg is executed whenever a pipeline (which may consist of a single simple command), a list, or a compound command returns a non-zero exit status, subject to the following conditions...

20
27.01.2020, 19:50

Это не сигналы, но имеет смысл использовать механизм trapи для некоторых других ситуаций. Эти имена имеют значение только внутри bash.

15
27.01.2020, 19:50

Теги

Похожие вопросы