Tuesday, 20 December 2016

Shell Script 7: for-loop



The basic outline of a for-loop is as follows:
for VARIABLE in LIST; do
command1
command2

commandn
done

VARIABLE can be any variable, though most often the lowercase “i” is used by convention. LIST is a list of items; you can specify multiple items (separating them by a space), point to an external text file, or use an asterisk (*) to denote any file in the current directory. The commands listed are indented by convention, so it’s easier to see nesting – putting loops in loops (so you can loop while you loop).

jtony@genoa:~/learn/shell$ cat forloop.sh
#!/bin/bash

# A simply display for loop
#for i in item1 item2 item3 item4 item5 item6; do
for i in *; do
  echo "$i"
done
jtony@genoa:~/learn/shell$ cat zip.sh
#!/bin/bash

# Zip all the arguments with for loop
for i in $@; do
  zip archive "$i"
done



example 3:
remove all the killed key words in all the expand-isel-*.mir files:
for f in expand-isel-*.mir; do sed -e 's/killed//g' $f  > $f.cpp; done



Note all these series blogs are originated from, I did some modification to it, many thanks to the author.
http://www.howtogeek.com/68184/the-beginners-guide-to-shell-scripting-2-for-loops/








 

No comments:

Post a Comment