[英]How to format a number in bash, possibly with printf?
這個簡單的腳本失敗了:
#!/bin/bash
n=1
printf -v fn "%05d" $n
echo $fn
與
3: printf: Illegal option -v
為什么! (Ubuntu 14.04)
您可能正在使用不支持-v
選項的Bash版本。 嘗試運行bash --version
。 它應該是3.1或更高版本。
另外,請確保您確實使用bash運行腳本。 嘗試顯式調用bash script.sh
。
對於@Joe,這似乎是Whats和Shell腳本作為./script.sh和sh script.sh運行之間的區別的重復。
對於@ 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
您正在將腳本作為/bin/sh
的參數運行,如下所示:
$ sh script.sh
script.sh: 3: printf: Illegal option -v
忽略#!
線。 運行為:
$ ./script.sh
00001
代替。
正如其他答案告訴您的那樣,較早的BASH版本不支持printf -v
但是您可以在較早的BASH中執行以下操作來設置fn
:
#!/bin/bash
n=1
fn=$(printf "%05d" "$n")
echo "$fn"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.