18
Unix Shell Basics Edited from Greg Wilson's "Software Carpentry" http://www.swc.scipy.org/

Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Embed Size (px)

Citation preview

Page 1: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Unix Shell Basics

Edited from Greg Wilson's "Software Carpentry" http://www.swc.scipy.org/

Page 2: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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

Page 3: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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)

Page 4: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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.

Page 5: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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

Page 6: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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 ~

Page 7: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Navigating

pwd

cd

ls

Page 8: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Options

ls –F

ls –a

ls –l

ls –l -t

Page 9: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Viewing Files

cd /usr/include

cat math.h

more math.h

Page 10: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Getting help

man – the Unix Manual if installed correctly!

man ls

man more

Page 11: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Grep

grep extern math.h

grep –v extern math.h

Page 12: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Word Count

wc math.h

Page 13: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Doing Instead of Looking

cd ~mkdir mydir

cd mydir

cp /usr/bin/math.h .

ls

Page 14: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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!

Page 15: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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

Page 16: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

awk

awk '{print $7}' result_file

awk '{print $7 $8}' result_file

awk '{print $7" "$8}' result_file

Page 17: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

Pipes

awk '{print $7" "$8}' result_file | /usr/bin/sort -g –k 2 > sorted_outputs

Page 18: Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"

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.