LinuxSir.cn,穿越时空的Linuxsir!

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

请教大虾,shell怎么把参数加引号?

[复制链接]
发表于 2006-3-31 15:37:35 | 显示全部楼层 |阅读模式
请教大虾我现在遇到这样一个问题,
  在传参数时,遇到带空格的参数我加上引号后,shell自动把我加的引号去掉了传给java程序,导致出错。
原来如下:test.sh
------------------------------------------------------------
Commandstr=""
   until [ $# -eq 0 ]
   do
        Commandstr="$Commandstr $1"
        echo $Commandstr
        shift
   done
  
   #Run the dpmhp command
   "$JAVA_HOME"/bin/java -jar $JarFile $Commandstr


--------------------------------------------------------------------

我执行时 如下:
〉test.sh export -c rp3410 -outfile "c:\program file\peng"

结果系统自动把我的c:\program file\peng引号去掉了(虽然他知道那只是一个参数)再传给程序,我希望系统保留我的引号,直接传给JAVA程序。大家懂了吗?

谢谢指点!!!!
发表于 2006-4-2 22:26:41 | 显示全部楼层
把这一行:
"$JAVA_HOME"/bin/java -jar $JarFile $Commandstr
改为:
"$JAVA_HOME"/bin/java -jar "$JarFile" "$Commandstr"
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-3 12:57:54 | 显示全部楼层
试过了,不行.谢谢。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-3 12:58:00 | 显示全部楼层
试过了,不行.谢谢。
回复 支持 反对

使用道具 举报

发表于 2006-4-3 23:53:00 | 显示全部楼层
  1. test.sh export -c rp3410 -outfile '"c:\program file\peng"'
  2. export
  3. export -c
  4. export -c rp3410
  5. export -c rp3410 -outfile
  6. export -c rp3410 -outfile "c:\program file\peng"
  7. /lib/jre1.5.0_06/bin/java -jar export -c rp3410 -outfile "c:\program file\peng"
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-4-4 00:17:54 | 显示全部楼层
Can you run your cmd by using
  1. sh -xv script
复制代码
and paste the detail execution processes out? It will be easiler to see how your script is processed. My previous reply will just show the correct output, but when you take the result to java, shell will process the arguments again and take off the "".  You might have to try
  1. '"c:\\program\ file\\peng"'
复制代码
, so when the second time processing, \ and space will be treated literally.
回复 支持 反对

使用道具 举报

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

本版积分规则

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