команда printf в bash [закрыто]

Técnicamente, depende de cómo lo controle el proceso de inicio de sesión. A menos que AIX sea muy especial allí, el núcleo no comprueba qué hay o no en los archivos group y passwd.

Si un proceso raíz le da a alguien una terminal y un shell con una ID de usuario arbitraria, más o menos iniciará sesión -Sin embargo, algunos programas no funcionarán correctamente, ya que usarán el UID y/o el GID principal como argumento para llamadas a libc y error si hay un comportamiento defectuoso.

-4
29.07.2018, 01:52
2 ответа

Вы можете присвоить 50 такой переменной:

variable=50

Вы можете использовать эту переменную в цикле for следующим образом:

for i in foo bar baz; do
    printf "%${variable}s\n" "I ate 4 eggs"
done

Если вы пытаетесь увеличить отступ в своем операторе printf, вы можете сделать что-то вроде этого:

$ for i in {45..50}; do printf "%${i}s\n" "I ate 4 eggs"; done
                                 I ate 4 eggs
                                  I ate 4 eggs
                                   I ate 4 eggs
                                    I ate 4 eggs
                                     I ate 4 eggs
                                      I ate 4 eggs
0
28.01.2020, 05:19

По крайней мере, с bashвы можете передать переменную ширину поля, используя модификатор преобразования стиля C -printf*:

width=50
printf '%*s\n' $width "I ate 4 eggs"
                                      I ate 4 eggs

Подробнее см. man 3 printf.

Как использовать его в цикле for, зависит от желаемого результата.


Судя по вашему комментарию, вы можете использовать

for i in {45..50}; do 
  for j in {1..13}; do 
    printf '%*.*s\n' $i $j "I ate 4 eggs"
  done
done

для предоставления как ширины поля, так и точности в качестве переменных, в результате чего

                                   I
                                   I
                                  I a
                                 I at
                                I ate
                               I ate
                              I ate 4
                             I ate 4
                            I ate 4 e
                           I ate 4 eg
                          I ate 4 egg
                         I ate 4 eggs
                         I ate 4 eggs
                                     I
                                    I
                                   I a
                                  I at
                                 I ate
                                I ate
                               I ate 4
                              I ate 4
                             I ate 4 e
                            I ate 4 eg
                           I ate 4 egg
                          I ate 4 eggs
                          I ate 4 eggs

и так далее.

6
28.01.2020, 05:19

Теги

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