这个简单的脚本失败了:
#!/bin/bash
n=1
printf -v fn "%05d" $n
echo $fn
和
3: printf: Illegal option -v
为什么 !!!(Ubuntu 14.04)
对于每个@Joe,这似乎是Whats与./script.sh和sh script.sh运行shell脚本之间的区别的重复。
对于@ Telemachus,Debian及其派生工具使用破折号作为默认外壳。有关更多信息,请参见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上重现该问题。我确实在Ubuntu上通过使用sh
而不是调用脚本来重现了该脚本bash
。
我将其余答案保留在原处,因为它演示了您可能要采取的一些步骤来解决问题。
您可以检查您的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
已被其他地方的定义所替代?这是我用来检查外壳程序中名称定义的函数:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句