Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux+ Certification Bible.pdf
Скачиваний:
46
Добавлен:
15.03.2015
Размер:
3.78 Mб
Скачать

Chapter 10 Linux Terminals and Shells 341

Option

Description

 

 

--numeric-ports

Shows numerical port numbers but does not affect the resolution

 

of host or user names.

 

 

--numeric-users

Shows numerical user IDs but does not affect the resolution of

 

host or port names.

 

 

-o --timers

Includes information related to networking timers.

 

 

-p --program

Shows the PID and name of the program to which each socket

 

belongs.

 

 

-r --route

Displays the kernel routing tables.

 

 

-s --statistics

Displays summary statistics for each protocol.

 

 

-v --verbose

Verbose mode that explains what is being done.

 

 

Netstat is a powerful tool that is used by experienced network administrators, but can also be used as a basic tool to see active connections to a system.

The use of any Linux command, such as netstat, requires the use of the proper syntax. Know the basic structure of the command and the switches.

Common Shell Commands

4.10 Use common shell commands and expressions.

Basic tools are built into the shells, which are started as the environment when a user or administrator logs in to a Linux system. The most common shell for Linux is Bash or Bourne Again SHell. Other major shell programs used for Linux are Public Domain Korn Shell (PDKSH), the TCSH shell, and the Z-shell.

See Chapter 8 for more information on shell programs.

Bash is an sh-compatible command language interpreter, which is intended to conform with the IEEE POSIX Shell and Tools specification. Bash also incorporates useful features from the Korn and C shells (ksh and csh). The following are the configuration files for Bash:

/bin/bash: The bash executable

/etc/profile: The system wide initialization file for login shells

~/.bash_profile: The personal initialization file for login shells

342 Part IV Administration

~/.bashrc: The individual per-interactive-shell startup file

~/.bash_logout: The login shell cleanup file that executed when the shell exits

~/.inputrc: Individual read line initialization file

Instructions for the Bash shell are very large and complex — in fact, the MAN page contains over 5000 lines of text, and thus covering this topic would require another book. Some of the most common and useful tools included in the shell are shown in Table 10-2.

Table 10-2

Bash Shell Commands and Tools Used

Option

Description

 

 

Arrow up and down

Scrolls through recent commands used.

 

 

|

Uses the format command1 | command2 and places the

 

output of command1 and inputs into command2.

 

 

&&

Uses the format command1 && command2 and

 

command2 is executed if, and only if, command1 returns

 

an exit status of zero.

 

 

||

Uses the format command1 || command2 and command2

 

is executed if and only if command1 returns a non-zero exit

 

status.

 

 

alias

Alias with no arguments or with the -p option prints the list

 

of aliases in the form alias name=value on standard output.

 

 

bg [jobspec]

Resumes the suspended job jobspec in the background, as

 

if it had been started with &. (background=bg)

 

 

break [n]

Exit from within a for, while, until, or select loop.

 

 

cd

Changes the current directory to dir.

 

 

continue [n]

Resumes the next iteration of the enclosing for, while, until,

 

or select loop.

 

 

cut [options][FILE...]

Prints selected parts of lines from each FILE to standard

 

output.

 

 

dirs

This displays the list of currently remembered directories.

 

 

echo

Outputs the args, separated by spaces, followed by a new

 

line.

 

 

enable

Enables and disables built-in shell commands.

 

 

Chapter 10 Linux Terminals and Shells 343

Option

Description

 

 

exit [n]

Causes the shell to exit with a status of n.

 

 

find [path...] [expression]

Searches the directory tree rooted at each given file name

 

by evaluating the given expression from left to right,

 

according to the rules of precedence until the outcome is

 

known at which point find moves on to the next file name.

 

 

fg [jobspec]

Resumes jobspec in the foreground, and makes it the

 

current job. (foreground=fg)

 

 

grep [options][FILE...]

Grep searches the named input FILE.

 

 

help [-s] [pattern]

Displays helpful information about built-in commands.

 

 

history

Displays the command history list with line numbers.

 

 

kill [-s sigspec | -n signum |

Sends the signal named by sigspec or signum to the

-sigspec] [pid | jobspec]

processes named by pid or jobspec.

 

 

logout

Exits a login shell.

 

 

pwd

Prints the absolute pathname of the current working

 

directory.

 

 

read

One line is read from the standard input, and the first word

 

is assigned to the first name, the second word to the

 

second name, and so on, with leftover words and their

 

intervening separators assigned to the last name.

 

 

umask [-p] [-S] [mode]

The user file-creation mask is set to mode.

 

 

unalias

Removes each name from the list of defined aliases.

 

 

wait [n]

Waits for the specified process and returns its termination

 

status.

 

 

This is by no means the definitive list of tools for Bash. With experience, you will learn more of the included shell commands and the necessary commands for each environment. These common commands can help make Linux tools — especially the history tool — easier to use for everyone.

Basic shell scripts

4.18 Program basic shell scripts using common shell commands (e.g., grep, find, cut, if)

344 Part IV Administration

You can use the common shell commands to create some very simple scripts to perform routine tasks. You can chain together a number of basic commands to perform a specific task. Here is an example of a shell script:

#!/bin/sh

# This script changes to your home directory and does a directory list

cd /home/user1 ls -al

In this simple script, all we are doing is changing to a user’s home directory, and listing the contents of that directory.

The first line identifies this as a shell script that is passed to the BASH shell program at /bin/sh. This is important to ensure that the script is run under the BASH shell and not another one such as C shell.

The second line is a comment, telling the user what the script does. All comments are preceded with a “#” mark.

The next two commands are simply BASH shell commands to change to the user home directory, and list the contents.

Here is a list of common commands that can be very useful when programming shell scripts:

grep — This command searches for a specific pattern of text in a file. This is useful for searching large amounts of text for a certain name or error message.

find — This command is similar to grep, as it searches a directory for a pattern or file that you are looking for.

cut — This command removes sections of text from lines of a file.

if — If you need to perform some type of logical decision making within a script, you would use the if command. For example:

if [ “yes” = “yes” ]; then echo “The answer is yes”

These simple examples barely scratch the surface of the many large tasks you can perform using shell scripts.

Соседние файлы в предмете Операционные системы