Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT...
-
Upload
geoffrey-potter -
Category
Documents
-
view
217 -
download
2
Transcript of Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT...
![Page 1: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/1.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Scripting Tools, languages and the Shell
intERLab at AITNetwork Management WorkshopMarch 11-15 – Bangkok, Thailand
Hervey Allen
![Page 2: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/2.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Languages
Interpreted
bash/sh, Perl, PHP, Python, Ruby
Compiled
C, C++, Java
![Page 3: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/3.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Tools
sed: Stream EDitor awk: Pattern scanning & processing dc : Arbitrary precision calculator bc: Arbitrary precision calculator
language. tr: Translate or delete characters grep: Print lines matching a pattern
![Page 4: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/4.jpg)
nsrc@intERLab 2008Bangkok, Thailand
The Shell (bash) export printenv ~/.bashrc ~/.profile /etc/profile /etc/bash.bashrc /etc/skel (in Ubuntu)
![Page 5: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/5.jpg)
nsrc@intERLab 2008Bangkok, Thailand
The Shell cont.
By default, on Ubuntu, we use the Bourne Again SHell, or BASH.
The shell is your interface to the kernel.
Programs use the shell to determine their environment.
Your shell environment can be customized. We'll go over how it's configured in Ubuntu.
![Page 6: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/6.jpg)
nsrc@intERLab 2008Bangkok, Thailand
The Shell cont.
Flow of bash execution at startup:Interactive login
0. /etc/profile
1. ~/.profile
2. ~/.bashrc
This is true for Ubuntu. See “man bash” for standard startup sequence.
We'll go through this now...
![Page 7: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/7.jpg)
nsrc@intERLab 2008Bangkok, Thailand
The Shell cont.
Flow of bash execution at startup:Interactive shell, no login
0. /etc/bash.bashrc
1. ~/.bashrc
Non-interactive startup, run a script
1. Script looks for variable BASH_ENV
2. If it is set and is a file bash does this:if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
![Page 8: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/8.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Using the ShellCommand Line Interpreter or CLI
To understand scripts let's practice a bit with the CLI. At the shell prompt try:
# cd; echo “Hello, World” > test.txt; cp test.txt test.txt.bak; vi test.txt
The above is all on one line.
What happened?
Now try this:# cp test.txt $( date +%F )test.txt
![Page 9: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/9.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Using the ShellIn a file
Now create a new file and place the some of our previous command in it:
# cd
# vi newscript
echo “Hello world” > hello.txt
cp hello.txt hello.txt.bak
cat hello.txt+hello.txt.bak > new.txt
cat new.txt
:wq
![Page 10: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/10.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Using the ShellIn a file
Now we can execute those commands in the order in which they appear in the file by doing this:# bash newscript
# sh newscript
# . newscript
![Page 11: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/11.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Using the ShellAs a shell script
Now we can take the last step and start to create self-contained scripts that run on their own.
We'll need to do two things:
1. Specify the CLI to use, and
2. Make our file executable
![Page 12: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/12.jpg)
nsrc@intERLab 2008Bangkok, Thailand
The “Shebang”
To specify that a file is to become a shell script you specify the interpreter like this at the very start of the file:
#!/bin/sh
or
#!/bin/bash
etc...
This is known as the “Shebang” (#!).
![Page 13: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/13.jpg)
nsrc@intERLab 2008Bangkok, Thailand
What's Next?
Now let's create a very simple shell script. This will simply echo back what you enter on the command line:#!/bin/sh
echo $1
Enter this in a file new.sh, then do:# chmod 755 new.sh
![Page 14: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/14.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Run your script
To run the script do:# ./new.sh text
# ./new.sh “text with spaces...”
Try updating the script to echo two, or more separated items on the command line.
![Page 15: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/15.jpg)
nsrc@intERLab 2008Bangkok, Thailand
When Not to Use
A long'ish list...Resource-intensive tasks, especially where speed is a factor (sorting, hashing, etc.)
Procedures involving heavy-duty math operations, especially floating point arithmetic arbitrary precision calculations, or complex numbers
Cross-platform portability required
Complex applications, where structured programming is a necessity (need type-checking of variables, function prototypes, etc.)
Project consists of subcomponents with interlocking dependencies
Extensive file operations required (Bash is limited to serial file access, and that only in a particularly clumsy and inefficient line-by-line fashion)
Need native support for multi-dimensional arrays or data structures, such as linked lists or trees
Need to generate or manipulate graphics or GUIs
Need direct access to system hardware or port or socket I/O
![Page 16: Nsrc@intERLab 2008 Bangkok, Thailand Scripting Tools, languages and the Shell intERLab at AIT Network Management Workshop March 11-15 – Bangkok, Thailand.](https://reader036.fdocuments.net/reader036/viewer/2022082710/56649e635503460f94b5f5d8/html5/thumbnails/16.jpg)
nsrc@intERLab 2008Bangkok, Thailand
Resources
The books you received in class:
Classic Shell Scripting
The Bash Cookbook
The on-line Advanced Bash Scripting Guide, available at:
http://www.tldp.org/LDP/abs/html/