Example: biology

Shell Programming - Pennsylvania State University

Institute for Networking and Security ResearchDepartment of Computer Science and EngineeringPennsylvania State University , University Park, PASystems and Internet Infrastructure SecurityiiShell ProgrammingProfessor Patrick McDanielFall 2016 Vim + Make Vim integrates with make Ty p e :make target(or just :make) to build Vim will read the output and jump to each error or warning Next error: :cn Previous error: :cp Show current error again: :ccShell Programming aka Shell scripting , bash scripting What is it? Series of commands Programming with programs What for Automating System administration PrototypingA sample script: shello First line: interpreter The #!is important! Comment: #to end-of-line Give the file execute permission chmod+x shello Not determined by file extension Ty p i c a l : .shor none Run it ./shello#! /bin/ bash # Greetings!echoShello world# Use a variableechoShello "$USER"# Set a variablegreetz=Shellutationsecho"$greetz world" Shell variables Setting/unsetting exportvar=value No spaces!

Shell programming • aka “shell scripting,” “Bash scripting ... • Advanced Bash-Scripting Guide

Tags:

  Guide, Programming, Advanced, Shell, Bash, Scripting, Advanced bash scripting guide, Shell programming, Bash scripting

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Shell Programming - Pennsylvania State University

1 Institute for Networking and Security ResearchDepartment of Computer Science and EngineeringPennsylvania State University , University Park, PASystems and Internet Infrastructure SecurityiiShell ProgrammingProfessor Patrick McDanielFall 2016 Vim + Make Vim integrates with make Ty p e :make target(or just :make) to build Vim will read the output and jump to each error or warning Next error: :cn Previous error: :cp Show current error again: :ccShell Programming aka Shell scripting , bash scripting What is it? Series of commands Programming with programs What for Automating System administration PrototypingA sample script: shello First line: interpreter The #!is important! Comment: #to end-of-line Give the file execute permission chmod+x shello Not determined by file extension Ty p i c a l : .shor none Run it ./shello#! /bin/ bash # Greetings!echoShello world# Use a variableechoShello "$USER"# Set a variablegreetz=Shellutationsecho"$greetz world" Shell variables Setting/unsetting exportvar=value No spaces!

2 Unsetvar Using the value $var Untypedby default Behave like strings (See help declarefor more)Special variables Change Shell behavior or give you information PWD: c u r r e n t d i r e c t o r y USER: n a m e o f t h e current user HOME: t h e c u r r e n t u s e r s home directory Can usually be abbreviated as a tilde (~) PATH: w h e r e t o s e a rc h for executables PS1: B a s h p ro m p t ( w i l l see later)Exercise Make a directory ~/bin Move shelloscript there Prepend the directory to your $PATH PATH=~/bin:$PATH Change to home dir Run by typing shelloShell initialization Set custom variables At startup, bash runs Shell commands from ~/.bashrc Just a Shell script This script can do whatever you wantFun with prompts Main prompt: $PS1 Special values \u: u s e r n a m e \h: h o s t n a m e \w: w o r k i n g d i r \e: e s c a p e ( f o r c o l o r s ) many others This is something you can set in your.

3 BashrcVery detailed treatment: : Vim initialization Vim reads : commands from ~/.vimrc Color scheme Indenting preferences Backup files Appearance preferences Line numbers Highlighting matched parentheses, braces, etc. Titlebarin terminal Remapping keyscolorscheme koehlerset autoindentset autowriteset backupset numberset showmatchset titlefiletype plugin indent onsyntax onSpecial charactersecho Penn State is #1echo Micro$oftWindowsecho Steins;Gate What happened?Special charactersecho Penn State is #1echo Micro$oftWindowsecho Steins;Gate What happened? Many special characters Whitespace #$*&^?!~'`"\{}[]<>()|; What if we want these characters?Quoting Removes specialness Hard quotes: '..' Quote everything except closing ' Soft quotes: ".." Allow variables (and some other things) Good practice: "$var" Backslash (escaping) Quotes next characterArguments In C: argcand argv[] Split at whitespace How can we override this?

4 Arguments to a script ./script foo bar $#is the same as argc "$@": a l l args "$1"to "$9": i n d i v i d u a lDebug mode Shows each command Variables expanded Arguments quoted Run with bash -x Te m p o r a r y just for that run bash -x shello Use -xvfor even more infoExercises Make a script that: Prints its first argument foofoofoo Prints its first four argsin brackets, one per "foo bar"baz[foo bar][baz][][]Redirecting input/output Assigns stdinand/or stdoutto a file echohello >world echohello >>world trh j <worldPipelines Connect stdoutof one command to stdinof the next echohello |trh j .. |rev .. |hexdump-C .. |grep06 UNIX philosophy at work!Some references advanced bash - scripting guide Actually a great reference from beginner to advanced Lots of gems, somewhat more advanced Fun to figure out how they work bash man page man bash Ve r y c o m p l e t e , o n c e yo u ' re u s e d t o re a d i n g m a n p a g e sCode for today$ wget $ tar -xvzf 311shell2$ cd shell2$ make Today we re learning some loops If it starts to run away, Ctrl-Cis your friend Sends a signal that ends the process More on signals Wo r k s o n m a ny d i f fe re n t programs, as long as they were started from the command line Displayed as ^CHow to kill a processReturn from main In C, the main function always returns an int Used as an error code for the entire process Same convention as any other C function Zero: success Nonzero: failure, error, killed by a signal, etc.

5 Also known as the exit statusof the processExit status in scripts $?: get exit status of the previous command The exit status of a script comes from the last command it runs Or use the exitbuiltinto exit early, exit 1 ! cmdreverses the value: 0 for failure and 1 for success Exactly like the !( logical not ) operator in CStatus sample program$ ./status 0$ echo $?$ ./status 2$ echo $?$ ! ./status 2$ echo $?$ ./status -1$ echo $?#include < >intmain(intargc, char**argv){// Quick-and-dirty int conversionreturnatoi(argv[1]);}Custom prompt for today You can include $?in your prompt I personally like this it lets me know for sure when something fails For today, let s do this:source newprompt Now try:./status 42Te s t c o m m a n d s Builtincommands that test handy conditions true: always succeeds false: always fails Many other conditions: testbuiltin Returns 0 if test is true, 1 otherwise Full list: help testWhat do these do?

6 $ test $ test -easdf$ test $ test -d/etc$ test 10 -gt5$ test 10 -lt10$ test 10 -le10$ test 12 -ge15 Useful tests test -e file Tr u e i f f i l e e x i s t s test -d dir Tr u e i f direxists and is a directory test -z "$var" Tr u e i f varis empty (zero-length) test -n "$var" Tr u e i f varis nonempty test str1 = str2 test num1 -gtnum2 or -lt, -ge, -le, -eq, -neCommand lists Simple command list: ; Runs each command regardless of exit status Example:do_this; do_that Shortcutting command lists &&stops after failure ||stops after success Examples:foo && echo successbar || echo failedTr y i t o u ttrue && echo onetrue || echo twofalse && echo threefalse || echo fourtest -e Makefile && makecat dog || echo 4 && echo 0 && echo 0cat dog; cat ; makemake clean && makeShorthand tests Shorthand test: [[.. ]] Wo r k a l i kefor test For example:age=20test $age -ge16 &&echo can drive[[ $age -ge16 ]] &&echo can drive Now say age=3and try againConditionals Exit status is used as the test for ifstatements:if list; thencmdsfi Runs list, and if the exit statusis 0, then cmdsis executed There are also elifand elsecommands that work the same loops You can write a whileloop using the same idea: while list; docmdsdone Runs list, cmds, list, cmds, for as long as listsucceeds (exit status 0) Similarly, an untilloop will execute as long as listfailsConditional practiceif !

7 [[ -e foo ]]; thenecho hello > foofiwhile [[ "$x" -lt 99999 ]]; doecho "$x"x="1$x"doneif cat foo; thenecho Same to youfiif cat dog; thenecho WooffiFor statement The forloop is for-each style:for varin words; docmdsdone The cmdsare executed once for each argument in words, with varset to that argumentFor (get it??)for a in A B C hello 4; doecho "$a$a$a"donefor ext in h c; docat "hello.$ext"doneFor (get it??)for a in A B C hello 4; doecho "$a$a$a"donefor ext in h c; docat "hello.$ext"doneGlobbing Old name for filename wildcards (Comes from global command ) *means any number of characters:echo *echo *.c ?means any one character:echo Bulk rename:for f in hello.*; domv "$f" "$ "doneSome more useful tools touch foo: m o d i f y t h e f i l e foowithout really changing it sleep t: w a i t f o r tseconds fgrep string: f i l t e r s t d i n t o j u s t lines containing string find.

8 -name '*.c': l i s t a l l t h e . c files under the current directory Many other things you can search for; see man find file foo: d e t e r m i n e w h a t k i n d o f file foois wc: c o u n t s w o r d s / c h a r a c t e r s / l i n e s from stdin bc: c o m m a n d l i n e c a l c u l a t o rExercises Print out foo once per second until ^C d Find all the .pngfiles in dir/ Find all the files in dir/ which are actuallyPNG graphics Use a pipe and bcto calculate the product of 199 and 42


Related search queries