2014-09-05 142 views
2

我試圖運行.sh文件,通過keytool命令將證書放入〜/ .java/deployment/deployment.properties文件中描述的每個jre/lib/security/cacert文件中。在循環內部評估shell變量

但得到錯誤「密鑰庫參數不能爲空」在

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" 

UPDATE:通過慶典-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 

顯示,因爲用戶應該密鑰存儲文件輸入密碼,但是當這個腳本運行時,不會提示輸入密碼的提示。我該如何解決這個問題?

+0

用'bash -vx腳本'運行腳本並檢查輸出 – 2014-09-05 19:05:24

+0

不幸,我只能使用sh ./script.sh來運行腳本。 – 2014-09-05 19:24:19

+1

但你的「sh-bang」行是'!#/ bin/bash'。你嘗試過'sh -x。/ script.sh'還是不能使用它?你可以編輯你的腳本或從/ tmp目錄運行它的編輯副本嗎?然後在該行後面的'sudo'行和'set -x'之上添加'set -x'。祝你好運。 – shellter 2014-09-05 19:52:43

回答

1

您應該使用雙引號確保變量擴展${resultPath}爲shell生成一個單詞。

因此您的臨界線應該是

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

您還可能有意:?:-變量擴展修飾符。

注意 在我看來,該keytool程序抱怨你的文件是無效的,也許你的問題沒有任何shell編程。