Bash script interacting with application The 2019 Stack Overflow Developer Survey Results Are InProblems copying files with spaces inside the file name in a bash scriptHow to ensure a bash script is executable via web without being in /cgi-bin directory?cpulimit on a bash script which runs other commandsBash root to user. Best in same or separate script?bash - get pid for a script using the script filenameBash script works via terminal but not via main menubash escape exclamation character inside variable with backtickWriting a script to run commands inside a consoleBash Script for filebot, ffmpeg, and rclone — stuckexecute sh script from crontab problem

How to change the limits of integration

What is the best strategy for white in this position?

What is a mixture ratio of propellant?

Why can Shazam do this?

Geography at the pixel level

Deadlock Graph and Interpretation, solution to avoid

Carnot-Caratheodory metric

Evaluating number of iteration with a certain map with While

Is "plugging out" electronic devices an American expression?

If the Wish spell is used to duplicate the effect of Simulacrum, are existing duplicates destroyed?

How to reverse every other sublist of a list?

Feasability of miniature nuclear reactors for humanoid cyborgs

What do hard-Brexiteers want with respect to the Irish border?

Any good smartcontract for "business calendar" oracles?

Is this food a bread or a loaf?

Could JWST stay at L2 "forever"?

Should I write numbers in words or as numerals when there are multiple next to each other?

Unbreakable Formation vs. Cry of the Carnarium

Time travel alters history but people keep saying nothing's changed

Why Did Howard Stark Use All The Vibranium They Had On A Prototype Shield?

What is the motivation for a law requiring 2 parties to consent for recording a conversation

JSON.serialize: is it possible to suppress null values of a map?

Why isn't airport relocation done gradually?

It's possible to achieve negative score?



Bash script interacting with application



The 2019 Stack Overflow Developer Survey Results Are InProblems copying files with spaces inside the file name in a bash scriptHow to ensure a bash script is executable via web without being in /cgi-bin directory?cpulimit on a bash script which runs other commandsBash root to user. Best in same or separate script?bash - get pid for a script using the script filenameBash script works via terminal but not via main menubash escape exclamation character inside variable with backtickWriting a script to run commands inside a consoleBash Script for filebot, ffmpeg, and rclone — stuckexecute sh script from crontab problem



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to automate a scanning proccess for a physical application. My script is



#!/bin/bash

i=1
file="ATLASbins.txt"
while IFS= read line
do
scan=$line
cat test.sh | sed "s/ set vchi 5000/ set vchi $scan/g" > test2.sh
chmod +x test2.sh
bash -x /home/mario/Mine/test2.sh
i=$((i + 1))
done <"$file"


Where test2.sh is another script in which I launch the application in which the scans are made. An example of what goes in the second script is



#!/bin/bash
/home/mario/mg5/bin/mg5_aMC

"import model Implementation"

"generate u++ > l+ l+"

output firstscript$i

set vchi 6500

launch firstscript$i


Where 'import model', 'output' and 'launch' are commands of the application (which runs in the terminal).



What happens is that the commands (inside the application) don't work and I get lines like




PATH/test2.sh: line 5: import model Implementation:
command not found




I have no idea how to do this (which is to write a script that can write the commands to the application) and have tried various different separators, running the test2 script in a different shell and to call it in a new terminal using terminal-gnome. How can I make this work?



Another observation is that I need the variable value of i to be written inside the application. I tried this writing, for instance, 'output firstscript$i', which wouldn't work even if the command worked, I imagine.










share|improve this question



















  • 1





    So, the second scripts starts the mg5_aMC program, and you want the rest of that script sent as input to said program? Or something else? Does the first script have anything to do with the issue at hand?

    – ilkkachu
    Feb 20 at 10:31






  • 1





    The first script seems to try to change things in the second script that isn't even there (the set vchi setting). Is the application, which I presume is mg5_aMC, expecting input on its standard input or by using its command line arguments when starting it? The $i in the second script would be empty since it's an unexported shell variable in the first script only.

    – Kusalananda
    Feb 20 at 10:34











  • @ilkkachu That's exactly it. The first script changes some initial script test.sh to a new form several times, which is to run and write input (which is changing) to the program. Also, there is the fact that I need to access the shell variable $i and use it as input to the program.

    – GaloisFan
    Feb 20 at 10:38











  • @Kusalananda That was my bad: the second script is quite long and I didn't paste the part where the vchi is relevant, which made the question confusing. I edited it to correct this. And what you said lastly is exactly the problem I have, and don't know how to solve, with the $i. As for the question regarding the type of input mg5_aMC is expecting, I don't really understand.

    – GaloisFan
    Feb 20 at 10:41











  • If you ran mg5_aMC from the command line, would it expect the other commands in your second script as input from the keyboard? Would redirecting these command from a text file into mg5_aMC make the program accept them as commands?

    – Kusalananda
    Feb 20 at 10:44


















0















I am trying to automate a scanning proccess for a physical application. My script is



#!/bin/bash

i=1
file="ATLASbins.txt"
while IFS= read line
do
scan=$line
cat test.sh | sed "s/ set vchi 5000/ set vchi $scan/g" > test2.sh
chmod +x test2.sh
bash -x /home/mario/Mine/test2.sh
i=$((i + 1))
done <"$file"


Where test2.sh is another script in which I launch the application in which the scans are made. An example of what goes in the second script is



#!/bin/bash
/home/mario/mg5/bin/mg5_aMC

"import model Implementation"

"generate u++ > l+ l+"

output firstscript$i

set vchi 6500

launch firstscript$i


Where 'import model', 'output' and 'launch' are commands of the application (which runs in the terminal).



What happens is that the commands (inside the application) don't work and I get lines like




PATH/test2.sh: line 5: import model Implementation:
command not found




I have no idea how to do this (which is to write a script that can write the commands to the application) and have tried various different separators, running the test2 script in a different shell and to call it in a new terminal using terminal-gnome. How can I make this work?



Another observation is that I need the variable value of i to be written inside the application. I tried this writing, for instance, 'output firstscript$i', which wouldn't work even if the command worked, I imagine.










share|improve this question



















  • 1





    So, the second scripts starts the mg5_aMC program, and you want the rest of that script sent as input to said program? Or something else? Does the first script have anything to do with the issue at hand?

    – ilkkachu
    Feb 20 at 10:31






  • 1





    The first script seems to try to change things in the second script that isn't even there (the set vchi setting). Is the application, which I presume is mg5_aMC, expecting input on its standard input or by using its command line arguments when starting it? The $i in the second script would be empty since it's an unexported shell variable in the first script only.

    – Kusalananda
    Feb 20 at 10:34











  • @ilkkachu That's exactly it. The first script changes some initial script test.sh to a new form several times, which is to run and write input (which is changing) to the program. Also, there is the fact that I need to access the shell variable $i and use it as input to the program.

    – GaloisFan
    Feb 20 at 10:38











  • @Kusalananda That was my bad: the second script is quite long and I didn't paste the part where the vchi is relevant, which made the question confusing. I edited it to correct this. And what you said lastly is exactly the problem I have, and don't know how to solve, with the $i. As for the question regarding the type of input mg5_aMC is expecting, I don't really understand.

    – GaloisFan
    Feb 20 at 10:41











  • If you ran mg5_aMC from the command line, would it expect the other commands in your second script as input from the keyboard? Would redirecting these command from a text file into mg5_aMC make the program accept them as commands?

    – Kusalananda
    Feb 20 at 10:44














0












0








0








I am trying to automate a scanning proccess for a physical application. My script is



#!/bin/bash

i=1
file="ATLASbins.txt"
while IFS= read line
do
scan=$line
cat test.sh | sed "s/ set vchi 5000/ set vchi $scan/g" > test2.sh
chmod +x test2.sh
bash -x /home/mario/Mine/test2.sh
i=$((i + 1))
done <"$file"


Where test2.sh is another script in which I launch the application in which the scans are made. An example of what goes in the second script is



#!/bin/bash
/home/mario/mg5/bin/mg5_aMC

"import model Implementation"

"generate u++ > l+ l+"

output firstscript$i

set vchi 6500

launch firstscript$i


Where 'import model', 'output' and 'launch' are commands of the application (which runs in the terminal).



What happens is that the commands (inside the application) don't work and I get lines like




PATH/test2.sh: line 5: import model Implementation:
command not found




I have no idea how to do this (which is to write a script that can write the commands to the application) and have tried various different separators, running the test2 script in a different shell and to call it in a new terminal using terminal-gnome. How can I make this work?



Another observation is that I need the variable value of i to be written inside the application. I tried this writing, for instance, 'output firstscript$i', which wouldn't work even if the command worked, I imagine.










share|improve this question
















I am trying to automate a scanning proccess for a physical application. My script is



#!/bin/bash

i=1
file="ATLASbins.txt"
while IFS= read line
do
scan=$line
cat test.sh | sed "s/ set vchi 5000/ set vchi $scan/g" > test2.sh
chmod +x test2.sh
bash -x /home/mario/Mine/test2.sh
i=$((i + 1))
done <"$file"


Where test2.sh is another script in which I launch the application in which the scans are made. An example of what goes in the second script is



#!/bin/bash
/home/mario/mg5/bin/mg5_aMC

"import model Implementation"

"generate u++ > l+ l+"

output firstscript$i

set vchi 6500

launch firstscript$i


Where 'import model', 'output' and 'launch' are commands of the application (which runs in the terminal).



What happens is that the commands (inside the application) don't work and I get lines like




PATH/test2.sh: line 5: import model Implementation:
command not found




I have no idea how to do this (which is to write a script that can write the commands to the application) and have tried various different separators, running the test2 script in a different shell and to call it in a new terminal using terminal-gnome. How can I make this work?



Another observation is that I need the variable value of i to be written inside the application. I tried this writing, for instance, 'output firstscript$i', which wouldn't work even if the command worked, I imagine.







shell-script scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 6 at 17:42









Rui F Ribeiro

42k1483142




42k1483142










asked Feb 20 at 10:21









GaloisFanGaloisFan

1034




1034







  • 1





    So, the second scripts starts the mg5_aMC program, and you want the rest of that script sent as input to said program? Or something else? Does the first script have anything to do with the issue at hand?

    – ilkkachu
    Feb 20 at 10:31






  • 1





    The first script seems to try to change things in the second script that isn't even there (the set vchi setting). Is the application, which I presume is mg5_aMC, expecting input on its standard input or by using its command line arguments when starting it? The $i in the second script would be empty since it's an unexported shell variable in the first script only.

    – Kusalananda
    Feb 20 at 10:34











  • @ilkkachu That's exactly it. The first script changes some initial script test.sh to a new form several times, which is to run and write input (which is changing) to the program. Also, there is the fact that I need to access the shell variable $i and use it as input to the program.

    – GaloisFan
    Feb 20 at 10:38











  • @Kusalananda That was my bad: the second script is quite long and I didn't paste the part where the vchi is relevant, which made the question confusing. I edited it to correct this. And what you said lastly is exactly the problem I have, and don't know how to solve, with the $i. As for the question regarding the type of input mg5_aMC is expecting, I don't really understand.

    – GaloisFan
    Feb 20 at 10:41











  • If you ran mg5_aMC from the command line, would it expect the other commands in your second script as input from the keyboard? Would redirecting these command from a text file into mg5_aMC make the program accept them as commands?

    – Kusalananda
    Feb 20 at 10:44













  • 1





    So, the second scripts starts the mg5_aMC program, and you want the rest of that script sent as input to said program? Or something else? Does the first script have anything to do with the issue at hand?

    – ilkkachu
    Feb 20 at 10:31






  • 1





    The first script seems to try to change things in the second script that isn't even there (the set vchi setting). Is the application, which I presume is mg5_aMC, expecting input on its standard input or by using its command line arguments when starting it? The $i in the second script would be empty since it's an unexported shell variable in the first script only.

    – Kusalananda
    Feb 20 at 10:34











  • @ilkkachu That's exactly it. The first script changes some initial script test.sh to a new form several times, which is to run and write input (which is changing) to the program. Also, there is the fact that I need to access the shell variable $i and use it as input to the program.

    – GaloisFan
    Feb 20 at 10:38











  • @Kusalananda That was my bad: the second script is quite long and I didn't paste the part where the vchi is relevant, which made the question confusing. I edited it to correct this. And what you said lastly is exactly the problem I have, and don't know how to solve, with the $i. As for the question regarding the type of input mg5_aMC is expecting, I don't really understand.

    – GaloisFan
    Feb 20 at 10:41











  • If you ran mg5_aMC from the command line, would it expect the other commands in your second script as input from the keyboard? Would redirecting these command from a text file into mg5_aMC make the program accept them as commands?

    – Kusalananda
    Feb 20 at 10:44








1




1





So, the second scripts starts the mg5_aMC program, and you want the rest of that script sent as input to said program? Or something else? Does the first script have anything to do with the issue at hand?

– ilkkachu
Feb 20 at 10:31





So, the second scripts starts the mg5_aMC program, and you want the rest of that script sent as input to said program? Or something else? Does the first script have anything to do with the issue at hand?

– ilkkachu
Feb 20 at 10:31




1




1





The first script seems to try to change things in the second script that isn't even there (the set vchi setting). Is the application, which I presume is mg5_aMC, expecting input on its standard input or by using its command line arguments when starting it? The $i in the second script would be empty since it's an unexported shell variable in the first script only.

– Kusalananda
Feb 20 at 10:34





The first script seems to try to change things in the second script that isn't even there (the set vchi setting). Is the application, which I presume is mg5_aMC, expecting input on its standard input or by using its command line arguments when starting it? The $i in the second script would be empty since it's an unexported shell variable in the first script only.

– Kusalananda
Feb 20 at 10:34













@ilkkachu That's exactly it. The first script changes some initial script test.sh to a new form several times, which is to run and write input (which is changing) to the program. Also, there is the fact that I need to access the shell variable $i and use it as input to the program.

– GaloisFan
Feb 20 at 10:38





@ilkkachu That's exactly it. The first script changes some initial script test.sh to a new form several times, which is to run and write input (which is changing) to the program. Also, there is the fact that I need to access the shell variable $i and use it as input to the program.

– GaloisFan
Feb 20 at 10:38













@Kusalananda That was my bad: the second script is quite long and I didn't paste the part where the vchi is relevant, which made the question confusing. I edited it to correct this. And what you said lastly is exactly the problem I have, and don't know how to solve, with the $i. As for the question regarding the type of input mg5_aMC is expecting, I don't really understand.

– GaloisFan
Feb 20 at 10:41





@Kusalananda That was my bad: the second script is quite long and I didn't paste the part where the vchi is relevant, which made the question confusing. I edited it to correct this. And what you said lastly is exactly the problem I have, and don't know how to solve, with the $i. As for the question regarding the type of input mg5_aMC is expecting, I don't really understand.

– GaloisFan
Feb 20 at 10:41













If you ran mg5_aMC from the command line, would it expect the other commands in your second script as input from the keyboard? Would redirecting these command from a text file into mg5_aMC make the program accept them as commands?

– Kusalananda
Feb 20 at 10:44






If you ran mg5_aMC from the command line, would it expect the other commands in your second script as input from the keyboard? Would redirecting these command from a text file into mg5_aMC make the program accept them as commands?

– Kusalananda
Feb 20 at 10:44











1 Answer
1






active

oldest

votes


















1














Since you run your test2.sh as a separate shell script, the shell variable i is not available in it. This means that the expansion of $i will be empty in the test2.sh script. You can solve this in two ways:



  1. Make i an environment variable through export i in the first script. This is convenient, but not really a good solution in the general case, as the other script may well want to use its own i variable which may be independent of whatever value the variable is in the calling script.


  2. Give $i on the command line of the test2.sh script when invoking it: test2.sh "$i". This would make it possible to access the value $i in test2.sh as "$1" (the first command line argument).


The second issue is that your mg5_aMC program expects to get input, i.e. the commands that you list in the script. But the way you have entered these in test2.sh means that they would be taken as shell commands. There is nothing in the script that passes the special control commands to the program.



As you can see from the error that you get, it's the shell that complains that the commands can't be found. They are not shell commands, so this is (when you know how things will be interpreted) no surprise.




Assuming that your mg5_aMC program reads from standard input, I would not write out a shell script for each run of the program, but instead provide an input control/command file for it:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
sed -e "s/@scan@/$scan/"
-e "s/@i@/$i/"
input-template.in >input.in

/home/mario/mg5/bin/mg5_aMC <input.in

i=$((i + 1))
done <"$file"


Here, the file input-template.in may look something like



"import model Implementation"
"generate u++ > l+ l+"
output firstscript@i@
set vchi @scan@
launch firstscript@i@


I've chosen to use @thing@ for things I will replace with the sed call in the shell script.



This assumes that the value of $scan contains no characters that would disturb the sed command (such as /).




An alternative, if the control script for your program is reasonably short, is to use "here-document" to feed the control commands into your program:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
/home/mario/mg5/bin/mg5_aMC <<END_INPUT
"import model Implementation"
"generate u++ > l+ l+"
output firstscript$i
set vchi $scan
launch firstscript$i
END_INPUT

i=$((i + 1))
done <"$file"


A here-document is basically a type of redirection of a piece of text which is given, not in a file, but between <<TAG and the ending TAG. The variables in the text would (if written as above) be expanded by the shell before being fed into your command.






share|improve this answer

























  • I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

    – GaloisFan
    Feb 20 at 11:12











  • @GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

    – Kusalananda
    Feb 20 at 11:14












  • @GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

    – Kusalananda
    Feb 20 at 11:24







  • 1





    Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

    – GaloisFan
    Feb 20 at 11:28











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501807%2fbash-script-interacting-with-application%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









1














Since you run your test2.sh as a separate shell script, the shell variable i is not available in it. This means that the expansion of $i will be empty in the test2.sh script. You can solve this in two ways:



  1. Make i an environment variable through export i in the first script. This is convenient, but not really a good solution in the general case, as the other script may well want to use its own i variable which may be independent of whatever value the variable is in the calling script.


  2. Give $i on the command line of the test2.sh script when invoking it: test2.sh "$i". This would make it possible to access the value $i in test2.sh as "$1" (the first command line argument).


The second issue is that your mg5_aMC program expects to get input, i.e. the commands that you list in the script. But the way you have entered these in test2.sh means that they would be taken as shell commands. There is nothing in the script that passes the special control commands to the program.



As you can see from the error that you get, it's the shell that complains that the commands can't be found. They are not shell commands, so this is (when you know how things will be interpreted) no surprise.




Assuming that your mg5_aMC program reads from standard input, I would not write out a shell script for each run of the program, but instead provide an input control/command file for it:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
sed -e "s/@scan@/$scan/"
-e "s/@i@/$i/"
input-template.in >input.in

/home/mario/mg5/bin/mg5_aMC <input.in

i=$((i + 1))
done <"$file"


Here, the file input-template.in may look something like



"import model Implementation"
"generate u++ > l+ l+"
output firstscript@i@
set vchi @scan@
launch firstscript@i@


I've chosen to use @thing@ for things I will replace with the sed call in the shell script.



This assumes that the value of $scan contains no characters that would disturb the sed command (such as /).




An alternative, if the control script for your program is reasonably short, is to use "here-document" to feed the control commands into your program:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
/home/mario/mg5/bin/mg5_aMC <<END_INPUT
"import model Implementation"
"generate u++ > l+ l+"
output firstscript$i
set vchi $scan
launch firstscript$i
END_INPUT

i=$((i + 1))
done <"$file"


A here-document is basically a type of redirection of a piece of text which is given, not in a file, but between <<TAG and the ending TAG. The variables in the text would (if written as above) be expanded by the shell before being fed into your command.






share|improve this answer

























  • I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

    – GaloisFan
    Feb 20 at 11:12











  • @GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

    – Kusalananda
    Feb 20 at 11:14












  • @GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

    – Kusalananda
    Feb 20 at 11:24







  • 1





    Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

    – GaloisFan
    Feb 20 at 11:28















1














Since you run your test2.sh as a separate shell script, the shell variable i is not available in it. This means that the expansion of $i will be empty in the test2.sh script. You can solve this in two ways:



  1. Make i an environment variable through export i in the first script. This is convenient, but not really a good solution in the general case, as the other script may well want to use its own i variable which may be independent of whatever value the variable is in the calling script.


  2. Give $i on the command line of the test2.sh script when invoking it: test2.sh "$i". This would make it possible to access the value $i in test2.sh as "$1" (the first command line argument).


The second issue is that your mg5_aMC program expects to get input, i.e. the commands that you list in the script. But the way you have entered these in test2.sh means that they would be taken as shell commands. There is nothing in the script that passes the special control commands to the program.



As you can see from the error that you get, it's the shell that complains that the commands can't be found. They are not shell commands, so this is (when you know how things will be interpreted) no surprise.




Assuming that your mg5_aMC program reads from standard input, I would not write out a shell script for each run of the program, but instead provide an input control/command file for it:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
sed -e "s/@scan@/$scan/"
-e "s/@i@/$i/"
input-template.in >input.in

/home/mario/mg5/bin/mg5_aMC <input.in

i=$((i + 1))
done <"$file"


Here, the file input-template.in may look something like



"import model Implementation"
"generate u++ > l+ l+"
output firstscript@i@
set vchi @scan@
launch firstscript@i@


I've chosen to use @thing@ for things I will replace with the sed call in the shell script.



This assumes that the value of $scan contains no characters that would disturb the sed command (such as /).




An alternative, if the control script for your program is reasonably short, is to use "here-document" to feed the control commands into your program:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
/home/mario/mg5/bin/mg5_aMC <<END_INPUT
"import model Implementation"
"generate u++ > l+ l+"
output firstscript$i
set vchi $scan
launch firstscript$i
END_INPUT

i=$((i + 1))
done <"$file"


A here-document is basically a type of redirection of a piece of text which is given, not in a file, but between <<TAG and the ending TAG. The variables in the text would (if written as above) be expanded by the shell before being fed into your command.






share|improve this answer

























  • I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

    – GaloisFan
    Feb 20 at 11:12











  • @GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

    – Kusalananda
    Feb 20 at 11:14












  • @GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

    – Kusalananda
    Feb 20 at 11:24







  • 1





    Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

    – GaloisFan
    Feb 20 at 11:28













1












1








1







Since you run your test2.sh as a separate shell script, the shell variable i is not available in it. This means that the expansion of $i will be empty in the test2.sh script. You can solve this in two ways:



  1. Make i an environment variable through export i in the first script. This is convenient, but not really a good solution in the general case, as the other script may well want to use its own i variable which may be independent of whatever value the variable is in the calling script.


  2. Give $i on the command line of the test2.sh script when invoking it: test2.sh "$i". This would make it possible to access the value $i in test2.sh as "$1" (the first command line argument).


The second issue is that your mg5_aMC program expects to get input, i.e. the commands that you list in the script. But the way you have entered these in test2.sh means that they would be taken as shell commands. There is nothing in the script that passes the special control commands to the program.



As you can see from the error that you get, it's the shell that complains that the commands can't be found. They are not shell commands, so this is (when you know how things will be interpreted) no surprise.




Assuming that your mg5_aMC program reads from standard input, I would not write out a shell script for each run of the program, but instead provide an input control/command file for it:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
sed -e "s/@scan@/$scan/"
-e "s/@i@/$i/"
input-template.in >input.in

/home/mario/mg5/bin/mg5_aMC <input.in

i=$((i + 1))
done <"$file"


Here, the file input-template.in may look something like



"import model Implementation"
"generate u++ > l+ l+"
output firstscript@i@
set vchi @scan@
launch firstscript@i@


I've chosen to use @thing@ for things I will replace with the sed call in the shell script.



This assumes that the value of $scan contains no characters that would disturb the sed command (such as /).




An alternative, if the control script for your program is reasonably short, is to use "here-document" to feed the control commands into your program:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
/home/mario/mg5/bin/mg5_aMC <<END_INPUT
"import model Implementation"
"generate u++ > l+ l+"
output firstscript$i
set vchi $scan
launch firstscript$i
END_INPUT

i=$((i + 1))
done <"$file"


A here-document is basically a type of redirection of a piece of text which is given, not in a file, but between <<TAG and the ending TAG. The variables in the text would (if written as above) be expanded by the shell before being fed into your command.






share|improve this answer















Since you run your test2.sh as a separate shell script, the shell variable i is not available in it. This means that the expansion of $i will be empty in the test2.sh script. You can solve this in two ways:



  1. Make i an environment variable through export i in the first script. This is convenient, but not really a good solution in the general case, as the other script may well want to use its own i variable which may be independent of whatever value the variable is in the calling script.


  2. Give $i on the command line of the test2.sh script when invoking it: test2.sh "$i". This would make it possible to access the value $i in test2.sh as "$1" (the first command line argument).


The second issue is that your mg5_aMC program expects to get input, i.e. the commands that you list in the script. But the way you have entered these in test2.sh means that they would be taken as shell commands. There is nothing in the script that passes the special control commands to the program.



As you can see from the error that you get, it's the shell that complains that the commands can't be found. They are not shell commands, so this is (when you know how things will be interpreted) no surprise.




Assuming that your mg5_aMC program reads from standard input, I would not write out a shell script for each run of the program, but instead provide an input control/command file for it:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
sed -e "s/@scan@/$scan/"
-e "s/@i@/$i/"
input-template.in >input.in

/home/mario/mg5/bin/mg5_aMC <input.in

i=$((i + 1))
done <"$file"


Here, the file input-template.in may look something like



"import model Implementation"
"generate u++ > l+ l+"
output firstscript@i@
set vchi @scan@
launch firstscript@i@


I've chosen to use @thing@ for things I will replace with the sed call in the shell script.



This assumes that the value of $scan contains no characters that would disturb the sed command (such as /).




An alternative, if the control script for your program is reasonably short, is to use "here-document" to feed the control commands into your program:



#!/bin/bash

file="ATLASbins.txt"

i=1
while IFS= read -r scan
do
/home/mario/mg5/bin/mg5_aMC <<END_INPUT
"import model Implementation"
"generate u++ > l+ l+"
output firstscript$i
set vchi $scan
launch firstscript$i
END_INPUT

i=$((i + 1))
done <"$file"


A here-document is basically a type of redirection of a piece of text which is given, not in a file, but between <<TAG and the ending TAG. The variables in the text would (if written as above) be expanded by the shell before being fed into your command.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 20 at 11:41

























answered Feb 20 at 10:57









KusalanandaKusalananda

140k17261436




140k17261436












  • I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

    – GaloisFan
    Feb 20 at 11:12











  • @GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

    – Kusalananda
    Feb 20 at 11:14












  • @GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

    – Kusalananda
    Feb 20 at 11:24







  • 1





    Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

    – GaloisFan
    Feb 20 at 11:28

















  • I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

    – GaloisFan
    Feb 20 at 11:12











  • @GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

    – Kusalananda
    Feb 20 at 11:14












  • @GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

    – Kusalananda
    Feb 20 at 11:24







  • 1





    Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

    – GaloisFan
    Feb 20 at 11:28
















I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

– GaloisFan
Feb 20 at 11:12





I will try it as soon as I can, thank you! It is clear why calling the variable $i which did't even existed in that enviroment would not work, but can you say why the command themselves didn't work in a script like that?

– GaloisFan
Feb 20 at 11:12













@GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

– Kusalananda
Feb 20 at 11:14






@GaloisFan When you run a shell script like your test2.sh script, the commands that were supposed to be fed into your program would have been interpreted as shell commands. This is why you get those "command not found" errors. It's the shell rather than your program that receives those as commands. I will add text to the answer about this.

– Kusalananda
Feb 20 at 11:14














@GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

– Kusalananda
Feb 20 at 11:24






@GaloisFan See extra stuff just added. Note that all of this depends on your program actually reading from its standard input stream.

– Kusalananda
Feb 20 at 11:24





1




1





Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

– GaloisFan
Feb 20 at 11:28





Your solution worked perfectly and your explanation clarified a lot. Thank you very much!

– GaloisFan
Feb 20 at 11:28

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501807%2fbash-script-interacting-with-application%23new-answer', 'question_page');

);

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







-scripting, shell-script

Popular posts from this blog

Creating 100m^2 grid automatically using QGIS?Creating grid constrained within polygon in QGIS?Createing polygon layer from point data using QGIS?Creating vector grid using QGIS?Creating grid polygons from coordinates using R or PythonCreating grid from spatio temporal point data?Creating fields in attributes table using other layers using QGISCreate .shp vector grid in QGISQGIS Creating 4km point grid within polygonsCreate a vector grid over a raster layerVector Grid Creates just one grid

What is this called? Old film camera viewer?What makes a good film camera?What to do with an old film camera?What should one look for when buying a used film camera?What is the value and age of this pre-1967 Ricoh 35 mm camera?DSLR recommendation, question about old Canon 35mm film Camera & lensesCan anyone identify the silver rangefinder-style camera in this advertisement?What kind of a Polaroid 600-camera is this?Will an old film camera still work even when not used in a very long time?What is this camera / Can I develop the film?How to fit an action camera into antique (bellows) housing?What to check when buying used and old film bodies?

Why is this plane circling around the Lucknow airport every day?Why do aircraft on Flight Radar 24 jump around randomly sometimes?What airport has this walkway over a taxiway?How does Chicago O'Hare's tower sequence aircraft at peak capacity?Which airport is featured in this Delta commercial?After a crash, for how long is the airport closed?Can a passenger plane stand still in the air, or hover at a fixed location above a ground?What are those trucks towing around, and why?What is this airport outside of Cairo, Egypt?Which US airport has the lowest circling MDH?What is this airport video?