Shell变量评估内部while循环

时间:2014-09-05 18:52:33

标签: shell

我正在尝试运行.sh文件,通过keytool命令将证书放在〜/ .java / deployment / deployment.properties文件中描述的每个jre / lib / security / cacert文件中。

但是

中的错误“Keystore参数不能为空”
sudo bash -c keytool -import -v -trustcacerts -alias test-cert -file ./test.cer -keystore ${resultPath};

我已经尝试过使用eval和“$()”符号 - 但这也失败了。我该如何解决这个问题?

#!/bin/bash

PATTERN=deployment\.javaws\.jre\.[0-9]*\.path
FILE=~/.java/deployment/deployment.properties
sep='='
trail=lib/security/cacerts

#Traverse file line by line
while read line ; do

  #If line matches pattern
  if printf %s\\n "${line}" | grep -q "${PATTERN}"; then
    case $line in 
      (*"$sep"*)

        #Process line to get path for ../jre/lib/security/cacert file
        after=${line#*"$sep"};
        resultPath=${after%????????}${trail};

        #This fails : ${resultPath} somehow is empty
        sudo bash -c keytool -import -v -trustcacerts -alias test-cert -file ./test.cer -keystore ${resultPath};

      ;;
      (*)
      ;;
    esac
  fi
done < "$FILE"

更新:通过bash -vx ./script.sh运行脚本显示此输出:

#!/bin/bash
PATTERN=deployment\.javaws\.jre\.[0-9]*\.path
+ PATTERN='deployment.javaws.jre.[0-9]*.path'
FILE=~/.java/deployment/deployment.properties
+ FILE=/home/sanya/.java/deployment/deployment.properties
sep='='
+ sep==
trail=lib/security/cacerts
+ trail=lib/security/cacerts

#Traverse file line by line
while read line ; do

  #If line matches pattern
  if printf %s\\n "${line}" | grep -q "${PATTERN}"; then
    case $line in 
      (*"$sep"*)

        #Process line to get path for ../jre/lib/security/cacert file
        after=${line#*"$sep"};
        resultPath=${after%????????}${trail};

        #This fails : ${resultPath} somehow is empty
        sudo keytool -importcert -v -trustcacerts -alias test -file ./test.cer -keystore ${resultPath};
      ;;
      (*)
      ;;
    esac
  fi
done < "$FILE"
+ read line
+ printf '%s\n' '#deployment.properties'
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ read line
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ printf '%s\n' '#Sat Sep 06 10:48:49 MSK 2014'
+ read line
+ printf '%s\n' deployment.modified.timestamp=1409986129309
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ read line
+ printf '%s\n' deployment.version=7.21
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ read line
+ printf '%s\n' deployment.browser.path=/usr/bin/firefox
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ read line
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ printf '%s\n' '#Java Deployment jre'\''s'
+ read line
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ printf '%s\n' '#Sat Sep 06 10:48:49 MSK 2014'
+ read line
+ printf '%s\n' deployment.javaws.jre.0.registered=true
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ read line
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ printf '%s\n' deployment.javaws.jre.0.platform=1.7
+ read line
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ printf '%s\n' deployment.javaws.jre.0.osname=Linux
+ read line
+ grep -q 'deployment.javaws.jre.[0-9]*.path'
+ printf '%s\n' deployment.javaws.jre.0.path=/usr/lib/jvm/java-7-oracle/jre/bin/java
+ case $line in
+ after=/usr/lib/jvm/java-7-oracle/jre/bin/java
+ resultPath=/usr/lib/jvm/java-7-oracle/jre/lib/security/cacerts
+ sudo keytool -importcert -v -trustcacerts -alias test -file ./test.cer -keystore /usr/lib/jvm/java-7-oracle/jre/lib/security/cacerts
Enter keystore password:  keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect
java.io.IOException: Keystore was tampered with, or password was incorrect
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:772)
    at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:55)
    at java.security.KeyStore.load(KeyStore.java:1214)
    at sun.security.tools.KeyTool.doCommands(KeyTool.java:885)
    at sun.security.tools.KeyTool.run(KeyTool.java:340)
    at sun.security.tools.KeyTool.main(KeyTool.java:333)
Caused by: java.security.UnrecoverableKeyException: Password verification failed
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:770)
    ... 5 more
+ read line

错误行

Enter keystore password:  keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect

显示因为用户应输入密钥库文件的密码,但是当此脚本运行时,不会提示输入密码。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您应该使用双引号来确保变量扩展${resultPath}为shell生成一个单词。

因此你的关键线应该是

sudo keytool -importcert -v -trustcacerts -alias test -file ./test.cer -keystore "${resultPath}";

您可能还对:?:-变量扩展修饰符感兴趣。

注意 在我看来,keytool程序抱怨你的文件无效,也许你的问题与shell编程无关。