Cat termination in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cat termination in script
# 1  
Old 07-02-2013
Cat termination in script

So, I'm writing a shell script to help automate a process I'm doing. Basically I want to take input from the user for 2 variables, then create a file that consists of:

Code:
Variable1,Variable2
Constant1
Constant2
..
Constant2000

then run an awk script. I'm pretty new to unix though, and so far I've been creating new files by hitting ctrl-d at the end of my input, like so:

Code:
cat > script.awk
{
do stuff
}
(ctrl-d)

My google-fu is failing me. Is there a way I can tell the script "this is where the file you're making ends" without hitting ctrl-d? Really stupid question I know, but hopefully there's a simple answer!
# 2  
Old 07-02-2013
ctrl-d is not a "cat" thing by the way, it's a terminal thing. It forces the terminal to send an end-of-file condition. If you do ctrl-d just at your prompt, it will probably log you out.

What you want sounds like a here-document. You can tell the shell to build a document and feed it into cat.

Code:
cat > outputfile <<EOF
line1
line2
line3
line with $variable in it
and other such stuff
EOF

Note that the ending EOF cannot be indented, it must be at the beginning of the line.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-02-2013
Code:
cat <<EOF > script.awk
{
do stuff
}
EOF

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 07-02-2013
Yeah, I knew ctrl-d wasn't a cat thing. That's why I was pretty embarrassed by this post. But if you can't ask anonymous people on the internet, who can you ask? :P
# 5  
Old 07-02-2013
There are some subtleties with regard to quoting, expansions, and leading tabs. I suggest you carefully read the relevant section of your shell's manual page.

Regards,
Alister
# 6  
Old 07-02-2013
Thanks everybody, that worked like a charm. So while we're here, how should I be telling cat to end files if I'm just doing command line work besides ctrl-d?
# 7  
Old 07-02-2013
If you want to use the terminal to send an end-of-file signal, that's what ctrl-d is for. You might be able to redefine it to another key if you really wanted, but it's what it is for.

If you want a better way to edit files, try an editor like vi, vim, pico, or nano.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Email alert after termination

I am running the gaussian program on UNIX with bash and I want to form a script that will email me once the output life terminates either "normal termination" or "false" I just started learning this last week so could you let me know how to go about this.:b: (13 Replies)
Discussion started by: Jade_Michael
13 Replies

2. Shell Programming and Scripting

Script termination without pressing Enter key[nohup]

I have a script in which we have used nohup. Once script is executed it will be terminated only when enter key is pressed. I want the script to be terminated without pressing enter key nohup imqbrokerd -name user_id port 2>1 1>$home_`date` & I am a newbie to shell, Kindly please help (3 Replies)
Discussion started by: Suganbabu
3 Replies

3. Shell Programming and Scripting

Catching the termination of one script

Hi I have a Shell script that needs to execute a command at the End of the excursion of other script And I cant get a handel On the trap command. And that is if the trap command Is the proper way to go this is a extract of the script MYHOST=`hostname| cut -d. -f1` echo $MYHOST ... (4 Replies)
Discussion started by: Ex-Capsa
4 Replies

4. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

5. Shell Programming and Scripting

random script termination

I'm writing a script to archive data. First, the files are all rsync'd to the archive directory via NFS mounts(I know not the most efficient, but the only choice in this situation), then I use md5sum to validate the transfers. During execution of the script, it will exit for no apparent reason. It... (6 Replies)
Discussion started by: mph
6 Replies

6. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

7. UNIX for Advanced & Expert Users

Child peocess termination.

Hello all, Here is the problem: A ksh script (let's call it abc.sh) gets kicked off from a menu program using "nohup abc.sh &". The process ID of abc.sh can be recieved (pid=$!). abc.sh runs an Oracle PL/SQL script (it creates a child process). In order to stop the abc.sh (and the child)... (5 Replies)
Discussion started by: Shaz
5 Replies

8. UNIX for Dummies Questions & Answers

Abnormal Termination errors

I'm having trouble with Abnormal Termination errors. What are they, what causes them and how can I prevent them from happening? Are they application specific? (2 Replies)
Discussion started by: bialsibub
2 Replies
Login or Register to Ask a Question