Upload
theodora-brown
View
215
Download
0
Embed Size (px)
Citation preview
Unix Shell Basics
Edited from Greg Wilson's "Software Carpentry" http://www.swc.scipy.org/
Introduction
Most modern tools have a graphical user interface (GUI)But command-line user interfaces (CLUIs) still have their placeEasier to build a simple CLUI than a simple GUIHigher action-to-keystroke ratioEasier to see and understand what the computer is doing on your behalf
Which is part of what this course is aboutMost important: it's easier to combine CLUI tools than GUI tools
Small tools, combined in many ways, can be very powerful
This lecture focuses on Unix
The Shell
The most important command-line tool is the command shellUsually just called “the shell”Looks (and works) like an interactive terminal circa 1980 (or cmd in Windows)
Varieties of Shell
The shell is just one program among manyMany different shells have been writtenThe Bourne shell, called sh, is an ancestor of many of them It's still a lowest common denominator that
you can always rely onWe'll use bash (the Bourne Again Shell) in this course Even on Windows (thanks to Cygwin)
Others include C shell (csh), TC shell (tcsh), etc.
Paths
Files and Directories are items in the file systemA path is a description of how to find something in a file system
An absolute path describes a location from the root directory down
Equivalent to a street address Always starts with "/" /home/hpotter is Harry Potter's home directory /courses/swc/web/lec/shell.html is this file
A relative path describes how to find something from some other location
Equivalent to saying, “Four blocks north, and seven east”
From /courses/swc, the relative path to this file is web/lec/shell.html
Current Directory
Every program (including the shell) has a current working directory pwd .
Every directory has a parent cd ..
Every user has a home cd ~
Navigating
pwd
cd
ls
Options
ls –F
ls –a
ls –l
ls –l -t
Viewing Files
cd /usr/include
cat math.h
more math.h
Getting help
man – the Unix Manual if installed correctly!
man ls
man more
Grep
grep extern math.h
grep –v extern math.h
Word Count
wc math.h
Doing Instead of Looking
cd ~mkdir mydir
cd mydir
cp /usr/bin/math.h .
ls
Optimization Example
Download SimulationApp.classPut in local directory, and run java SimulationApp <v1> <v2>
<v3>
look in coarse_grid_script runs simulation many many times
Too much output to handle!
Redirection
coarse_grid_script > result_file
/usr/bin/sort result_file
/usr/bin/sort –g –k 8 result_file
Don’t re-invent the wheel! Don’t code sorting routines into your own
code unless absolutely necessary
awk
awk '{print $7}' result_file
awk '{print $7 $8}' result_file
awk '{print $7" "$8}' result_file
Pipes
awk '{print $7" "$8}' result_file | /usr/bin/sort -g –k 2 > sorted_outputs
License
Copyright © 2005-06 Python Software FoundationPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.