0

I am wondering how I can validate user input, to check if it is a valid bash command - in terms of syntax? I guess it is not possible to create regex for that. Are there any libraries to handle this?

Thanks

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
uksz
  • 18,239
  • 30
  • 94
  • 161
  • Just show actual error(stderr) if command failed, while execution. – vp_arth Jun 09 '16 at 06:13
  • The issue is, that I want to validate on the front end. – uksz Jun 09 '16 at 06:13
  • Almost all character sequences can be interpreted as valid bash command – vp_arth Jun 09 '16 at 06:14
  • 1
    Valid related to what? It can be valid on one system and invalid on another. Is it valid when it produces an error output? – Günter Zöchbauer Jun 09 '16 at 06:17
  • @GünterZöchbauer, valid in terms of a syntax. I will correct it in the question, to make it clearer. – uksz Jun 09 '16 at 06:20
  • You can try a combination of http://stackoverflow.com/questions/171924/how-do-i-check-syntax-in-bash-without-running-the-script and http://unix.stackexchange.com/questions/38849/is-there-an-online-bash-script-testing-site – Günter Zöchbauer Jun 09 '16 at 06:24

1 Answers1

3

You can't. There is no practical way to validate a shell command without running it.

  • thanks for your answer. I will leave the question open for couple of hours, and if nothing pops out, I will mark it as correct. – uksz Jun 09 '16 at 06:17
  • This is right. A shell command can only be valid according to the shell itself. Another layer cannot tell if it *will* be valid in that shell. The best you can do is to inspire you from most common keywords. Still, it will be a poor approximation of what the shell can and cannot understand. – Frederik.L Jun 09 '16 at 06:25