How to echo $variable into txt file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to echo $variable into txt file?
# 1  
Old 12-20-2012
How to echo $variable into txt file?

Hello

i tried many times echo $variables into text file with no success

for example:
Code:
echo "#!/bin/sh

BBHTAG=RFOCLT_check          # What we put in bb-hosts to trigger this test
COLUMN=RFOCLT  # Name of the column, often same as tag in bb-hosts
$BBHOME/bin/bbhostgrep $BBHTAG | while read L
do
set $L  # To get one line of output from bbhostgrep
HOSTIP="$1"
MACHINEDOTS="$2"
MACHINE=`echo $2 | $SED -e's/\./,/g'`" > test.sh

can you advice how to do that ?
# 2  
Old 12-20-2012
Hi,

You can escape from variable interpolation using the below options.

Code:
1. Use single quotes for echo.
2. Just escape the '$' like '\$', So that variable wont be interpolated. In fact, You have to escape all the special meaning characters in shell.

Cheers,
RangaSmilie
# 3  
Old 12-20-2012
Quote:
Originally Posted by rangarasan
Hi,

You can escape from variable interpolation using the below options.

Code:
1. Use single quotes for echo.
2. Just escape the '$' like '\$', So that variable wont be interpolated. In fact, You have to escape all the special meaning characters in shell.

Cheers,
RangaSmilie
hello

yes single quotes helps but not all cases
for example this line
Code:
MACHINE=`echo $2 | $SED -e's/\./,/g'`" > test.sh

how to put in single quotes while the line already contain single quotes
also how to escape special character while your have already \ in the code
Smilie
# 4  
Old 12-20-2012
Hey,

You are right. single quotes won't work in all the class. In such cases double quotes would be helpful.

Code:
echo "MACHINE=\`echo \$2 | \$SED -e 's/\./,/g'\`" >
Dont forget to escape back tick(`).

Hope, It helps you.

Cheers,
RangaSmilie
# 5  
Old 12-20-2012
Could you give an example of what you would like the content of the file to look like,,,
# 6  
Old 12-20-2012
Have you tried using a file editor to create your script? When you use
Code:
echo "Whatever you say" >Filename.extension

if there is a double quote INSIDE 'Whatever you say', then you don't get what you want. echo stops copying at the first double quote found and then something happens... What? I don't know for sure. Guess it depends on what comes AFTER that first double quote found inside 'Whatever you say'...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Echo variable contents into a text file...

Hi all, I am trying to create a script to read my Windows UUIDs and create mounts in fstab. I know there are different and maybe even better ways to mount Windows partitions at boot time, but I always manually create them in fstab successfully. I do a clean install of Ubuntu often and would like to... (2 Replies)
Discussion started by: Ian Pride
2 Replies

3. Shell Programming and Scripting

Repeat command with new variable for each line in txt file

Well here is my question. Let's say I have this Script: find /var/mobile/ maxdepth -2 name "$x" >> /"$x".txt The thing is I want to repeat this script and pull out a variable from a text file like this (each line = new variable $x and another run of the whole command) Thanks for... (27 Replies)
Discussion started by: pasc
27 Replies

4. Shell Programming and Scripting

assigning variable in txt file

Hi all, One of my txt file has common format like . And I need to manually assign variable to "/a/b/c/file1/txt" , which has common text before "Calculated summary file:". I wonder if I can use some command to do that for me, that it read the file and check for that comonn text and assign... (2 Replies)
Discussion started by: emily
2 Replies

5. UNIX for Dummies Questions & Answers

Is echo $variable >> text.txt working in MacOSX?

Hi New at this, but want to learn more. I'm trying this as an Shell Command in MacOSX; newdate='<TIME>' echo $newdate >> /Users/ttadmin/Desktop/test.txt And it don't work. But if I just use; echo <TIME> >> /Users/ttadmin/Desktop/test.txt (<TIME> is an variable that one program... (6 Replies)
Discussion started by: jackt
6 Replies

6. Shell Programming and Scripting

cat vs head vs readline get variable from txt file

I have a file with a single filename in it, which I want to assign to a BASH variable, so I've been trying: c=$(head -1 somefile) echo $c which outputs correctly, but them when I do ... somecommand $c it says it can't find the file, is that because it's grabbing the whole line, and... (5 Replies)
Discussion started by: unclecameron
5 Replies

7. Shell Programming and Scripting

Problems reiterating values from txt file into a variable

Hi, I have written a script to retrieve phone numbers from an error log and output the phone numbers into an text file. I then use the list of phone numbers to process each value into a variable so I can run a sql query and update the database. My problem is I can only process the first value... (2 Replies)
Discussion started by: rdr411
2 Replies

8. Shell Programming and Scripting

echo 2 txt files to screen no carraige return

I have two text files, each of then only containing ONE line and NO carraige return or white space at the end...how do I echo both of these text files to the screen without putting an extra line? I want to do this from the command line. file1.txt: this is file1.txt 1 file2.txt: this is... (4 Replies)
Discussion started by: ajp7701
4 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies
Login or Register to Ask a Question