LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1798|回复: 9

echo参数问题

[复制链接]
发表于 2008-11-23 17:38:39 | 显示全部楼层 |阅读模式
下面的脚本中,如果用户输入一个*,则最后两句输出结果不同。不知道是什么原因。

#! /bin/bash

echo -n "Go ahead: "
read firstline
echo "You entered:"$firstline
echo "You entered:" $firstline
发表于 2008-11-28 08:31:18 | 显示全部楼层
Post by Parmenides;1912524
下面的脚本中,如果用户输入一个*,则最后两句输出结果不同。不知道是什么原因。

#! /bin/bash

echo -n "Go ahead: "
read firstline
echo "You entered:"$firstline
echo "You entered:" $firstline


也不明白的说,有高手帮忙解下么?
回复 支持 反对

使用道具 举报

发表于 2008-11-28 09:39:22 | 显示全部楼层
用这个试试:

  1. #! /bin/bash

  2. echo -n "Go ahead: "
  3. read firstline
  4. echo "You entered:""$firstline"
  5. echo "You entered:" "$firstline"
复制代码


变量引用的时候加双引号是个好习惯~
回复 支持 反对

使用道具 举报

发表于 2008-11-28 14:27:33 | 显示全部楼层
嗯,不能用单引号,不然会出大问题!
回复 支持 反对

使用道具 举报

发表于 2008-11-28 15:20:42 | 显示全部楼层
第一個echo 的argument 是變成 "You entered:*"
shell 把*擴張再交給 echo, 如果你的所在文件夾有 "You entered:" 開頭的都會輸出,
沒有就 輸出 "You entered:*"
第二個echo 有兩個argument, "You entered:" 和 "*".
echo 輸出 "You entered:" 和 shell 擴張后的 *, 即所在文件夾的所有文件(.開頭除外)
回复 支持 反对

使用道具 举报

发表于 2008-12-1 12:32:22 | 显示全部楼层
  1. [jimmy@localhost bash]$ cat test_readline.sh
  2. #! /bin/bash
  3. echo -n "Go ahead":
  4. read firstline
  5. echo "You entered: " $firstline
  6. echo "You entered: " $firstline

  7. [jimmy@localhost bash]$ ./test_readline.sh
  8. Go ahead:*
  9. You entered:  test_readline.sh test.sh while.sh
  10. You entered:  test_readline.sh test.sh while.sh

  11. [jimmy@localhost bash]$


复制代码


the same!
回复 支持 反对

使用道具 举报

发表于 2008-12-1 17:06:08 | 显示全部楼层
Post by future_god;1917135
  1. [jimmy@localhost bash]$ cat test_readline.sh
  2. #! /bin/bash
  3. echo -n "Go ahead":
  4. read firstline
  5. echo "You entered: " $firstline
  6. echo "You entered: " $firstline

  7. [jimmy@localhost bash]$ ./test_readline.sh
  8. Go ahead:*
  9. You entered:  test_readline.sh test.sh while.sh
  10. You entered:  test_readline.sh test.sh while.sh

  11. [jimmy@localhost bash]$


复制代码


the same!


第一个 echo "You entered: " 里面多了一个空格~
回复 支持 反对

使用道具 举报

发表于 2008-12-2 12:35:16 | 显示全部楼层
  1. [jimmy@localhost bash]$ cat test_readline.sh
  2. #! /bin/bash
  3. echo -n "Go ahead":
  4. read firstline
  5. echo "You entered:"[$firstline]
  6. echo "You entered:"[$firstline]

  7. [jimmy@localhost bash]$ ./test_readline.sh
  8. Go ahead:*
  9. You entered:[*]
  10. You entered:[*]
  11. [jimmy@localhost bash]$
复制代码



回复 支持 反对

使用道具 举报

发表于 2008-12-3 10:25:48 | 显示全部楼层
LS想说明什么问题?
回复 支持 反对

使用道具 举报

发表于 2008-12-4 16:40:56 | 显示全部楼层
Post by 茶飯不思;1915635
第一個echo 的argument 是變成 "You entered:*"
shell 把*擴張再交給 echo, 如果你的所在文件夾有 "You entered:" 開頭的都會輸出,
沒有就 輸出 "You entered:*"
第二個echo 有兩個argument, "You entered:" 和 "*".
echo 輸出 "You entered:" 和 shell 擴張后的 *, 即所在文件夾的所有文件(.開頭除外)


受教了,谢谢。
又看了下楼主的帖子,才发现第二行有个空格。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表