2.bash
2.bash
INTRODUCTION
Linux & bash shell
I. Linux
2
What is Linux?
Linux is an operating system (OS) developed
by Linus Torvalds in 199
Why Linux?
Developed by Bell Labs in 1969, and initially free, UNIX
was quickly adopted as de facto scienti c computing O
Linux is everywhere
- 90% of supercomputers run Linux (incl. Blue Waters)
- Android OS is based on a Linux kernel
- Ubuntu distro is the most popular OS in the world
4
y
fi
y
5
.
6
II. bash shell
7
The command line
CLI and GDE offer alternatives to interact with a machin
8
:
fi
n
fi
t
bash shell
“Command line interpreters”
or “shells” convert text inputs
into OS command
9
fl
h
10
bash: basics
Pop a bash terminal by clicking on
or navigating Applications è Accessories è Termina
>
bash: basics
touch <file> - make new le < le> or update last
access of existing l
mkdir <dir> - make director
fi
fi
e
bash: basics
./<execFile> - execute execFile in cdir
<path>/<execFile> - execute execFile in pat
13
.
l
fi
d
bash: basics
cp <source> <target> - copy le source to target
e.g. cp myFile /apps/doc/
rm <file> - remove le
rm -r <directory> - remove recursively directory
14
.
fi
fi
fi
)
bash: safety!
cp / rm / mv - These do exactly what you ask
They do not ask for permission
Furthermore, there is no Trash/Recycling
Once you remove / overwrite a le, it’s gone
Standard “safety” choices: use alias in your .bashrc
alias cp=‘cp -i’
alias rm=‘rm -i’
alias mv=‘mv -i’
setopt noclobber
You don’t have to do this, but you may breathe a little
easier with some safety.
15
fi
.
bash: basics
whoami - show your login username
who - show everyone currently logged in
16
fi
fi
fi
fi
fi
s
fi
bash: basics
zip <archive> <file1 file2 ...>
- create zip le archive.zip containing le1, le2, ...
unzip <archive>
- unzip zip le archive.zi
fi
fi
bash: basics
top - show active processes
top -o cpu - show active processes ordered by cpu %
top -U <usr> - show active processes owned by us
fi
e
. - current director
* - wildcard characte
| - pipe
e.g. cat <file> | grep tungsten 19
.
y
21
bash: integer arithmetic
expr - integer arithmetic engin
e.g
$ echo `expr 1 + 1`
2
$ var1=`expr 10 \* 2`
$ var2=`expr 21 / 7`
$ echo $var1 $var2 `expr $var1 / $var2`
20 3 6
22
.
24
fi
)
https://answers.uillinois.edu/
www.putty.org
illinois.engineering/page.php?id=81727
25
l
bash: sftp
SFTP more sophisticated alternative to scp
(secure file transfer protocol)
sftp <user>@<hostname> - login to host
ls - remote ls
lls - local ls
pwd - remote pwd
lpwd - local pwd
cd - remote cd
lcd - local cd
get <file> - download le
put <file> - upload le
quit - logout
26
fi
fi
bash: vi/vim
Two built-in CLI text editors: vi/vim & emacs
Seem slow and painful, but invaluable for on-the- y edit
fl
)
bash: vi/vim
Nav mod
éêçè - single char / single line movemen
gg - go to top of le
^ - go to beginning of lin
$ - go to end of lin
<n>G - go to line
w - skip forward one wor
b - skip backward one wor
fi
e
bash: vi/vim
Nav mod
x - delete characte
o - create new line below and enter insert mod
29
e
bash: vi/vim
Nav mod
dd or d$ - delete current lin
d<n>w - delete next n word
d<n>ê - delete next n line
u - und
Ctrl+r - red
30
e
bash: vi/vim
Nav mod
/<str><Enter> - search forward for st
?<str><Enter> - search backward for st
n - go to next matc
<N>n - go to Nth matc
31
e
bash: vi/vim
Nav mod
:w - writes le
:w! - writes le even if read only
:q - quit
:q! - quit and don't question me
(good way to mess things up)
32
e
fi
fi
bash: vi/vim
Ins mod
33
e
35
fi
s
fi
c
36
fi
bash: .bash_pro le & .bashrc
(ii) Use vi to add ~/local/bin to your PATH in .bashr
37
fi
w
fi
c
38
fi
bash: installing software
Typical anatomy of an installation from source
39
fi
l
:
V. bash scripting
40
What is bash scripting?
A bash script is nothing more than a list of bash
commands in an executable text l
e.g le downloads
system backups
job submission
le processing
41
.
fi
fi
fi
e
Anatomy of a script
A script is nothing more than a text le
- write using vi, emacs, Notepad, or favorite text editor
42
fi
Script 1: hello world!
$ touch helloWorld new script le
$ chmod 755 helloWorld making executable
$ vi helloWorld edit line belo
$ i enter insert mode
$ #!/bin/bash <Enter>
$ # this is my hello world script <Enter>
$ echo “Hello World!”
$ Esc escape to navigate mod
$ :wq write and quit
43
fi
w
Script 2: backup
Passing variables $1, $2, $3, ...
44
fi
fi
z
Script 3: summer
while loo arithmetic comparisons
$# and shift
Initializing sum to
- arithmetic comparisons:
-l
-g
-l <
-g >
-e =
-n !=
45
t
e
t
e
q
e
<
>
Script 4: oracle
if/else statemen
nesting
if loo
nested if loo
46
f
e
Script 5: calculator
case conditional
exit
defensive programming
safeguard on usag
- exit terminates scrip
case conditiona
- starts case, ends esac
- ) terminates pattern matc
- ;; terminates each cas
- | is the “or” characte
- * is the wildcard “catch all”
47
l
Script 5: calculator
48
Script 6: stringer
array
$@
Create an array strArray from parameter
- $@ = all parameters passed to bash cal
- ${ARRAY[@]} = array content
>
>
fi
:
y
fi
y
l
y
n
Script 7: ler
in nite loo read user input
Ctrl + C
- le comparison operators
- le exists (may be directory
- le exists (not directory
- directory exist
- le readabl
- le writabl
- le executable
50
fi
fi
e
f
d
r
w
x
fi
fi
fi
fi
fi
fi
e
fi
fi
r
Script 8: squarer
iteratin sleep
functions
51
g
VI. awk
52
awk
awk is a programming language in its own righ
53
n
fi
fl
e
Why awk?
“Forget awk, I’ll just use vi / emacs / Notepad!
OK, good luck...
- extract the third column of this 50,000 line le
- divide the second eld of each line by the seventh, and
save results in csv format
- extract every 15th line of this le and invert the eld
ordering to run from last to rs
fi
s
fi
awk basics
Rudimentary awk, comprehensive beginner’s tutorial at:
http://www.grymoire.com/Unix/Awk.html
fi
g
awk basics
Alternatively, can pipe input from termina
cat inFile > awk 'BEGIN { ...
{ ...
END { ... }
> outFil
'
'
Printing to terminal or l
Basic arithmeti
57
c
fi
e
58
fi
fi
s
setting up iteratio
initializing concat l
rm each coordsX.txt le
increment iterator
59
fi
fi
fi
fi
n
fi
fi
n
fi
e
fi