Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
25Activity
0 of .
Results for:
No results containing your search query
P. 1
Unix Shell Scripting

Unix Shell Scripting

Ratings: (0)|Views: 688|Likes:
Published by api-26972476

More info:

Published by: api-26972476 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Shell Scripting
Kyle Wheeler
Contents
1 Intro
1
1.1 What is a Shell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.2 What are Shell Scripts?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.3 Why Use Shell Scripts?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
2 OneLiners
2
2.1 Redirection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2.1.1 FileDescriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2.1.2 SimpleRedirection(
\u00a2
\u00a1
\u00a4
\u00a3
\u00a6
\u00a5
). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2.1.3 LotsofFiles!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2.1.4 ConnectingOutputs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
2.1.5 Appending. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.2 Conditionals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.3 EnVariables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
2.4 Quotes, Strings, and Expansion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.4.1 Wildcards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.4.2 TheTilde. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.4.3 Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.4.4 Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2.4.5 Quotes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3 StoryProblems
5
3.1 Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3.1.1 For-Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
3.1.2 While-LoopsandUntil-Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
3.2 MoreVariables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
3.2.1 Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
3.2.2 BetterStringManipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6
3.3 Conditionals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
4 AdvancedScripts
7
4.1 Pre-De\ufb01nedVariables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
4.1.1 TheNumbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
4.1.2 TheRest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
4.1.3 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
4.2 Pre-De\ufb01nedFunctions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
4.3 Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
5 BeyondScripts
8
5.1 Readline\u2019sBeauty. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
5.2 UnixTools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
5.3 Con\ufb01gFiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
6 Examples
11
6.1 ChangeBackground. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6.2 Check
\u00a7
\u00a2
\u00a8
\ue000
\u00a9
\u00a3
\u00a7
\u00a6
\ue001
\u00a1
\u00a4
\ue002
\u00a7
\ue004
\ue003
\ue006
\ue005
\u00a6
\u00a9
\ue002
\ue005
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
1 Intro

This document is an introduction to shell scripting. It\u2019s not meant to be a complete or authoritative source, but by the same token, I\u2019m pretty sure I know what I\u2019m talking about. All examples in this docu- ment (unless otherwise noted) use

\u00a9
\u00a2
\u00a1
syntax1. If I mention a script in here, I try and include it at the
end of this document in theExamples section.
1.1 What is a Shell

Many computers have some form of a command-line interface. That is, on many computers you can bring up a window with what is called a command-prompt where you can type in commands to get the computer to do something that you want it to do. In each of these interfaces, there is a program run- ning that reads what characters you have typed in, and translates them into actual commands that the computer hardware can understand. This program is called the command-interpreter, or \u201cthe shell.\u201d2

In the strictest of senses, DOS, as a command-line environment, has a shell, called
\u00a3\u00a5
\u00a4\u00a7
\u00a6\u00a8
\u00a6\u00a8
\u00a9\u00a8
\ue000\ue002
\ue001\ue004
\ue003\ue005
\u00a3\ue006
\u00a4\u00a7
\u00a6
. On
Unix systems, there are many available shells.
1.2 WhatareShellScripts?
A shell script is a text \ufb01le containing a set of commands to run within the shell. A simplistic example
of a shell script would be a Windows
\ue003\ue008
\ue007\u00a5
\u00a9\ue002
\ue009

\ufb01le. Advanced shells support more complete or convenient command sets within such a \ufb01le (script). In most cases, the contents of that \ufb01le could be typed into the command-prompt and the effect would be the same as running the script.

In Unix, since there are many, many shells available, scripts can be written for any of the shells. And since each shell may use different syntax and may have different capabilities, each shell script must iden- tify which shell it should be interpreted3 with to get the correct behavior. In Unix, the way that shell scripts identify the correct shell is the \ufb01rst line of the script. The \ufb01rst line of the \ufb01le is a shebang4 followed by the absolute path to the shell (generally, shells are kept in

\u00a7
!
\ue00a#
"
). Here is a standard example:
$\ue004
%
\u00a7
!
\ue00a&
"
\u00a7
\u00a9
\u00a2
\u00a1
By convention, the hash symbol (#) is almost always a comment delimiter in shell scripts (and behaves
similarly to the C++ comment delimiter,
\u00a7
\u00a6
\u00a7
).
1.3 WhyUseShellScripts?

Some people look at shell scripts and think \u201cHey, that looks like another programming language! I\u2019ll just do it in the programming languages that I\u2019m used to.\u201d And for most things, you could. However, shell scripts make doing simple tasks easy. For example, if you wanted a program to look at a directory full of JPEG image \ufb01les, select one of them at random, and set that JPEG \ufb01le as the desktop picture, you

couldwrite a C program to do that. But then you\u2019d have to use the various system calls, and then learn

the Xwindows API commands for setting the desktop picture, trap for errors, compile, and then hunt down the bugs. Or, you could write a \ufb01ve-line shell script that uses the pre-made tools that come with X (twelve lines, if you want to be fancy). If you want to monitor

\u00a7
\u00a8
\ue000
\u00a9
\u00a3
\u00a7
\u00a6
\ue001
\u00a1
\u00a4
\ue002
\u00a7
\ue003
\ue006
\ue005
\u00a6
\u00a9
\ue002
\ue005
, and you want to have
the computer email you a sorted copy of it when there are \ufb01ve lines that match a speci\ufb01c pattern, you
couldwrite a C program that opens
\u00a7
\u00a8
\ue000
\u00a9
\u00a3
\u00a7
\u00a6
\ue001
\u00a1
\u00a4
\ue002
\u00a7
\ue003
\ue006
\ue005
\u00a6
\u00a9
\ue002
\ue005

, then uses your hand-made pattern matching algorithm to look for this one speci\ufb01c pattern, and counts how many there are, saves them in memory, uses your hand-made sorting algorithm, opens a TCP port, connects to your mail server, talks SMTP (use

1Because, as you will realize by the end of this document,
')
()
02
1
is the \u201cone true shell,\u201d and all others are pretenders to the throne.
2It is called a \u201cshell\u201d because all (well, almost all) commands that you can execute will run as a child process of the shell program
\u2013 and therefore will inherit (or be \u201cin\u201d) the shell\u2019s \u201cenvirionment.\u201d (see theEnVariables section) Thus, because the shell provides the
environment, it can be thought of as a shell around the commands that are given to it.
3\u201cInterpreting\u201d a shell script is almost always the same as running or executing the contents of the shell script, and most people
use the terms interchangeably.
4A shebang is a hash symbol (#) followed by a bang, also known as an exclamation mark (!).
1

Activity (25)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Wahiba Wahiba liked this
Shekhar Singh liked this
Salem Mabrouk liked this
qayxswedcvfr liked this
sharad_lk liked this
sharad_lk liked this
Vamshi Krishna liked this
asdf344 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->