gpt4 book ai didi

bash - 如何在 bash 中格式化数字,可能使用 printf?

转载 作者:行者123 更新时间:2023-12-02 07:29:08 25 4
gpt4 key购买 nike

这个如此简单的脚本失败了:

#!/bin/bash
n=1
printf -v fn "%05d" $n
echo $fn

3: printf: Illegal option -v

为什么!!! (Ubuntu 14.04)

最佳答案

根据@Joe,这似乎是 Whats the difference between running a shell script as ./script.sh and sh script.sh 的副本.

根据@Telemachus,Debian 及其衍生产品使用 dash 作为它们的默认 shell。参见 http://wiki.ubuntu.com/DashAsBinSh获取更多信息。

这是我在 Ubuntu 系统上看到的:

$ ls -lF `which sh`
lrwxrwxrwx 1 root root 4 Aug 15 2012 /bin/sh -> dash*
$ ls -lF `which bash`
-rwxr-xr-x 1 root root 959168 Mar 30 2013 /bin/bash*

这就是为什么我无法在 Mac OS X 10.8.5 上重现该问题的原因。我确实通过使用 sh 而不是 bash 调用脚本在 Ubuntu 上重现了它。

我将保留其余的答案,因为它展示了您可能采取的一些步骤来解决问题。

你能检查一下你的 bash 版本吗?

$ bash --version
bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)

这真的有用吗?

#!/bin/bash
n=1
printf -v fn "%05d" $n
echo $fn

检查名称printf的类型?

$ type printf
printf is a shell builtin
$ function printf { echo "giggle" ; }
giggle
$ type printf
printf is a function
printf ()
{
echo "giggle"
}
giggle
$

查看 printf 内置帮助?

$ help printf
help printf

printf: printf [-v var] format [arguments]
printf formats and prints ARGUMENTS under control of the FORMAT. FORMAT
is a character string which contains three types of objects: plain
characters, which are simply copied to standard output, character escape
sequences which are converted and copied to the standard output, and
format specifications, each of which causes printing of the next successive
argument. In addition to the standard printf(1) formats, %b means to
expand backslash escape sequences in the corresponding argument, and %q
means to quote the argument in a way that can be reused as shell input.
If the -v option is supplied, the output is placed into the value of the
shell variable VAR rather than being sent to the standard output.

内置的 printf 是否被其他地方的定义所取代?这是我用来检查 shell 中名称定义的函数:

list () 
{
if [[ 0 == $# ]]; then
Log "";
Log "FUNCTIONS:";
Log "----------";
declare -F;
Log "";
Log "EXPORTS:";
Log "--------";
export -p;
Log "";
Log "PRINTENV:";
Log "--------";
printenv;
else
while [[ ! -z "$1" ]]; do
local name="$1";
shift;
if ! alias "${name}" 2> /dev/null; then
if ! declare -f "${name}"; then
if ! help "${name}" 2> /dev/null; then
if ! which "${name}"; then
Log "Not found: '${name}'";
fi;
fi;
fi;
fi;
done;
fi
}

这是我在新的 shell 中运行它时的输出:

$ list printf
printf: printf [-v var] format [arguments]
printf formats and prints ARGUMENTS under control of the FORMAT. FORMAT
[… snip …]

但是如果我重新定义printf,它会显示定义:

$ function printf { echo "kibble" ; }
kibble
$ printf
kibble
kibble
$ list printf
printf ()
{
echo "kibble"
}
kibble
$

我很想知道这里到底发生了什么!!!

我喜欢其他答案的建议,尝试使用 bash 显式调用脚本:

$ bash myscript.sh

这是我在 Ubuntu 服务器上看到的:

$ uname -a
Linux rack 3.11.0-17-generic #31-Ubuntu SMP Mon Feb 3 21:52:43 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ cat > dme.sh
#!/bin/bash
n=1
printf -v fn "%05d" $n
echo $fn
$ chmod +x ./dme.sh
$ ./dme.sh
00001
$ bash dme.sh
00001
$ sh dme.sh
dme.sh: 3: printf: Illegal option -v

关于bash - 如何在 bash 中格式化数字,可能使用 printf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com