Send keys in shell script

Hi All,

Consider we have script Linux server . But we don't have permission to edit that file.
When run the script, it opens a file as "/tmp/xxx0000 .txt" with few contents. Most of the time the contents doesn't required to modify and they just save and close (:wq!) .
So the script continues and creates "/tmp/xxx000n.txt" file until some n iteration.

How can automatically send " :wq! " key /tmp/xxx0000.txt ' file which presents in active window.

Thanks in advance,

You are using vi / vim in a script?

Hi Neo, "vi" option used in that script.

Hi
Any open file in "vim" generates a "swap" file. This is of course a band-aid

ex -rnes +wq /tmp/.xxx0000.txt.swp

--- Post updated at 12:00 ---

So it will be more correct

ex -rns +wq /tmp/xxx0000.txt
1 Like

Thanks nezabudka. Actually a primary script open a text file from vi command. But end user need close the file manually, than only the primary script resumes execute the next flows. Above command helps to save the file even if it open. Is there a way to send ( :wq ) in current active window ?

the command given above writes the open buffer to a disk. So there is no need for an open window to execute command :w .
Do you need to exit vim?

--- Post updated at 16:53 ---

Maybe the solution is if you run a line of code in a script in the background?

...
vim file &
...

And connect to it when necessary

reptyr $(pidof vim) #install reptyr

Real question being what is the purpose of running :wq inside script ?

You wrote

Can you elaborate on this ?
How does :wq make things work better or worse for file in question ?

There are other tools on your disposal such as lsof or fuser to see if the file is opened.

You can also use screen or tmux to achieve similar hackery, sending commands into sessions.
Check out -X options and examples online.

But try to correct the workflow first and use other tools not editors in scripts in such way...
Can you show the code, perhaps a better way can be devised to make entire processing.

Hope that helps.
Regards
Peasant.

Yes, i need to save the file content as well exit from current vi screen.

Considered the below sample codes did same as the real environment.
In real script i didn't have permission to change the file, so i am not able to change the workflow.
The script do lot of checks and finally create file by using vi command.
Currently the end user must type :wq! to save and exit the file, So the script move on next iteration.
I expected to find some solution to send keys to close that file and complete the script without manual intervention

#!/usr/bin/perl

### script name as test.pl and we doesn't have permission to modify that file.
### considered file1.txt is new file and it default text populated 
system("vi file1.txt");
### User manually save the file by using of :wq!. so it moves to next instruction in script

### considered file2.txt is new file and it default text populated 
system("vi file2.txt");
### User manually save the file by using of :wq!. so it moves to next instruction in script


### iteration going on Nth term
### considered file_n.txt is new file and it default text populated 
system("vi file_n.txt");
### User manually save the file by using of :wq!