Chinaunix

标题: 已经解决.结贴 脚本 shell - printf 打印具有空白字符的变量 (神奇的双引号) [打印本页]

作者: sunzhiguolu    时间: 2015-09-17 00:06
标题: 已经解决.结贴 脚本 shell - printf 打印具有空白字符的变量 (神奇的双引号)
本帖最后由 sunzhiguolu 于 2015-09-17 08:15 编辑

我有一个名为 animal 的脚本文件, 在使用 printf 函数进行打印变量 animal 的时候, 出现了问题. 原因是我的变量的值中包含空格.

  1. #!/bin/bash
  2. animal='sea horse'
  3. printf "animal name is $animal\n";
  4. printf "animal name is %s\n" $animal;
复制代码
下面是输出结果:
animal name is sea horse
animal name is sea
animal name is horse


如果使用带有格式字符串的 printf 函数, 可以避免这种输出吗? 或者有其他解决方法也行!
作者: sunzhiguolu    时间: 2015-09-17 00:17
我已经把包含空格的变量用引号进行圈引, 为啥还是不行?
作者: MMMIX    时间: 2015-09-17 00:23
sunzhiguolu 发表于 2015-09-17 00:06
printf "animal name is %s\n" $animal;


换成 printf "animal name is %s\n" "$animal"
作者: sunzhiguolu    时间: 2015-09-17 00:28
本帖最后由 sunzhiguolu 于 2015-09-17 08:14 编辑

回复 3# MMMIX
真是对您佩服的五体投地, 我把能想到的方法都试过了! 就这个双引号进行圈引没有试!

   
作者: tc1989tc    时间: 2015-09-17 09:53
man bash
printf [-v var] format [arguments]
              Write the formatted arguments to the standard output under the control of the
              format.  The format is a character  string  which  contains  three  types  of
              objects:  plain characters, which are simply copied to standard output, char-
              acter escape sequences, which are converted and copied to the  standard  out-
              put,  and  format  specifications,  each of which causes printing of the next
              successive argument.  In addition  to  the  standard  printf(1)  formats,  %b
              causes printf to expand backslash escape sequences in the corresponding argu-
              ment (except that \c terminates output, backslashes in \', \", and \? are not
              removed,  and octal escapes beginning with \0 may contain up to four digits),
              and %q causes printf to output the corresponding argument in  a  format  that
              can be reused as shell input.

              The  -v  option  causes  the output to be assigned to the variable var rather
              than being printed to the standard output.

             The format is reused as necessary to consume all of the  arguments.   If  the
              format requires more arguments than are supplied, the extra format specifica-
              tions behave as if a zero value or null string, as appropriate, had been sup-
              plied.  The return value is zero on success, non-zero on failure
.
作者: sunzhiguolu    时间: 2015-09-17 10:48
本帖最后由 sunzhiguolu 于 2015-09-17 10:49 编辑

回复 5# tc1989tc
您给我的这段描述, 我翻译了半天也没搞懂这个 -v 选项的确切的意思. 能不能简单的说明一下?

   
作者: sunzhiguolu    时间: 2015-09-17 10:57
本帖最后由 sunzhiguolu 于 2015-09-17 12:27 编辑

回复 5# tc1989tc
按照您提供的这个语法:
printf [-v var] format [arguments]

  1. #!/bin/bash
  2. animal='sea horse'
  3. printf ...

  4. ----------------------------------打印出下面这样的结果-----------------------------------
  5. Animal name is sea horse
复制代码
printf 函数后面的部分咋添啊?
  1. printf -v $animal "Animal name is %s\n";   #啥都不输出
复制代码
-v 选项是不是屏蔽后面的输出啊?
作者: tc1989tc    时间: 2015-09-17 13:22
本帖最后由 tc1989tc 于 2015-09-17 13:22 编辑

回复 7# sunzhiguolu


    -v 是把输出赋值给变量。。不在输出到stdout
作者: tc1989tc    时间: 2015-09-17 13:23
回复 7# sunzhiguolu


     The  -v  option  causes  the output to be assigned to the variable var rather
              than being printed to the standard output.
作者: sunzhiguolu    时间: 2015-09-17 13:39
回复 8# tc1989tc
能不能给个具体的例子?
就是针对我的这个问题, 这个选项如何使用?

   
作者: tc1989tc    时间: 2015-09-17 13:52
  1. animal='sea horse'
  2. $printf -v cc "animal name is %s\n" "$animal"
  3. $echo $cc
  4. animal name is sea horse
复制代码

作者: sunzhiguolu    时间: 2015-09-17 14:02
回复 11# tc1989tc
还有一点不明白, 这个玩意不输出字符串啥时候能用的到啊?

   
作者: sunzhiguolu    时间: 2015-09-17 14:37
回复 9# tc1989tc
长见识了, 原来 -v 选项还有这种功能. 多谢!

   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2