LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-04-2005, 09:09 AM   #1
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Rep: Reputation: 30
Bash - Getting a for loop to process variables with a space in them


Say I have a file:
[user@myserver conf]$ cat schedule.txt
051004,echo "helloworld",100
051004,echo "helloworld",100
051004,echo "helloworld",100
051004,echo "helloworld",100

How can I run each command in the list from one line?

I've tried:
[user@myserver conf]$ for i in `awk -F, '{print $2}' schedule.txt`; do `$i`; done
bash: "helloworld": command not found
bash: "helloworld": command not found
bash: "helloworld": command not found
bash: "helloworld": command not found

Which runs the echos, but thinks the argument is another command...

I suppose my real question is - How can I get the for loop to treat each line as a single command/variable?

Dave
 
Old 10-04-2005, 09:36 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Im'n not entirely sure what it is you actually want, but maybe this wil help:

Code:
#!/bin/bash

# put whole line in variable
cat schedule.txt | while read THISLINE
do
  echo $THISLINE
done

# only the echo "helloworld" part
awk -F"," '{print $2}' schedule.txt
I do believe you are asking for the first one (the while read construct).

Anyway, hope this helps.
 
Old 10-04-2005, 09:38 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Here's an example that should answer your question.
Code:
awk '{ print; }' sample | sh
 
Old 10-04-2005, 10:46 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Do you need a loop?
awk -F, '{print $2}' schedule.txt

If you do want to use a loop, maybe this will get you started...
Code:
#!/bin/bash
# Example: ./test schedule.txt
cat $1 | \
while read i; do
awk -F, '{print $2}'
done
 
Old 10-08-2005, 11:21 AM   #5
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
Thanks folks - all these are helpful; solved my problem.

Dave
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM
BASH variables getting unset outside of loop trevelluk Programming 2 03-25-2005 07:14 AM
Bash for loop Genjix Programming 5 12-23-2004 02:56 AM
how to find which process are involved in the infinite loop? chinesealbert Linux - General 0 07-01-2004 07:51 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration