Bash - Getting a for loop to process variables with a space in them
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.