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
# 8  
Old 12-20-2006
justsam, I don't think you are understanding what I want, unless I am completely misunderstanding your script. I don't want to sit there and enter the file names and directories every time I back my files up. It would be simpler to just do it all manually rather than getting a script to do it. I want to be able to automate my backups. I want the script to read the backup file name, and the backup file location from my list of files to be backed up, and do it all automatically without requiring any input. Once I figure out the script I will schedule it to run with cron so I don't have to think about it.
# 9  
Old 12-20-2006
@ vgersh99

I used bash

#!/bin/bash

I'll try ksh and see if that makes a difference.

edit: It didn't, and I still don't understand why 'echo "hello world"' isn't printing anything to the console...
# 10  
Old 12-20-2006
Quote:
Originally Posted by Drek
@ vgersh99

I used bash

#!/bin/bash

I'll try ksh and see if that makes a difference.

edit: It didn't, and I still don't understand why 'echo "hello world"' isn't printing anything to the console...
ok.... how about the ol' fashion way:
Code:
#!/bin/ksh

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

# 11  
Old 12-20-2006
My code works with bash:
Code:
bash-3.00$
bash-3.00$ cat inputfile
mysql_config /etc/mysql
apache2_config /etc/apache2
bash-3.00$ cat script
#! /usr/local/bin/bash
exec < inputfile
while read file dir ; do
       echo dir = $dir
       echo file = $file
done
bash-3.00$ chmod u+x script
bash-3.00$ ./script
dir = /etc/mysql
file = mysql_config
dir = /etc/apache2
file = apache2_config
bash-3.00$

As for your problems, did you call your script "test"? And then you tried to run it by typing "test" at the prompt? "test" is a builtin command. Call it something else. And use a path to the script like I did:
./script
That's why I got away with calling it "script". There is also a "script" command.
# 12  
Old 12-20-2006
Thanks everyone, it worked perfectly. I wish I understood why it works, but it does work. There were two problems. Yes I named my test script "test", that was why my "hello world" script didn't work (*phew* I thought I'd broken something on my box). The other problem was that I wrote my script with Notetab on my Windows machine and it saved it in DOS format. I was thrown off because it didn't have the dos end of line characters, so I thought it was fine. Anyway, I've configured Notetab to save in Unix format, so hopefully I won't have that problem again. Thanks again for all the help and the really quick answers. Now all I have to do is figure out how to get the same script to backup my MySQL databases, and I will be all set. I'll post back when I take up that challenge...
# 13  
Old 12-20-2006
The same basic thing also worked perfectly for the MySQL database backups. Again, thank you very much, I would never have figured it out on my own.
# 14  
Old 12-20-2006
Just thought I'd post the script that was the answer to this question. It works perfectly as I said. It gzips the files/folders I want to backup and saves them in the /var/backups folder. It does the same for the databases for my website. Tar even preserves the file structure so all I would need to do to restore is place the files in / and untar them. The only thing I have to do manually is backup the files in that folder to the usb drive I use for that purpose. I use sftp through my Windows system because the file system on the usb drive is ntfs.

Code:
#!/bin/bash

exec < /etc/backups/backup_files
while read file_name files ; do
tar -czf $file_name.tgz $files 2>&1 | grep -v "Removing leading"
mv $file_name.tgz /var/backups/$file_name.tgz
done

exec < /etc/backups/backup_databases
while read file_name user pass database ; do
mysqldump --add-drop-table -c -e -u $user -p$pass $database > $file_name.sql
tar -czf $file_name.tgz $file_name.sql
rm $file_name.sql
mv $file_name.tgz /var/backups/$file_name.tgz
done

The password is stored in plain text. I've set strict permissions on it, but this wouldn't be good for a server with any sort of public access but it's good enough for my clan server which I operate from my home.

Last edited by Drek; 12-20-2006 at 07:21 PM..
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