2

I would like to do the following:

  1. Open a terminal.
  2. Change to the directory where target script resides.
  3. Open the text file which provides the input to the target script. Hold/wait until the user has saved and closed the text file.
  4. Execute the target script.

I wrote the following script to do the above:

#!/bin/bash 
echo "hello"
gnome-terminal -x bash -c "cd ~/Scripts;pwd;gedit input.txt;python test.py;exec $SHELL"
echo "good bye"

The above gives me the following output:

user4@user-pc-4:~/Desktop$ ./DAT_run.sh
hello
good bye

And on the new gnome-terminal opened, I see the following message:

/home/user4/Scripts
From python script
From python script
From python script
From python script
From python script

The above means it has executed the python code and my requirements 1,2 and 4 have been met (not the 3rd). I'm unable to hold the gedit window until it has been saved and closed.

What I'm wondering is - why is the gedit command being treated as a background process when I have not indicated any such thing by writing & at the end of the statement? How can I make sure that my gedit is running as a foreground process and requires the user to take control?

What could be going wrong here? I am new to shell scripting and feel like I could very well be missing something here. How can I achieve all my above requirements?

4
  • gedit doesn't have a synchronous mode, you'll have to use another editor. Apr 6, 2017 at 7:28
  • @MichaelHomer, Could you suggest another editor in that case? Apr 6, 2017 at 7:35
  • Try nano instead. It should run in the opened terminal.
    – Mio Rin
    Apr 6, 2017 at 10:03
  • Are you looking at expect command? Apr 6, 2017 at 14:21

1 Answer 1

2

As mentioned in the comments, gedit and possibly other graphical editors might not totally function as expected because it runs asynchronously.

Also, the question gives the impression, but I'm not 100% confident, that you want to use the file as input ("stdin") for the python script. In that case, as also mentioned in the commands, you might want to use expect for that, depending of how complex is your need. expect provides ways to generate the input dynamically, while the input file will be static. Anyway, I'll answer assuming you want a static input and that my understanding of the question is correct.

For that you need to send the contents of the file through pipe to the standard input of python. You would achieve that through the following script (using vim as gedit won't fit your needs):

#!/bin/bash 
echo "hello"
gnome-terminal -x bash -c "cd ~/Scripts; pwd; vim input.txt; cat input.txt | python test.py; exec $SHELL"
echo "good bye"

PS: If I misunderstood what you mean by 'emulating a user at terminal', please tell me so I can edit my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.