LinuxSir.cn,穿越时空的Linuxsir!

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

问一个关于bash脚本的问题

[复制链接]
发表于 2008-2-25 20:34:52 | 显示全部楼层 |阅读模式
#!/bin/bash
read LOCATION
ls $LOCATION
ls ~
ls $HOME

脚本中的ls ~ 和 ls $HOME在运行中能正确显示,但是read读入~或者$HOME,ls $LOCATION这行显示找不到~这个文件,为啥?
发表于 2008-2-25 21:27:41 | 显示全部楼层
因为shell只执行了一次变量值替换,要想得到正确的结果要用eval
回复 支持 反对

使用道具 举报

发表于 2008-2-25 21:29:04 | 显示全部楼层
thunder@thunder:~/shellprogram$ read LOCATION
$HOME
thunder@thunder:~/shellprogram$ eval ls $LOCATION
append.sed  Downloads  MyDownloads  rolo.sh       test      test.c~    thundercron
Desktop     Examples   net2         rolo.sh~      test2     test.sh    workspace
dic.txt     fd         net.sh       share         test2.c~  test.sh~
doc         linux.lnx  pic          shellprogram  test.awk  test.txt~
thunder@thunder:~/shellprogram$
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-2-25 22:01:59 | 显示全部楼层
果然是这样
,谢谢了
回复 支持 反对

使用道具 举报

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

本版积分规则

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