Appending data into a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending data into a variable
# 1  
Old 03-08-2007
Appending data into a variable

Hi,

I would like to know if it's possible to append data into a variable, rather than into a file. Although I can write information into a temporary file in /tmp, I'd rather if possible write into a variable, as I don't like the idea that should my script fail, I'll be polluting the server with messy temp files. Currently my script has a for loop containing:

echo "$line" >> $tmpdir

where $tmpdir represents a filename. Is there a way round this?
# 2  
Old 03-08-2007
Code:
str="${str}
${line}"

# 3  
Old 03-08-2007
you could use the read statement:

Code:
echo "HELLO" |read hello
echo $hello

Note: read command Reads one line only from standard input
# 4  
Old 03-08-2007
Quote:
Originally Posted by ahmedwaseem2000
you could use the read statement:

Code:
echo "HELLO" |read hello
echo $hello

Note: read command Reads one line only from standard input
That will fail in most shells, because each element of a pipeline is executed in a subshell.
# 5  
Old 03-08-2007
Quote:
Originally Posted by michaeltravisuk
Hi,

I would like to know if it's possible to append data into a variable, rather than into a file. Although I can write information into a temporary file in /tmp, I'd rather if possible write into a variable, as I don't like the idea that should my script fail, I'll be polluting the server with messy temp files. Currently my script has a for loop containing:

echo "$line" >> $tmpdir

where $tmpdir represents a filename. Is there a way round this?
If all you are doing is echoing a value, just assign the value to a variable instead:

Code:
var=$line

If it's a real command, use command substitution, e.g.:

Code:
var=$( ls -l "$FILE" )

# 6  
Old 01-31-2009
Spp

you can var=`echo "$var $tmpdir"` in the loop
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with data appending to a file

Hi I have a file called text.txt contains x y z when i run a command i will get output like below x 20 z 30 i want to insert x, z value in text.txt file and should be like this x 20 y 0 z 30 can anyone help me please? (1 Reply)
Discussion started by: siva kumar
1 Replies

2. Shell Programming and Scripting

appending data from similar files

I am familiar with scripting, but I am trying to see if there is an easy way to append files from similar files into one file. For example, if there is file1_20121201, file1_20121202, file1_20121203, file2_20121201, file2_20121202, file2_20121203 I want to be able to combine all the data from... (3 Replies)
Discussion started by: mrbean1975
3 Replies

3. Shell Programming and Scripting

Appending value to variable

I have a Query! by using command cat >> file1 we can append data's to a already existing file, Similarly is it possible to append a data to a variable. Thanks in Advance!! (2 Replies)
Discussion started by: gwgreen1
2 Replies

4. Shell Programming and Scripting

appending data to last line of file

A friend contacted me recently with an interesting question. We got something worked out, but I'm curious what answers you all can come up with. Given a shell script (in bash) that processes a bunch of data and appends it to a file, how would you append the date, time, and a filename to the... (6 Replies)
Discussion started by: malcolmpdx
6 Replies

5. Shell Programming and Scripting

appending previous data.

I have on file abc.txt abc.txt: 20090807 Now I want to delete empty lines which has tap/whit spaces from abc.txt .and store the date value in the file into variable.some processs will update the this file with some date . if the process updtes thiis file with empty string , write the... (3 Replies)
Discussion started by: Gopal_Engg
3 Replies

6. Shell Programming and Scripting

appending data to file

Hi. I wrote a very simple script and it doesn't work :( It is supposed to go to a certain directory, execute some command and append the output to the file "expo.dat" what it does is that it writes to the file only one entery. I dont know if Im using the write synthax for "append". Here is... (3 Replies)
Discussion started by: Enigma08
3 Replies

7. Shell Programming and Scripting

Appending to a variable?

Hey, I'm creating a custom useradd script, and I'm giving the option to add secondary groups. Basically what I want to do is ask for the name of the group, you type in the group you want to add, it assigns that group name to the variable $sgroup. Then the scripts asks if you want add another. If... (0 Replies)
Discussion started by: paqman
0 Replies

8. Programming

appending space in binary data in c

Hi ... I am having a string in the buffer .. It is binary data it may contain space . When i try to append a space in using strcpy it is taking the inbetween \n as the end and appending space .. How to append space in the binary data in C ?? please let me know Thanks in advance, Arun. (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

9. Shell Programming and Scripting

Appending data at the first and last line of a file

Hi, Am trying to write a shell script which will append a header and a footer to an existing file. Header will contain details like the current date while the footer will contain the no: of records listed in the file. I know we can use the CAT command, but i have no clue abt the syntax to... (4 Replies)
Discussion started by: brainstormer
4 Replies

10. UNIX for Dummies Questions & Answers

help on appending data to existing data

I need to know how to record the hostname, date/time and all of the process and send it all to one file. I know that the commands I need are hostname, date and ps but I don't know how to do them all and send them all to the same file. Please help! (1 Reply)
Discussion started by: precious51980
1 Replies
Login or Register to Ask a Question