Redian新闻
>
奇怪的 printf!! ksh programming
avatar
奇怪的 printf!! ksh programming# Unix - 噫吁兮,危乎高哉
c*t
1
有这么一段code:
#!/bin/ksh
func()
{
printf "%10s|%-10s\n" $x $x
return 0;
}
x=123
echo `func`
其output和
printf "%10s|%-10s" 123 123
不同. 区别在于第一个格式.
大家有什么意见, 赫赫
avatar
c*t
2
echo reformat output to single spaced. I often use it to strip spaces.

【在 c**t 的大作中提到】
: 有这么一段code:
: #!/bin/ksh
: func()
: {
: printf "%10s|%-10s\n" $x $x
: return 0;
: }
: x=123
: echo `func`
: 其output和

avatar
c*t
3
如果将 echo 换成 printf 也不行呀

【在 c*****t 的大作中提到】
: echo reformat output to single spaced. I often use it to strip spaces.
avatar
c*t
4
答案找到
y=`func`
echo $y
这时echo为什么不重新格式化?

【在 c**t 的大作中提到】
: 如果将 echo 换成 printf 也不行呀
avatar
c*t
5

or
echo "`func`"

【在 c**t 的大作中提到】
: 有这么一段code:
: #!/bin/ksh
: func()
: {
: printf "%10s|%-10s\n" $x $x
: return 0;
: }
: x=123
: echo `func`
: 其output和

avatar
c*t
6

Actually, I take back what I said. It was NOT echo doing the formating.
Rather, when you call
echo `func`
the shell first get the results from func which can be 10 10
then, that thing is passed to echo by calling as
echo 10 10
Note that echo only see two arguments, not a formated string. So echo
just print the argument out with space in between. So, one way is to
assign the output to a temp variable in your case y. Then call echo
with double quotes around the variable

【在 c**t 的大作中提到】
: 答案找到
: y=`func`
: echo $y
: 这时echo为什么不重新格式化?

avatar
c*t
7
U R Genius! I got it.
Thank you so much!!!!!!!!!

【在 c*****t 的大作中提到】
:
: Actually, I take back what I said. It was NOT echo doing the formating.
: Rather, when you call
: echo `func`
: the shell first get the results from func which can be 10 10
: then, that thing is passed to echo by calling as
: echo 10 10
: Note that echo only see two arguments, not a formated string. So echo
: just print the argument out with space in between. So, one way is to
: assign the output to a temp variable in your case y. Then call echo

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。