Created
January 20, 2015 19:13
-
-
Save wasade/12ffed97a3f5f86963d4 to your computer and use it in GitHub Desktop.
stupid for loop examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rename some files based on some counter | |
x=0 # set an environment variable for our counter | |
for i in foo/* # for every file in the foo directory | |
do | |
# we're going to rename the file, and prefix it with the value in the | |
# counter. the variable $i will contain both the directory foo, as well as | |
# the filename (e.g., foo/bar). Since we want to prefix the number on to | |
# the filename, we need to somehow strip out the foo/ part. we can do this | |
# using a program called basename (it's worth a peak at its man page). | |
# basename can be used to get just the filename from a path. the syntax is | |
# janky, but you can have things get executed inline if you put it | |
# inbetween parentheses with a dollar sign to tell the computer to treat it | |
# as magical gold. there's also a few ways in which the variables are being | |
# referred too here, sometimes its ${thing}, or $thing. the use of curly | |
# braces just make it explicit, and avoid cases of ambiguity that might | |
# confuse the computer (remember, the computer is stupid). | |
mv $i foo/${x}_$(basename $i) | |
# in order to increment our little counter, we need to use let. its annoying. | |
let x=$x+1 | |
done | |
# woooo... awesome fun party time. now lets grep stuff and do things with the | |
# stuff. | |
# also, hyperbolie and a half is an amazing comic. necessary meme: | |
# http://imgur.com/4EjMcKR | |
for thing in `/bin/ls | grep m | rev` # grep things with m, and reverse them | |
do | |
echo $thing | |
done | |
# do a whole lot of something | |
for f1 in `seq 1 5` | |
do | |
for f2 in {a,b,c,d} | |
do | |
echo $f1 $2 | |
done | |
done | |
# say you have access to a giant super computer. and say you don't like the other users and want to submit a large number of jobs. yes. | |
for some_file in ls foo/* | |
do | |
# this is specific somewhat to torque based clusters | |
echo "your_awesome_program --some-argument -i ${some_file}" | qsub -N "w00t" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment