Peter Pan
Peter Pan

Reputation: 21

Shell variable evaluating inside while loop

I am trying to run .sh file to put certificate via keytool command in every jre/lib/security/cacert file described in ~/.java/deployment/deployment.properties file.

But getting error "Keystore parameter must not be empty" in

sudo bash -c keytool -import -v -trustcacerts -alias test-cert -file ./test.cer -keystore ${resultPath};

I already tried use eval and "$()" notation - but this failed too. How can I fix that?

#!/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: Running script through bash -vx ./script.sh show this output:

#!/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

An error line

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

showing because user should enter password for keystore file, but when this script runs, no prompt about entering password is given. How can I fix that?

Upvotes: 2

Views: 270

Answers (1)

You should use double quotes to ensure that the variable expansion ${resultPath} produces a word for the shell.

Thus your critical line should be

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

You could also be interested in :? or :- variable expansion modifiers.

Note It seems to me that the keytool program complains that your file is not valid, maybe your issue has nothing with shell programming.

Upvotes: 1

Related Questions