标签: printf

printf "$V" 在以下情况下失败: '^+%&/%+^

避免\n
使用“ printf ”而不是“ echo

设置字符串

IFS='' read -r -d '' V <<'EOF'
'^+%&/%+^
EOF
Run Code Online (Sandbox Code Playgroud)

回声测试(工作正常)

echo "$V"
'^+%&/%+^
Run Code Online (Sandbox Code Playgroud)

使用printf测试(失败)

printf "$V"
bash: printf: `&': invalid format character
Run Code Online (Sandbox Code Playgroud)

printf

0
推荐指数
1
解决办法
276
查看次数

为什么在 bash 的 printf 内置函数中只有一些字母被认为是无效数字?

我在胡闹,忘了删除printf语句的一部分,得到了这个输出:

# printf '%*s%c' {A..Z}
bash: printf: A: invalid number
bash: printf: D: invalid number
bash: printf: G: invalid number
bash: printf: J: invalid number
bash: printf: M: invalid number
bash: printf: P: invalid number
bash: printf: S: invalid number
bash: printf: V: invalid number
bash: printf: Y: invalid number
BCEFHIKLNOQRTUWXZ
Run Code Online (Sandbox Code Playgroud)

为什么被BCEFHIKLNOQRTUWXZ认为是有效数字?我认为这是因为它们可能是格式字符,但这似乎并不能解释Z.

有什么地方可以阅读/了解更多相关信息吗?

系统信息:

  • GNU bash,版本 4.4.12(1)-release (x86_64-unknown-linux-gnu)

bash printf

0
推荐指数
1
解决办法
1990
查看次数

Bash printf 格式不起作用!

我的脚本是:

#!/bin/bash
num1=32.55
num2=145.958
printf "%5.2f\n" $num1
printf "%5.2f\n" $num2
Run Code Online (Sandbox Code Playgroud)

输出是:

32.55
145.96
Run Code Online (Sandbox Code Playgroud)

代替:

   32.55
  145.96
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?如果有帮助,我正在使用 Fedora 27 BetaLinux Kernel 4.13.8-300.fc27.x86_64

bash printf text-formatting

0
推荐指数
1
解决办法
933
查看次数

printf 和 echo -e 给出不同的结果

我使用\0n转义序列与echo -eprintf其中n是要被打印的ASCII字符的八进制值。当我使用该语句时echo -e "\0101",shell 正在打印字符,A但是当我使用printf "\0101"shell 时,正在打印1这是我提供的八进制数的最低有效位,但是如果我0在开头删除并使用命令,因为printf "\101"A按预期正确打印。所以我的疑问是为什么这个特定的转义序列会有如此大的差异,我尝试了许多其他转义序列,但它们使用 echo 和 printf 都给出了相同的结果。我正在使用Ubuntu并且目前正在研究bash.

ubuntu echo ascii escape-characters printf

0
推荐指数
1
解决办法
229
查看次数

在列中打印出序列 r1 r2 ... r100

我想要以下输出

r 1
r 2
r 3
.
.
.
r 100
Run Code Online (Sandbox Code Playgroud)

到目前为止我已经尝试过 printf "%s\n" "r {1..100..1}"

bash printf

0
推荐指数
1
解决办法
55
查看次数

printf 的输出与 FORMAT 参数中指定的不同

我有点困惑, 的输出printfFORMAT参数中指定的不同:

$ echo "$PWD_HASH"
{SHA512-CRYPT}$6$ZLgRL2DZSKYUCzht$PaTcpkoOi6P6p9ItIjRTL00MCB/8IU.fJbGk9EO/LxUAgZwGASO6qXNSNSzxQRBGjNiPpiArgwOacZSG5A6FL1
$ PWD_JSON=$(printf '{"password": "%s"}' "$PWD_HASH")
$ echo "$PWD_JSON" 
"}password": "{SHA512-CRYPT}$6$ZLgRL2DZSKYUCzht$PaTcpkoOi6P6p9ItIjRTL00MCB/8IU.fJbGk9EO/LxUAgZwGASO6qXNSNSzxQRBGjNiPpiArgwOacZSG5A6FL1
Run Code Online (Sandbox Code Playgroud)

如何/为什么?如何获得所需的输出?

如果我直接传递字符串(不是从变量中),那么我会得到预期的输出:

$ PWD_JSON=$(printf '{"password": "%s"}' '{SHA512-CRYPT}$6$ZLgRL2DZSKYUCzht$PaTcpkoOi6P6p9ItIjRTL00MCB/8IU.fJbGk9EO/LxUAgZwGASO6qXNSNSzxQRBGjNiPpiArgwOacZSG5A6FL1')
$ echo "$PWD_JSON"
{"password": "{SHA512-CRYPT}$6$ZLgRL2DZSKYUCzht$PaTcpkoOi6P6p9ItIjRTL00MCB/8IU.fJbGk9EO/LxUAgZwGASO6qXNSNSzxQRBGjNiPpiArgwOacZSG5A6FL1"}
Run Code Online (Sandbox Code Playgroud)

只是为了补充接受的答案,在我这里PWD_HASH我有来自docker exec -t. 显然,使用的行尾TTYCRLF( \r\n),因此出乎意料的\r......

bash shell-script printf

0
推荐指数
1
解决办法
56
查看次数

在 bash 中使用 printf

在 bash 中使用 printf 时,是否总是需要提供格式字符串,如下所示。

printf "" "V01 Oct 2021"
Run Code Online (Sandbox Code Playgroud)

或者printf "V01 Oct 2021"还是可以的?

bash printf

0
推荐指数
1
解决办法
393
查看次数

为什么我想在打印到标准输出时避免使用“printf”这种类似于回显的方式?

当我试图了解如何工作时,我编写了以下脚本printf

#!/usr/bin/bash

printf "Give me your three preferences.?
"
read p1
read p2
read p3
printf "%s\n" "${p1}" "${p2}" "${p3}"
printf "Just the same as this, \n"
printf "%s\n%s\n%s\n" "${p1}" "${p2}" "${p3}"
printf "I've found this way to be the easiest, ${p1}\n ${p2}\n ${p3}\n"
Run Code Online (Sandbox Code Playgroud)

通过反复试验,我发现第三种方法是有效的 - 尽管没有指定这个学习资源。我printf最常使用这种方式,因为它echo类似于 -。是否存在这种打印值的方式stdout不起作用的变量情况或值?为什么我想避免这种使用方式printf

bash stdout printf

0
推荐指数
1
解决办法
140
查看次数

如何将字段与 printf 对齐

以下语法将 output1 打印下来

echo "$status" 
Run Code Online (Sandbox Code Playgroud)

输出 1:

    component_name : TEZ_CLIENT     recovery_enabled : true
    component_name : WEBHCAT_SERVER         recovery_enabled : true
    component_name : YARN_CLIENT            recovery_enabled : true
    component_name : ZKFC           recovery_enabled : true
    component_name : ZOOKEEPER_CLIENT             recovery_enabled : true
    component_name : ZOOKEEPER_SERVER                      recovery_enabled : true
Run Code Online (Sandbox Code Playgroud)

如何添加 printf 语法以获得以下几行:

预期产出

    component_name : TEZ_CLIENT             recovery_enabled : true
    component_name : WEBHCAT_SERVER         recovery_enabled : true
    component_name : YARN_CLIENT            recovery_enabled : true
    component_name : ZKFC                   recovery_enabled : true
    component_name : ZOOKEEPER_CLIENT       recovery_enabled : true
    component_name : ZOOKEEPER_SERVER …
Run Code Online (Sandbox Code Playgroud)

linux awk printf

-1
推荐指数
1
解决办法
5730
查看次数

bash中的printf命令

printf "%50s\n" " I ate 4 eggs"
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我想从一个变量中分配 50。以及如何在 bash 的 for 循环中使用变量?

任何的想法?

bash printf

-4
推荐指数
1
解决办法
1156
查看次数