bash syntax: looping through a comma-separated list
How can I get bash to read a list of inputs one at a time?
I'm trying to process some video and I would like to run the same command at predetermined intervals on the file. I'd like to use a comma-delineated list of timestamps as the input. I want to do something like this: for TIME in "time1,time2,time3,time4" ; do command @ $TIME ; done But I can't figure out how to get bash to process such a list. I tried looking at the 'read' command, but I even figure out exactly what it's supposed to do, or whether it would apply in this case. I've found a way to input the times via a text file (using the 'line' command), but it's a big hassle since I have to edit the list so that there's one entry per line. There must be a way to use the list as-is. I've searched this forum and this web but haven't found anything useful. I want to be able to do this both from the command prompt and eventually through an actual script. Can someone help me out here? PS: As a related question, is there any easy way to generate a list of regularly spaced timestamps, say every 300 seconds, with the format "hh:mm:ss"? |
Something like that?
1) IFS="," var="a,b,c" for v in $var do echo "$v" done 2) while /bin/true; do sleep 300; date +"%H:%M:%S"; done |
Quote:
Code:
> echo a,b,c,d | sed s/,/\\n/g |
...and with a default IFS "for v in ${var//,/ }"?
|
Quote:
Quote:
@colucix: Thank you. That does make converting the list between formats easier. But I'd still like to avoid having to use a text file altogether. |
unSpawn, I just tried your string, but that doesn't seem to work. It only removes the commas, concatenating all the values together into a single long one.
|
Quote:
Code:
#/bin/bash |
Based on your original question here are a couple of ideas:
The shell for command looks for white space between entries and you could use this for example: for x in time1 time2 time3 do ; <script> ; done or you can put the entries in a file and have linux execute a shell command to read the file. For example if you have a list of the times in a file called dotimes and you want the for statement to read the list you could use the back quote (just to the left of the 1 key). for x in `cat dotimes` do ; <script> ; done as a bash script you could call it would look like this: #!/bin/bash for x in `cat /home/me/dotimes` do <script> done But the simplest way I can think of to do something like what you want is to run a simple loop with the sleep command. Say you want to list the contents of the /var/tmp directory every 5 minutes to see if the file is growing. From the command line you would type the following: while true do date /bin/ls -la /var/tmp sleep 300 done The command would run every 5 minutes till you stop (break AKA <CTRL> C) it. It would list the date at the beginning of each run so you could see the time change. Hope this helps. rhoward1231@yahoo.com |
Quote:
If you do it from the command line then yes, you have to set it back,maybe like this: old_IFS=${IFS} blabah IFS=${old_IFS} Well, it's not that tricky I think. Otherwise unSpawn method is shorter but you need to be using bash, it's not POSIX compliant. Code:
$ echo $0 |
Quote:
|
Thanks for the advice everyone. Sorry I couldn't get back to you for a couple of days. There's a lot I have to reply to.
colucix: Now that appears to do just what I was looking for. A bit of modification to add some user inputs and that script will be perfect for generating various lists of timestamps. rch1231: A-ha, I see. So it's the comma separation that's doing it. The reason I've been using commas is because that's how all the dvd authoring programs handle their chapter lists, and I've just been copy/pasting the numbers. But now I can generate my own lists or use the IFS setting or unSpawn's method to work around it. Speaking of which... unSpawn: You were right, I did miss the space. It's always the little typos that get you, isn't it? It works when I type it in correctly. I'll probably go with that, since I don't have to worry about setting external variables that way. nx5000: That's what I thought. I want to be able to do this both with a script and quick-&-dirty manually, so I want to be careful about the IFS setting when I'm doing the latter. And no worries about compatibility. I'll only be using bash. I appreciate all the help. Now to implement what I've learned. |
All times are GMT -5. The time now is 05:51 PM. |