由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Unix版 - 奇怪的 printf!! ksh programming
相关主题
Shell script求教[转载] some body uses DBX?
awk question[转载] the C programming Language by K&R
大侠帮忙看看这段程序,想不通emacs问题求教----有关C程序格式
新手unix shell script debugging help!interview question
[转载] 问个shell怎么写?how to kill a process in emacs?
请教:有关位置参数的问题。问一个Unix Shell Script 的问题
idb problem (转载)A problem about child process.
[转载] Help, about clock().使用 AWK 中的问题
相关话题的讨论汇总
话题: echo话题: printf话题: ksh话题: func话题: 10s
进入Unix版参与讨论
1 (共1页)
c**t
发帖数: 2744
1
有这么一段code:
#!/bin/ksh
func()
{
printf "%10s|%-10s\n" $x $x
return 0;
}
x=123
echo `func`
其output和
printf "%10s|%-10s" 123 123
不同. 区别在于第一个格式.
大家有什么意见, 赫赫
c*****t
发帖数: 1879
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和

c**t
发帖数: 2744
3
如果将 echo 换成 printf 也不行呀

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

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

or
echo "`func`"

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

c*****t
发帖数: 1879
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为什么不重新格式化?

c**t
发帖数: 2744
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

1 (共1页)
进入Unix版参与讨论
相关主题
使用 AWK 中的问题[转载] 问个shell怎么写?
UNIX文件系统一问请教:有关位置参数的问题。
[转载] QUestion about Perlidb problem (转载)
error in my function "write_log"[转载] Help, about clock().
Shell script求教[转载] some body uses DBX?
awk question[转载] the C programming Language by K&R
大侠帮忙看看这段程序,想不通emacs问题求教----有关C程序格式
新手unix shell script debugging help!interview question
相关话题的讨论汇总
话题: echo话题: printf话题: ksh话题: func话题: 10s