Reading Multiple Variables From a Single Line in Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading Multiple Variables From a Single Line in Shell
# 1  
Old 12-20-2006
Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that you will probably need to keep things simple if you answer my question. I use ssh to access my server so it is all command line.

I want to write a script to automate backups of certain files on my server. What I've done is create at text file with the name I want to call the backup file, and the folder or file I want to back up seperated by a space, one backup file per line, as follows:

mysql_config /etc/mysql
apache2_config /etc/apache2

What I want to do is have the script read each line, and create a gzipped tarball of each file/folder with the name I have given. My problem is that, as I understand it, cat goes line by line, how do I extract the two different variables from each line? Everything else I think I can figure out, but that one has me stumped. I was thinking I could use awk or mawk, but how do I get awk to only parse one line incrementally?

Thanks in advance. I'm pretty sure this will be simple once I see the solution...
# 2  
Old 12-20-2006
Code:
exec < inputfile
while read file dir ; do
       echo dir = $dir
       echo file = $file
done

# 3  
Old 12-20-2006
Why couldn't you have your input file contain:
Code:
/etc/mysql/mysql_config
/etc/apache2/apache2_config

It seems it would be easier, since you could then just read the file line by line -- one line per backup.

EDIT: Disregard that. I misinterpreted your requirement.
# 4  
Old 12-20-2006
Quote:
exec < inputfile
while read file dir ; do
echo dir = $dir
echo file = $file
done
I tried the following:

exec < backup_files
while read names files ; do
echo files = $files
echo names = $names
tar -czf $names.tgz $files
mv $names.tgz /var/backups/$names.tgz
done

All I got was a bad interpreter error.
# 5  
Old 12-20-2006
#!/bin/sh

echo "File Name:"
read filename

line=`cat $filename`
for item in $line
do
echo "Generating tar file $item"
echo "Enter Tar file name:"
read tarfnm
tar cvf ${tarfnm}.tar ${item}
echo "Done"
done
justsam
# 6  
Old 12-20-2006
Now I'm really confused, I can't even get a 'echo "hello world"' script to work. The script runs but nothing gets printed to the screen...
# 7  
Old 12-20-2006
Quote:
Originally Posted by Drek
I tried the following:

exec < backup_files
while read names files ; do
echo files = $files
echo names = $names
tar -czf $names.tgz $files
mv $names.tgz /var/backups/$names.tgz
done

All I got was a bad interpreter error.
Code:
#!/bin/ksh

exec < inputfile
while read file dir ; do
echo dir = $dir
echo file = $file
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading multiple variables in a loop

Hi, I managed to read and print variable as shown in the below code. table_name=table1,table2,table3 i=0 IFS="," for i in $table_name do echo $i done Is there a way how I can read more than one variable. For example I need to read 2 variables and populate the output... (6 Replies)
Discussion started by: shash
6 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. Shell Programming and Scripting

Reading of variable in a single line command

Hi All, Below is a sample command that I can run without any problem in the command line. Command Line dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt -rw-r--r-- 1 monuser oinstall 0 Jan 18 11:02 ./filename_20130118.txt But once I put that command line in file (list.txt) and... (3 Replies)
Discussion started by: padi
3 Replies

4. Shell Programming and Scripting

How to ignore single or multiple lines between /* and */ while reading from a file in unix?

I have a file proc.txt: if @debug = 1 then message 'Start Processing ', @procname, dateformat(now(*), 'hh:mm:ss'), @julian type info to client; end if; /* execute immediate with quotes 'insert into sys_suppdata (property, value, key_name) location ''' || @supp_server || '.' ||... (5 Replies)
Discussion started by: kidncute
5 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

7. Shell Programming and Scripting

Reading a single character from each line of the file

Hi, I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file (8 Replies)
Discussion started by: arsii
8 Replies

8. Shell Programming and Scripting

Filtering Multiple variables from a single column

Hi, I am currently filtering a file, "BUILD_TIMES", that has multiple column of information in it. An example of the data is as follows; Fri Nov 5 15:31:33 2010 00:28:17 R7_BCGNOFJ_70.68 Fri Nov 5 20:57:41 2010 00:07:21 R7_ADJCEL_80.6 Wed Nov 10 17:33:21 2010 00:01:13 R7_BCTTEST3_80.1X... (7 Replies)
Discussion started by: crunchie
7 Replies

9. Shell Programming and Scripting

Reading multiple lines as single

Hi, Is it possible to read every 2 lines as single record, I have a file like below, ~CZK ~TSCHECHISCHE KRONE ~TSCHECH. REPUBLIK Dec 1 2005 12:00AM~ 10.840000~ ~DKK ~DAENISCHE KRONE ~DAENEMARK Dec 2 2005 12:00AM~ ... (9 Replies)
Discussion started by: braindrain
9 Replies

10. Shell Programming and Scripting

reading a single line

hello, i need an algorithm which reads a line specified to it in shortest possible time. i am already using sed, i need a better way. please suggest me alternatives (2 Replies)
Discussion started by: rochitsharma
2 Replies
Login or Register to Ask a Question