Vi : Is it possible to send ctrl + d signal from a file made with vi and executing it.

Hi Experts,

Is it possible to send ctrl + d signal from a inside a file made with vi, using Ctrl V , Esc and 004 , escape sequence.

Since : [ CTRL-D EOT (End of transmission) ] 004 should exit the script if executed. Is this something possible.

I am trying with vi , I put this code

^[004 

, and trying to execute it but not working:
Getting error, while executing the file:

testfile: ^[004:  not found.

to construct this used:
^[004 ## To do this : 1. vi ,then Esc i , then Esc V Esc , then type 004

But not working.

Thanks,

I don't think you can use ^D here. Why don't you use the exit command to exit a script?

1 Like

It does not make sense to put CTL-D inside a script file for purposes of exiting the script. As previously suggested, use "exit".

If you want to enter CTL-D into the file, using vi, just use CTL-V CTL-D sequence.

When a program is receiving input from stdin, you can enter CTL-D at the beginning of a line to signal "end of file". I think that's what you're getting mixed up. "end of file" for a Unix file is just signaled by end of file (no more content), period. Putting CTL-D in the middle of the file does not signal "end of file". You can verify with a test. Put CTL-D somewhere toward the top of the file, and cat the file. The whole file is shown, not just the part before the CTL-D.

1 Like

Thanks both, ll look for the workaround mentioned .

Can you tell us what exactly you are trying to do?

If you are trying to instruct a script to handle certain task while it is already running, then you can use trap to catch a user-defined signal and perform an action.

For example, here is a script:

#!/bin/ksh

while :
do
        trap "echo Date: $(date)" USR1
        echo "Sleeping for 5 seconds..."
        sleep 5
done

If you run this script on a session and from another session if you send a USR1 signal to the running script PID, the trap argument will be executed:

kill -USR1 <PID>

Modify it as per your requirement. I hope this helps.