How to find all interactive commands in a shell script using java?
I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read
, designed to capture user input and I think that it is the only one.
I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))
).
Here is my Validator class:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Validator {
Script script;
String interactiveCommands = {"read"};
public Validator(Script script) {
this.script = script;
}
public void validateInteractiveCommands() {
for (String string : interactiveCommands) {
streamService(string);
}
}
private void streamService(String string) {
try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
stream.filter(lines -> lines.startsWith(string))
.forEach(this::printFound);
} catch (IOException e) {
e.printStackTrace();
}
}
private void printFound(String string) {
System.out.println("Found an interactive command: " + string);
}
}
But in this case I only capture read when it is at the beggining of a line.
And in this case:
if true ; then
read a
fi
it fails to capture read because of 4 spaces.
Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?
java string bash lambda find
add a comment |
I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read
, designed to capture user input and I think that it is the only one.
I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))
).
Here is my Validator class:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Validator {
Script script;
String interactiveCommands = {"read"};
public Validator(Script script) {
this.script = script;
}
public void validateInteractiveCommands() {
for (String string : interactiveCommands) {
streamService(string);
}
}
private void streamService(String string) {
try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
stream.filter(lines -> lines.startsWith(string))
.forEach(this::printFound);
} catch (IOException e) {
e.printStackTrace();
}
}
private void printFound(String string) {
System.out.println("Found an interactive command: " + string);
}
}
But in this case I only capture read when it is at the beggining of a line.
And in this case:
if true ; then
read a
fi
it fails to capture read because of 4 spaces.
Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?
java string bash lambda find
add a comment |
I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read
, designed to capture user input and I think that it is the only one.
I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))
).
Here is my Validator class:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Validator {
Script script;
String interactiveCommands = {"read"};
public Validator(Script script) {
this.script = script;
}
public void validateInteractiveCommands() {
for (String string : interactiveCommands) {
streamService(string);
}
}
private void streamService(String string) {
try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
stream.filter(lines -> lines.startsWith(string))
.forEach(this::printFound);
} catch (IOException e) {
e.printStackTrace();
}
}
private void printFound(String string) {
System.out.println("Found an interactive command: " + string);
}
}
But in this case I only capture read when it is at the beggining of a line.
And in this case:
if true ; then
read a
fi
it fails to capture read because of 4 spaces.
Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?
java string bash lambda find
I want to write a java function, that will scan a bash file and find if there are any commands that require user input. I know that there is a command read
, designed to capture user input and I think that it is the only one.
I wrote a Validator class that takes a script (simple class that just loads a content of a script to a String variable using Files.readAllBytes(Paths.get(path))
).
Here is my Validator class:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class Validator {
Script script;
String interactiveCommands = {"read"};
public Validator(Script script) {
this.script = script;
}
public void validateInteractiveCommands() {
for (String string : interactiveCommands) {
streamService(string);
}
}
private void streamService(String string) {
try (Stream<String> stream = Files.lines(Paths.get(script.getPath()))) {
stream.filter(lines -> lines.startsWith(string))
.forEach(this::printFound);
} catch (IOException e) {
e.printStackTrace();
}
}
private void printFound(String string) {
System.out.println("Found an interactive command: " + string);
}
}
But in this case I only capture read when it is at the beggining of a line.
And in this case:
if true ; then
read a
fi
it fails to capture read because of 4 spaces.
Is there any simple way that I can handle it using stream or lambdas? Or should I switch to regexp?
java string bash lambda find
java string bash lambda find
asked Nov 22 '18 at 7:28
CrystalzordCrystalzord
559
559
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
"I know that there is a command read, designed to capture user input and I think that it is the only one."
In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:
rm -i *
cat > file
Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:
#!/bin/sh
echo running "$@"
"$@"
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
1
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425844%2fhow-to-find-all-interactive-commands-in-a-shell-script-using-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
"I know that there is a command read, designed to capture user input and I think that it is the only one."
In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:
rm -i *
cat > file
Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:
#!/bin/sh
echo running "$@"
"$@"
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
1
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
add a comment |
"I know that there is a command read, designed to capture user input and I think that it is the only one."
In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:
rm -i *
cat > file
Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:
#!/bin/sh
echo running "$@"
"$@"
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
1
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
add a comment |
"I know that there is a command read, designed to capture user input and I think that it is the only one."
In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:
rm -i *
cat > file
Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:
#!/bin/sh
echo running "$@"
"$@"
"I know that there is a command read, designed to capture user input and I think that it is the only one."
In fact, it is impossible to tell if a command in a shell script is going to capture user input. Here are some examples:
rm -i *
cat > file
Indeed, it is (in general) impossible to tell which commands might be executed by a shell script. For example:
#!/bin/sh
echo running "$@"
"$@"
answered Nov 22 '18 at 7:45
Stephen CStephen C
515k69563920
515k69563920
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
1
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
add a comment |
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
1
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
Indeed You are right. I did not think about it earlier. So it seems that the only way to assume that the script will run in a non-interactive mode is closing the stdin right?
– Crystalzord
Nov 22 '18 at 8:09
1
1
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
Even that is not sufficient. A program can (attempt to) open "/dev/tty" and read from there.
– Stephen C
Nov 22 '18 at 9:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425844%2fhow-to-find-all-interactive-commands-in-a-shell-script-using-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown