pls help with cat inside script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pls help with cat inside script
# 1  
Old 10-29-2011
pls help with cat inside script

Hi,
Am not very well versed with unix, i wrote a script which will make a new file using cat option like this,


trans.sh
Code:
#!/bin/sh
cat > file1
echo how are you

I wrote only this part since i want to use this concept for something else. What i want to ask is, this script when executed is obviously making file 'file1' which will accept input as what we enter while running script trans.sh with ^D to stop. Ex:- when i run trans.sh it asks for input,lets say i enter "cool" with control+D to stop, so file1 will be made with contents "cool"


Why doesnt file1 has this line 'echo who are you' in it, how can i make script trans.sh recognise the "^D" after line 'echo who are you' inside script
Smilie i tried even writing control+D using backticks after this echo line inside script.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-29-2011 at 06:29 PM.. Reason: code tags, please!
# 2  
Old 10-29-2011
not sure what 'concept' you're after, but... how about:
Code:

#!/bin/sh 
cat > file1 
echo how are you >> file1

# 3  
Old 10-29-2011
Thanks for the reply...yes we can do what you wrote, but is there not any way by which we can make the script recognize "^D" inside the script...

please let me know where am wrong when i say this..when we write cat > file at command pompt it asks for input to be redirected to file1, 7 input is stopped by "^D"


so when am running trans.sh, as contorl reaches word "cat" it asks for input & this input shoul enter file file1 such as

Admin@comp ~
$ ./trans.sh
juliet
caca


Admin@comp ~
$


Here above while executing script its askign for input which is stopped by ^D & file 1 will contain what we entered while executing trans.sh

Admin@comp ~
$ cat file1
juliet
caca

So, might be if we can make trans.sh recognize "^D" inside the script after line 'echo who are you' , it might consider this line 'echo who are you' as input to be redirected in file1
# 4  
Old 10-29-2011
Sounds like something called a "here doc" might be what you are looking for. If you are looking to have a bunch of lines written from the script into a file, you can use this in either Kshell or bash:

Code:
cat <<endKat >/tmp/test-file
this is line one
this is line two
this is line three
endKat

All lines that follow the cat command, until endKat is reached, are written to the named file (/tmp/test-file in this case). I use endKat as the ending marker string as it isn't likely to be in any text that comes before it.

Have a look at the Kshell documentation for more info on here documents.
http://www2.research.att.com/sw/down.../man1/ksh.html

Last edited by agama; 10-29-2011 at 06:59 PM.. Reason: clarification
This User Gave Thanks to agama For This Post:
# 5  
Old 10-29-2011
Quote:
Originally Posted by agama
Sounds like something called a "here doc" might be what you are looking for. If you are looking to have a bunch of lines written from the script into a file, you can use this in either Kshell or bash:

Code:
cat <<endKat >/tmp/test-file
this is line one
this is line two
this is line three
endKat

All lines that follow the cat command, until endKat is reached, are written to the named file (/tmp/test-file in this case). I use endKat as the ending marker string as it isn't likely to be in any text that comes before it.

Have a look at the Kshell documentation for more info on here documents.

Thanks bunch for your reply Smilie , ya similar to this only i wanted...

So, if we give any string which is not present in the file, it will write everything in named file after cat command until EOF is reached? can you also help me more with exactly what "<<" implies?

Thanks once again for your help !

ps: Also, am able to execute this "test-file" being made here without assigning execute permission on this.

Last edited by romsf; 10-29-2011 at 07:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem while displaying(cat) file content inside telnet loop .

Hi Team, Not getting the file output inside my email which i am sending from unix box. . Please refer the below code : #!/bin/sh { sleep 5 echo ehlo 10.56.185.13 sleep 3 echo mail from: oraairtel@CNDBMUREAPZP02.localdomain sleep 3 echo rcpt to: saurabhtripathi@anniksystems.com... (1 Reply)
Discussion started by: tripathi1990
1 Replies

2. Shell Programming and Scripting

Need help with first shell script pls.

Hi, I'm trying to extract information from one file to update another one and am a bit stuck. the first file is made up of tags e.g. <item>a@b.com</item> jksdhfjkdsh sldkjfds l klsjdf <item> c@d.com </item> what i'd like to do is extract the email addresses between these tags,... (6 Replies)
Discussion started by: newb1000
6 Replies

3. 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

4. Shell Programming and Scripting

Pls comment on my script.

Hi guys, hope you scripting gurus here can help me out, the logic in my script somehow not working the way it should, this script part of a bigger backup script suppose to do some checking on the cluster prior to bringing up the package on MC/SG after backend cloning operation, this portion is... (3 Replies)
Discussion started by: sparcguy
3 Replies

5. Shell Programming and Scripting

Script changes required...Pls. help me!!

Hi All, I'm giving input of four variable. I'm using this script for network field, so I'm defining the example on the same way. Example Input: $1 =ind00m1 $2=Gi1/1 $3=10.0.0.1 $4=Connectivity from 1 to 2 Applies to following line: object=$2 msg_grp=SNMP node=$1 msg_text="IF Down $2 $3... (2 Replies)
Discussion started by: ntgobinath
2 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 Dummies Questions & Answers

Using a variable inside a file to cat another.

I have a question to do and it's somewhat confusing. It says, and I quote "Create a file called file_1 with three lines of text in it. Create a shell variable called "f_name", assign it the string "file_1". Use the cat command and the variable "f_name" to display the contents of the file... (3 Replies)
Discussion started by: MaestroRage
3 Replies

8. Shell Programming and Scripting

Pls. Help my script not working as it should

I'm using nested for loops to gather ping IP addresses from a hostlist and also changing the packet size via a packet size list. The final results of (RTT and SDEV) will need to be grep'd out using awk into variables and then printed in columns (the packet sizes going across with IP address going... (1 Reply)
Discussion started by: cocoabeauty1
1 Replies

9. Shell Programming and Scripting

My script does not work - could you pls help?

Hi all, I put together a script that seems not working as I would like to and after spending hours to find the problem I decided to ask your help. The thing I am trying to do is call AWK command on the TRUE branch of if statement. Else branch should only print out a message to screen. This... (1 Reply)
Discussion started by: BearCheese
1 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question