0

I am using below script to mask a file, I am passing source and destination file as argument.

#!/bin/bash
awk '
    BEGIN {
        IGNORECASE = 1
    }

    $1 ~ /^(self-signed-cert:|encrypted-cert-password:|self-signed-cert:|public-cert:|publicKey:|aes-encrypted-symmetricKey:)$/ {
        gsub( /[^-]/, "X", $2 )
    }
    { print }
' "$1" >"$2"

When i run below command from command script this works fine , and its generating masked file.

./mask.sh /opt/aaa/bbbb/file.yml    /aaa/ccc/conf/msk.yml

But when i try running bash script from java code i get error -

bash.execute(new String[]{"sudo", "/scriptlocation/scripts/mask.sh" + " " + "/opt/aaa/bbbb/file.yml" +" "+ "/aaa/ccc/conf/msk.yml"});

Error : Could not execute bash command. Error: We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things:

  1. Respect the privacy of others.
  2. Think before you type.
  3. With great power comes great responsibility.

sudo: no tty present and no askpass program specified, input:

this is part of implementation of execute() method in Bash class

public int execute(String[] command) {
        int returnCode = 1;
        String strInput = null;
        String strError = null;
        StreamGobbler in = null;
        StreamGobbler err = null;
        try {
            Process p = getRuntime().exec(command);
            in = getStreamGobbler(p.getInputStream(), StreamGobbler.OUTPUT);
            err = getStreamGobbler(p.getErrorStream(), StreamGobbler.ERROR);
            in.start();
            err.start();

            returnCode = p.waitFor();
        }
  • Just curious, are you running as sudo your scripts etc? – RavinderSingh13 Jun 24 '18 at 17:31
  • @RavinderSingh13 yes i am running it as sudo – rupesh kumar Jun 24 '18 at 17:49
  • `bash.execute` - what is `bash` here? – Boris the Spider Jun 24 '18 at 18:11
  • if i am not passing arguments and hardcoding location in script then it works fine. – rupesh kumar Jun 24 '18 at 18:13
  • @BoristheSpider Bash (bash an object )is a utility class here used to run bash script. – rupesh kumar Jun 24 '18 at 18:15
  • Yeah, I assumed it was in `Object` - we are talking about Java after all. My question was more about what it does, how it executes - because concating a giant `String` is almost always wrong. – Boris the Spider Jun 24 '18 at 18:19
  • @BoristheSpiderpublic i have added execute method implementation. It takes input as array. and i using this to execute bash.execute(new String[]{"sudo", "/scriptlocation/scripts/mask.sh" + " " + "/opt/aaa/bbbb/file.yml" +" "+ "/aaa/ccc/conf/msk.yml"}); – rupesh kumar Jun 24 '18 at 18:32
  • @BoristheSpider I tried everything mentioned in linked question. Its not working, Even i am doing same thing as mentioned in liked post. may be i am running the script as "sudo" causing problem. As the directory i am passing is owned by different user. – rupesh kumar Jun 24 '18 at 18:51

0 Answers0