2

I am using a bash file to automate a process that I do in the terminal. Part of it is calling a function ~/Executables/postprocessor that asks for an input MyFileName. In the terminal, I do it like this, and works fine:

[user SimulationFolder]$ ~/Executables/postprocessor
Project name = first part of filenames >
MyFileName

When I put it in bash like:

#!/bin/bash
source ~/bin/activate_enviroment.sh

~/Executables/postprocessor
MyFileName

I get the error MyFileName: command not found, so MyFileName is interpreted as a bash command instead of the input for the function.

How do I make the bash introduce this input? (note that this is NOT a USER input, I know the value every time, and should be done directly from the bash)

I also tried

#!/bin/bash
source ~/bin/activate_enviroment.sh

~/Executables/postprocessor MyFileName

And the postprocessor simply didn't run because it didn't receive an input

Mr.Grasses
  • 21
  • 3
  • 3
    Just to be clear, I want to confirm that you're asking about bash (which is Linux-exclusive) and not batch (which is Windows-exclusive). Because you've written a bash script and gotten a bash error, but you've said the word "batch" multiple times and used the [batch-file] tag. – SomethingDark May 24 '23 at 11:00
  • 1
    You can try `~/Executables/postprocessor <<<'MyFileName'` – Fravadona May 24 '23 at 12:47
  • I think the syntax you probably want is "~/Executables/postprocessor $MyFileName" in order to correctly get the file name value. Also, if you want to see what the script is parsing in more detail, you can use "#!/bin/bash -x" at the start of your script – gremto May 24 '23 at 14:42
  • There are already 2 answers in the comments: the page provided via the link is listing several possibilities. My pref would have been ```( sleep 2 ; printf 'MyFilename' ) | ~/Executables/postprocessor``` – Jay jargot May 24 '23 at 15:05
  • 1
    @Jayjargot, ...what's the point of the `sleep`? And that printf doesn't provide a trailing newline; assuming someone typing at a prompt presses enter after it, you want `printf '%s\n' 'MyFilename' | ...`; that also lets you provide multiple lines of input (`printf '%s\n' 'Line One' 'Line Two'`) – Charles Duffy May 24 '23 at 15:10
  • 1
    @Mr.Grasses, mind, you _should_ modify your `postprocessor` to read its argv and, if it finds a name on the argument list, not prompt from stdin; that's proper practice, though as you've shown us it isn't written to follow that practice now. – Charles Duffy May 24 '23 at 15:16
  • @CharlesDuffy: we do not know the command's implementation. sometimes the standard input is cleared before a read (so that the string is lost) isn't it?, or sometimes the standard input is read after a delay?. To cope with these situations, a ```sleep``` could make the trick. – Jay jargot May 24 '23 at 15:46
  • 1
    @Jayjargot, those are very exceptional situations; well-behaved software doesn't do those things (in part _because_ that kind of practice would prevent that software from being able to read from a pipe, FIFO, etc); and even when it _is_ done, a sensible implementation will check `isatty()` and only go ahead with the clear operation when input is connected to a terminal as opposed to a file/FIFO/socket/etc. I'm not going to say never _ever_ necessary, but I can comfortably say that it's so rare that adding latency/runtime _just in case_ it might happen isn't sensible. – Charles Duffy May 24 '23 at 16:09

0 Answers0