0

I'm trying to do the following :

cat > somefile "some text" <ctrl+d>; clear; some other program

but without having to press <"ctrl + d"> so that line will create the file and then run some other program. I tried echo "some text" > somefile; but there are too many special chars for that. Anyone know a way around this

Morki
  • 215
  • 1
  • 3
  • 11
  • Your question is unclear; would a here document satisfy your requirement, or do you actually require input to come from the user? Then how do you know when the user is done? However, if a single line of input is required, the `read` command does that. In some versions of the shell, it also has a timeout facility. – tripleee Jun 14 '13 at 13:10

2 Answers2

1

I think what you may be looking for is something along these lines:

pax> cat >tempfile ; tput clear ; someprog
Enter your data here for tempfile
<ctrl-d>
**screen clears and someprog runs**

The end-file CTRL-D isn't part of the command you enter, it's part of the input stream for the cat command.

If you don't want to use the input stream, you're either going to have to work out the echo variant (learn to embrace the wonderful world of shell escapes - you can get around most of them by just using single quotes instead of double ones), or simply create your input file in advance and use something like:

cp sourcefile tempfile ; tput clear ; someprog
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Edited I'm trying to do it so you don't have to press ctrl+d it auto exits on a ; or something. – Morki Jun 14 '13 at 07:04
  • @Morki, I'm not sure how _any_ character is better than CTRL-D since you have to enter extra characters regardless. See the second bit of my answer to do this with no extra user input (other than the creation of `sourcefile` once). – paxdiablo Jun 14 '13 at 07:06
1

If you wish to write some text in somefile in multiple lines or with special characters, you should try this. EOF is treated as a special string here that will terminate cat automatically when found but it could be anything else.

cat > somefile << EOF
some text
EOF

some other program 
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • Actually `EOF` isn't special at all; you can put anything you like after `<<` and the first occurrence of that string alone on a line will terminate the so-called here document. This is a shell facility, not a feature of `cat`. – tripleee Jun 14 '13 at 07:10
  • Yes, I know, I've said it ambiguously, you're absolutely right. => edited. – Maxime Chéramy Jun 14 '13 at 07:12
  • You can do something like cat > somefile << EOF some text EOF; someprog; – Morki Jun 14 '13 at 07:16
  • 1
    It's commonly referred to as [heredoc](http://en.wikipedia.org/wiki/Here_document). – devnull Jun 14 '13 at 07:21