Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert ctrl+d into my linux script?

I want to make the following commands:

cat > template.txt [enter in the terminal] text [Ctrl+d in the terminal]

in a script.

Is there a way to tell the script to do enter\Ctrl d? Is there a way to create a file and write to it in script?

I didn't find anything that worked for me.

Thanks.

like image 972
Gum Bi Avatar asked Sep 17 '26 22:09

Gum Bi


2 Answers

A Here Document is kind of like a script version of what you're talking about, I think, although it is not entirely clear to me from your description.

#!/bin/bash

cat > template.txt <<- EOF
    Here
    is some 
    text.
EOF

Ctrl-D itself is the EOF character, ASCII 4.

like image 113
Jameson Avatar answered Sep 19 '26 18:09

Jameson


When you want an interactieve user enter lines and add them to your file until the user enters an ^D you can use the next script:

echo "Please give input"

while read -r line; do
            echo "Enter next line or ^D"
            echo "${line}" >> template.txt
done
echo "After loop"

You do not have to check for ^D, that will be recognized by read without doing something extra.So you do not need to use CTRL-V CTRL-D in vi.

like image 24
Walter A Avatar answered Sep 19 '26 20:09

Walter A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!