2014-03-02 23 views
1

我正在編寫一個在Android的mksh上運行的shell腳本。在bash中,我能做到以下幾點:替代Android mksh的printf?

printf -v NUMBER "%d" \'"$CHARACTER" 

上面的命令轉換$字符轉換爲符號十進制數。但mksh不支持printf。有沒有其他的解決方案只使用mksh/ksh的內部命令?

回答

0

mksh確實支持printf在一定程度上,但不推薦。

print [-nprsu[n] | -R [-en]] [argument ...] 
     print prints its arguments on the standard output, separated by 
     spaces and terminated with a newline. The -n option suppresses the 
     newline. By default, certain C escapes are translated. These in- 
     clude these mentioned in Backslash expansion above, as well as 
     '\c', which is equivalent to using the -n option. Backslash expan- 
     sion may be inhibited with the -r option. The -s option prints to 
     the history file instead of standard output; the -u option prints 
     to file descriptor n (n defaults to 1 if omitted); and the -p op- 
     tion prints to the co-process (see Co-processes above). 

     The -R option is used to emulate, to some degree, the BSD echo(1) 
     command which does not process '\' sequences unless the -e option 
     is given. As above, the -n option suppresses the trailing newline. 

printf format [arguments ...] 
     Formatted output. Approximately the same as the printf(1), utili- 
     ty, except it uses the same Backslash expansion and I/O code and 
     does hot handle floating point as the rest of mksh. This is not 
     normally part of mksh; however, distributors may have added this 
     as builtin as a speed hack. Do not use in new code. 
0

爲了提供用於mksh使用內置的printf,mksh需要與printf.c和構建環境變量USE_PRINTF_BUILTIN = 1個需要進行設定,這是不推薦的來構建。然後它會有printf。如果你打算分發這個文件,使它變得可移植,那麼這可能不是你想要做的。相反,我會使用更安全的「busybox printf」。大多數設備都有busybox,但爲了真正使腳本可移植,您需要使用mksh的「打印」功能。