Send keys in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send keys in shell script
# 1  
Old 02-05-2020
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,

Moderator's Comments:
Mod Comment Please do wrap your samples in CODE TAGS and small commands in iCODE TAGS as per forum rules.

Last edited by RavinderSingh13; 02-05-2020 at 08:32 AM..
# 2  
Old 02-05-2020
You are using vi / vim in a script?
# 3  
Old 02-06-2020
Hi Neo, "vi" option used in that script.
# 4  
Old 02-07-2020
Hi
Any open file in "vim" generates a "swap" file. This is of course a band-aid
Code:
ex -rnes +wq /tmp/.xxx0000.txt.swp

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

So it will be more correct
Code:
ex -rns +wq /tmp/xxx0000.txt


Last edited by nezabudka; 02-07-2020 at 04:33 AM..
This User Gave Thanks to nezabudka For This Post:
# 5  
Old 02-07-2020
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 ?
# 6  
Old 02-07-2020
Quote:
Originally Posted by k_manimuthu
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?
Code:
...
vim file &
...

And connect to it when necessary
Code:
reptyr $(pidof vim) #install reptyr

# 7  
Old 02-07-2020
Real question being what is the purpose of running :wq inside script ?

You wrote
Quote:
.....
Above command helps to save the file even if it open.
.....
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

To send a mail through shell script

I want to send a mail through shell script,If it is possible Please give me a code. mail id : upload.xxx@example.com (8 Replies)
Discussion started by: kannansoft1985
8 Replies

3. UNIX for Dummies Questions & Answers

How to send e-mail from shell script ( C shell )?

Hi , How to send e-mail from shell script ( C shell ) . Mailx command is not working ( It didn't giving error also ). Please help me (2 Replies)
Discussion started by: arukuku
2 Replies

4. Shell Programming and Scripting

Shell script for SFTP using Public and private keys

Hi all, I needed a shell script for file transfering using public/private keys for authentication. Could you please help me out on this? A procedure to write a shell script is enough. Thanks in advance. Regards. Vidya N (8 Replies)
Discussion started by: Vidya N
8 Replies

5. Shell Programming and Scripting

How to send email through shell script

Hi All, I am new to the unix , i have to deliver one script very urgently I have to write a shell script where i have i want to send email to specific email id in this script i want FROM to be parameterized and stored in a variable TO to be parameterized and stored in a variable... (3 Replies)
Discussion started by: nileshbhawsar
3 Replies

6. Shell Programming and Scripting

within shell script send expect and if else

Hi, I have written one shell script , using that i am able to connect to remote machine but i have to #!/usr/bin/expect -f set address set username set password set OOLpath set dbusername set dbpasswd set tnsname set recdbusername set recdbpasswd set rectnsname spawn ssh... (2 Replies)
Discussion started by: mnmonu
2 Replies

7. Shell Programming and Scripting

within shell script send expect and if else

Hi I have written one shell script , using that i am able to connect to remote machine but i have to #!/usr/bin/expect -f set address set username set password set OOLpath set dbusername set dbpasswd set tnsname set recdbusername set recdbpasswd set rectnsname spawn ssh... (1 Reply)
Discussion started by: mnmonu
1 Replies

8. Shell Programming and Scripting

[Bash] Send Keys help

Hi all, I have programmed in VB before, and have recently moved on to Bash. In VB there is a command: SendKeys {F12} > "Show Help" SendKeys {Y} > "Answer Yes" Is there any way of doing this in Bash? (I need to send the Ctrl key into the script?) Thanks in advance (3 Replies)
Discussion started by: mikejreading
3 Replies

9. Shell Programming and Scripting

Shell script to send a mail

Hi , I need to prepare a script which will check my database with specific to particluar table. If the row count exceeds a certain limit, i need to send a mail to a set of Recipients. Being new to unix, please guide me to complete this task. Advance thanks, Sekar. (4 Replies)
Discussion started by: Sekar1
4 Replies

10. Shell Programming and Scripting

Send e-mail in Shell script

How to send an error mail from a shell script e.g. mail destination_adr@blabla.int "Message : here an error message " thanks, Ann. (1 Reply)
Discussion started by: annelisa
1 Replies
Login or Register to Ask a Question