Applied Neurobiological Imaging, Dr. Katherine L. Narr

Our Protocols « back

UNIX Commands That Everyone Should Know and Some C-Shell Scripting

Although this page will not give an exhaustive review of UNIX and all that can be done on the command line, hopefully it will help to get at the more useful tools and tricks we use in the lab.

Basic Commands

See UNIX Tutorial For Beginners for some really useful information on how to get started, as well as how to do more complicated things in UNIX.

Creating a Loop

C-Shell

foreach x (cat dog hamster)
   echo ${x}
end

This will print:

cat
dog
hamster

We can also nest foreach loops, but make sure that you want to repeat each iteration of the inner loop for each iteration of the outer loop (indentation is for clarity and is not strictly necessary, but will make your code easier to read):

foreach animal (cat dog hamster)
   foreach place (house garden shed)
      echo "The ${animal} is in the ${place}."
   end
end

This results in:

The cat is in the house.
The cat is in the garden.
The cat is in the shed.
The dog is in the house.
The dog is in the garden.
The dog is in the shed.
The hamster is in the house.
The hamster is in the garden.
The hamster is in the shed.

So, if you'd wanted to print ONLY the lines "The cat is in the house, the dog is in the garden, the hamster is in the shed", then you would not nest your foreach loops.

For our neurological data, foreach loops become very useful when we have operations that we'd like to perform on multiple subjects.

Simple Scripts

You can use the information above to write a simple c-shell script, which you will then call from the command line without having to paste in everything line-by-line.

Online Resources

Non-online Resources

© 2014 BMAP. All rights reserved.