Академический Документы
Профессиональный Документы
Культура Документы
INTRODUCTION
A shell is an interactive command-line interface to an operating system. There are several Unix shells; among them, “tcsh” and
“bash” are both popular. “tcsh” is a modern version of the “csh” program, whereas “bash” is a modern version of the “sh” program.
They have similar capabilities, but the command interface has minor syntax variations. A “shell script” is a sequence of shell
commands which achieves something meaningful. You can also call it a “shell program.” Shell commands include ways to (a) declare
variables (b) assign variables and manipulate them e.g. carry out arithmetic operations (c) perform if-then-else type of control
sequencing based on the settings of variables and (d) iterations using “for” and “while” constructs. Shell scripts are a great way to
improve your personal productivity. As an example, suppose that you receive a “ZIP” file from a friend which contains a number of
files, including some PDF files. You wish to print each PDF file and then remove the PDF file since it takes too much file space. A shell
script can be written to perform these actions without manual intervention. You can write the script to receive the folder as an
argument, so that the script can be reused again, if required. While graphical user interfaces to operating systems are easy to use,
you will need to learn a command-line interface to become more productive in an industry.
GET STARTED!
In this online training, you will learn to use “bash.” You will need access to a computer with an Internet connection.
This website provides an interactive shell where you can practice “bash” commands. When you visit this website, you will
see three windows. Of these the “green” window at the bottom is where you can type “bash” commands. The window on
the top right is where you can create shell scripts. If you logout and go back to the website again next day, your work will be
lost – you will start with a clean slate again! It is recommended that you do all the commands mentioned in Step 2 in one
sitting (You can take breaks, but do not logout.).
STEP 2.
Visit the following website for online documentation of commands in “bash.” https://ss64.com/bash/ The commands are
listed in the alphabetical order. Clicking on the name of the command can provide additional details about the command.
Although you will see a whole lot of commands, we recommend that you go through them in the following order. Shell is
case sensitive! Be careful about the spaces between commands – sometimes, the command will be interpreted differently
depending on whether a space is provided/not provided.
30 cp f* subdir Copy all files whose names begin with “f” to subdir.
Notice that “*” is a wildcard character in shell.
Tip2 After typing cp f*, type s “bash” will help you complete the filename “subdir”.
and then type ESCAPE. You can try “ESCAPE” any time when you are trying to
type a file name.
31 echo * Echoes the names of all files in the current working
directory!
32 echo “*******” This is the correct way to print a string of
asterisks! Don’t forget the quotes. (What will
happen if you do? Go to [31] if you forgot.)
33 ls –l subdir/* Shows all files in subdir
34 ls –l subdir/f* Shows all files in subdir, but their filenames must
begin with “f”
35 help > helpfile; cat Two commands are separated by a “;”. They will be
helpfile executed one by one, from left to right. You can use
“;” to separate more commands. “cat” is a way to
type out files. “cat” is a short form for
“concatenate.”
36 echo “Hello” > h; echo We created three files called h, w, and hw. The file
“World” > w; cat h w > hw; hw is the concatenation of files h and w. Verify
cat hw this by ls –l.
37 more hw Allows you to view the file hw in leisure. Use space
bar to view more of the file. Use “b” to go
backward. Use “q” to quit browsing the file.
38 less hw Another way to browse the file. Similar to “more.”
39 wc –l h Shows the number of lines in file “h”. Also try “wc
–w” to get the number of words, and “wc –c” to get
the number of characters.
40 cat h w | wc -l This is an example of the “pipe” construct in shell.
The two commands are executed side by side (not one
after another). The output of “cat h w” command will
be sent as input to the “wc –l” command. Thus wc –l
will be applied to a “ghost” file which contains the
concatenation of files h and w.
41 ls ? “?” is the set of all files which have a single
character name such as “h” and “w”. Similarly, try
“ls ??” or “ls h?”. Note that “?” is sometimes
useful in locating files that match a particular
pattern.
42 seq 1 5 The list of numbers from 1 to 5 is printed
43 x=$(seq 1 5); echo $x The first command will run “seq 1 5” and return its
output to a variable called x. The second command
echoes the variable x.
44 allfiles=* Explain what happened. Verify by doing “echo
$allfiles”
45 for file in $allfiles When you type the first line with the “for”
do statement, you will see that “bash” puts out a “>”
wc –l $file prompt. Type the other commands after “>” prompt.
done This “for” loop ran a “wc –l” command on every file
in the directory.
46 x=10 This is the “while” loop in bash. What is the value