Los -d
y -z
no son comandos sino opciones de las utilidades test
y [
. Estas utilidades están integradas en bash
y documentadas en el manual bash
. Estas utilidades y estas banderas también están estandarizadas por POSIX, por lo que están disponibles en cualquier shell POSIX, no solo bash
.
Si está en una sesión interactiva bash
, puede obtener documentación para el -integrado en variantes de estas utilidades escribiendohelp test
(help [
también funciona, pero su texto solo hace referencia a la documentación paratest
).
man test
y man [
también deberían funcionar. Estos manuales describen las utilidades externas , probablemente /bin/test
y /bin/[
, no las que usa por defecto en bash
.
Entonces, por ejemplo,
! test -z "$dir" && test -d "$dir" && printf '%s is a directory' "$dir"
es exactamente equivalente a
! [ -z "$dir" ] && [ -d "$dir" ] && printf '%s is a directory' "$dir"
o, si se quiere,
if ! test -z "$dir" && test -d "$dir"; then
printf '%s is a directory' "$dir"
fi
y
if ! [ -z "$dir" ] && [ -d "$dir" ]; then
printf '%s is a directory' "$dir"
fi
(! [ -z "$dir" ]
probablemente se escribiría más comúnmente [ ! -z "$dir" ]
o [ -n "$dir" ]
, y solo he usado la prueba -z
anterior porque se mencionó en la pregunta, la prueba -d
en una cadena vacía fallaría de todos modos ).
Véase también:
$ sed '/,$/{N;s/\n//;}' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
Если пробелы следует удалить:
$ sed '/,$/{N;s/\n[[:blank:]]*//;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(если вы хотите, чтобы между строками оставался один пробел, замените //
в коде на/ /
)
Если строки можно продолжать несколько раз, как в
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
затем,
$ sed '/,$/{:loop;N;s/\n[[:blank:]]*//;/,$/bloop;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
Этот последний sed
скрипт объясняется аннотациями:
/,$/{ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/\n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
}
# implicit output of (possibly) modified line at end
sed '/,$/{N;s/\n[[:blank:]]\+/ /}' file
Если вы видите строку, заканчивающуюся запятой, прочитайте следующую строку, а затем замените новую строку и начальный пробел следующей строки одним пробелом.