Shell Games - Lecture Notes - Advanced Programming Tools and Techniques | CS 265, Study notes of Computer Science

Material Type: Notes; Class: Advanced Programming Tools and Techniques; Subject: Computer Science; University: Drexel University; Term: Winter 2004;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-z27
koofers-user-z27 🇺🇸

7 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Shell Games
Objective: To introduce students to the concept of
a shell, effective use of the shell, the shell as
programming language, and shell scripts. We will
use the bash shell for the examples and details
provided.
Shell syntax and commands
history and command completion
job control
variables
programming constructs
–scripts
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Shell Games - Lecture Notes - Advanced Programming Tools and Techniques | CS 265 and more Study notes Computer Science in PDF only on Docsity!

Shell Games

•^

Objective: To introduce students to the concept ofa shell, effective use of the shell, the shell asprogramming language, and shell scripts. We willuse the bash shell for the examples and detailsprovided.– Shell syntax and commands– history and command completion– job control– variables– programming constructs– scripts

shell

•^

command interpreter (bash, sh, csh,…)

-^

.bashrc, .profile

-^

PATH and shell variables

-^

metacharacters

-^

history and command completion

-^

file redirection

-^

pipes

-^

process management

Executing a Command

-^

After reading a command, the shell may do someprocessing (see wildcards etc in the syntax description thatfollows), then it must find a program to execute thecommand.

-^

Some commands are executed directly by the shell. Othercommands are executed by separate programs. These arefound by looking in a list of directories for programs withthe appropriate name. The shell searches directories in thePATH variable. A hash table is used to make the searchfast. You can add new commands simply by adding newprograms (a program can be any executable file includingscripts – review Unix permissions) to directories in thePATH. You can modify/add directories in the PATH.

Finding out about Commands

  • type echo • which echo • info echo • man echo • info bash

Notes about PATH

•^

If you do not have. in your PATH, commands inyour current directory will not be found. You mayor may not want to add. to your PATH. If you donot and want to execute a command in the currentdirectory^ – ./command

-^

The PATH is searched sequentially, the firstmatching program is executed. Beware of namingyour executables test as there is another programcalled test and this may be executed when youenter^ – test

PATH

[jjohnson@ws44 software]$ echo $PATH /usr/local/FrameMaker/bin:/home/jjohnson/bin:/usr/local/gpl/mpich-

1.2.4/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/openwin/bin:/opt/SUNWspro/bin:/usr/ccs/bin:/usr/ucb:/usr/sbin:/usr/bin:/etc:/usr/etc:/usr/UTILS/publisher/bin:/usr/bin/X11:/bin:/usr/remote/alg_soft/linda2.5.2sol2.3/bin:. [jjohnson@ws44 software]$ PATH=${PATH}:/home/jjohnson/software [jjohnson@ws44 software]$ echo $PATH /usr/local/FrameMaker/bin:/home/jjohnson/bin:/usr/local/gpl/mpich-

1.2.4/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/openwin/bin:/opt/SUNWspro/bin:/usr/ccs/bin:/usr/ucb:/usr/sbin:/usr/bin:/etc:/usr/etc:/usr/UTILS/publisher/bin:/usr/bin/X11:/bin:/usr/remote/alg_soft/linda2.5.2sol2.3/bin:.:/home/jjohnson/software

Aliases

[jjohnson@ws44 jjohnson]$ alias alias cd..='cd ..' alias cp='cp -i' alias d='ls' alias df='df -h -x supermount' alias du='du -h' alias kde='xinit /usr/bin/startkde' alias l='ls' alias la='ls -a' alias ll='ls -l' alias ls='ls -F --color=auto' alias lsd='ls -d */' alias md='mkdir' alias mv='mv -i' alias p='cd -' alias rd='rmdir' alias rm='rm -i' alias s='cd ..' [jjohnson@ws44 jjohnson]$ type rm rm is aliased to `rm -i'

Shell Syntax

•^

Comments^ – # This is a comment^ – ls # list the files in the current directory

-^

Line continuation^ – echo A long ^ – > line

-^

;^

#Command separator – you can list more than one command per line separated by ;^ – ls ; who

-^

^

#Pathname separator

  • cd \home\jjohnson

Shell Syntax

•^

File redirection and pipes^ – <

redirect input from specified source

  • >

redirect output to specified source

  • >>

redirect output and append to specified source

  • |

pipe the output from one command to the input to

the next

-^

Examples^ – grep word < /usr/dict/words^ – ls > listing^ – ls >> listing^ – ls -l| wc -l

Shell Syntax

•^

Note file redirection of standard output [stdout]does not include error messages, which go tostandard error [stderr] (when you are in a terminalsession, both stdout and stderr go to the screen;however, when you redirect stdout to a file, stderrstill goes to the screen).

-^

stdout is designated by the file descriptor 1 andstderr by 2 (standard input is 0)^ – To redirect standard error use 2>^ – ls filenothere > listing 2> error^ – ls filenothere 2>&1 > listing # both stdout and stderr

redirected to listing

Example

jjohnson@HILBERT ~ $ find / -name me -print & [1] 1936 jjohnson@HILBERT ~ $ jobs -l [1]+ 1936 Running

find / -name me -print &

jjohnson@HILBERT ~ $ ps^ PID

PPID

PGID

WINPID TTY UID

STIME COMMAND

1608

1 1608

1608 con 1000 21:52:42 /usr/bin/bash

1936

1608

1936

1816 con 1000 09:18:03 /usr/bin/find

2036

1608

2036

1488 con 1000 09:18:07 /usr/bin/ps

jjohnson@HILBERT ~ $ kill 1936 jjohnson@HILBERT ~ $ jobs [1]+ Terminated

find / -name me -print

Shell Syntax

•^

Control characters (hold the Ctrl key and thespecified key at the same time)^ – CTRL-C

interupt – default behavior is to stop the

current command – this is very handy for infinite loops,pages of output, etc.

  • CTRL-\ # quite – use when CTRL-C does not stop the

current command

  • CTRL-Z # suspend the current command, you can

continue the command either in the foreground with fgor the background with bg

  • CTRL-D # end of input (useful when entering input

from standard input, stdin)

quoting

  • Alternatively, you can turn of the

interpretation of special characters one at atime with backslash-escaping^ – echo 2 * 3 &gt; 5 is a valid inequality

variables

  • Arbitrary variables can be used in the shell,

assign with =, and obtain value with $.^ – name=jeremy^ – echo $name

  • Sometimes it is useful to include the value

of a variable as part of another expression.^ – echo ${name}.file