Académique Documents
Professionnel Documents
Culture Documents
Unit 1
ABI/API Compatibility
Unix Flavors
UNIX from AT&T, at Bell Labs, in 1969. AIX from IBM HP/UX from Hewlett-Packard SunOS (Later Solaris) from Sun IRIX from SGI (Silicon Graphics)
Unix Principles
Everything is a file.
Including hardware
Configuration data stored in text Small, single-purpose programs Avoid Captive User Interfaces Ability to chain programs together to perform complex tasks.
5
Goal : Create Free UNIX clone By, 1990, Nearly all required userspace application created. gcc, emacs etc. Non-profit Organization that manages the GNU Project Four Freedoms
http://www.gnu.org/philosophy/free-sw.html
6
Primary license for Open Source Software Encourages free software All enhancements and changes to GPL-software must also be GPL Often called CopyLeft
http://www.gnu.org/copyleft/gpl.html X Window System, has its own T&C. BSD code need to be abide the terms of Berkeley Software Distribution. Visit http://www.bsd.com
7
Linux Origins
Linux Torvalds
Why Linux?
Fresh Implementation of UNIX APIs. Open Source Development model. Supports wide variety of hardware. Supports many networking protocols and configurations. Fully supported. Linux is a UNIX-like OS. Multi-user and Multi-tasking. Wide hardware support. Checklist. Fully Supported.
9
A distribution of Linux
Custom version of a recent Linux Kernel. Utilities and applications. Installation and configuration software Support available. RHEL includes installation and configuration software, a patched and rigorously tested recent version of the Linux Kernel, and thousands of utilities and applications.
10
Or
64-bit Intel/AMD with 512 MB RAM 2-6 GB Disk Space Bootable CD Other processor architectures supported
Virtual Consoles
Virtual Consoles are available through CTRL ALT F[1-6] If X is running, it is available as CTRL ALT F7 The virtual consoles enable a user to have multiple logins even when not using an X Window system. You can scroll at the virtual consoles by using SHIFTPAGEUP and SHIFT-PAGEDOWN. The scroll buffer is stored in video memory, so if you are running a graphical program such as X, the scroll buffer is lost.
12
The goal of X.Org include a faster and more open development model than Xfree86, support for a wide variety of video cards and input devices and the development of a highly modular and flexible graphical framework for Unix and Linux. http://xorg.freedesktop.org
http://www.x.org/wiki X Client activity that spawns an X server event informing the web browser to send an HTTP request to the links target ( or anchor). You do not really see the X server, but X clients. X provides the data I/O infrastructure for X clients, like a human nervous system, it sends messages when touched by client activity. An X Client running on one system can display on any X server running on any operating system, if sufficient access is granted. For each managed display, this socket is /tmp/.X11-unix/X# where # is 0 to the greatest number of permitted connections.
13
Or
Collections of applications that provide a graphical working environment with a consistent look and feel. GNOME (GNU Network Object Model Environment) The default desktop environment. KDE ( K before L) Environment based on the Qt toolkit.
14
Starting Xorg
Nothing needed if system boots to a graphical login. Just authenticate. If system boots to a virtual console login, Xorg must be started manually.
Run startx to manually start Xorg. At least 6 but no more that 255 characters. Other criteria depends on particular OS.
15
Changing Password.
Unit 2
16
Getting Help
--help whatis -- with makewhatis man info /usr/share/doc/html/en/ Software source code Documentations
17
Running Commands
Anything in straight braces ([]) is optional. Anything followed by represents an arbitrarylength list of that thing. If you see multiple options separated by pipes (|), it means you can use any one of them. Text in straight brackets(<>) represents variable date. So <filename> means insert the filename you wish to use here. Sometimes, such variables are simply written in all CAPS.
19
The collection of all man pages on a system is called the Linux Manual. The Linux is divided into sections, each of which covers a particular topic, and every man page is associated with exactly one of these sections. The sections are:
Manual Sections.
1 2 3 4 5 6 7 8
User Commands System Calls Library Calls Special Files File Formats Games Miscellaneous Administrative Commands
20
info [<command>] If you run info with no arguments you will be presented with a list links to the top nodes of every available info page.
21
node s[<text>] Search for text (default: last search) q Quit Viewing page
If you prefer the navigation keys used by man, such as using /, n and N to search, you can start info with the vi-keys arguments.
22
Extended docs
/usr/share/doc http://www.redhat.com/docs
23
Miscellaneous
man f foo is the same as whatis foo. If your system has just been installed, neither man f nor whatis will work until an administrator runs makewhatis. ls lh
24
Unit 3
25
Files and directories are organized into a single-rooted inverted tree structure. Filesystem begins at the root directory, represented by a lone / (Forward slash) character. Names are case-sensitive. Paths are delimited by /. A users path is a list of directories that are searched for commands typed at the command line.
26
The essential binaries necessary to boot and maintain the system reside in /bin for regular binaries and /sbin for system binaries. Non-essential binaries, such as graphical environments, web browsers, office tools and so forth, are installed in /usr/bin and /usr/sbin. On a newly installed system, there will also be /usr/local/bin and /usr/local/sbin directories, but they will be empty. CD-ROM is mounted under /media/cdrom folder. Filesystems that are on non-removable media but are not part of the RHEL, hierarchy are usually mounted under /mnt. Visit http://www.pathname.com/fhs
27
/etc -System Configuration Files /tmp -Temporary files /boot -Kernel and boot loader /var and /svr -Server data /proc and /sys -system Information The lib directories
On Folders
Once a day the system automatically deletes any files over seven days old in /tmp and its subdirectories. The /var directory contains regularlychanging system files such as logs, print spools and email spools. In the future, server data may be moved to the /srv directory so that /var only contains logs, spools and so forth. Each shell and system process has a current working directory (cwd).
29
Names may be up to 255 characters. All characters are valid, except the /.
It may be unwise to use certain special characters in file or directory names. Some characters should be protected with quotes when referencing them. Example: MAIL, Mail, mail and mAiL Again, possible but may not be wise.
To access a file with name contains special characters, enclose the filename in quotes.
30
Absolute Pathnames
Absolute pathnames begin with a slash (/) Complete road map to file location. Can be used anytime you wish to specify a file name. To change to your previous working directory
$ cd
A dash (-) represents your previous working directory. Its a handy shortcut to use to switch back and forth between two directories.
31
More than one file may be copied at a time if the destination is a directory.
If final argument is an existing directory, a copy of the source files are placed in that directory with the same name as the source. Otherwise, the destination is interpreted as a file name, and a copy of the source file is created with that.
32
On Copying
When copying a single file to a destination, cp first checks to see if a directory exists with the destination name. If it does, a copy of the source file is placed there with its original name. If not, the destination is assumed to be a new file name, and a copy of the source file is made with the destination name.
33
More than one file may be moved at a time if the destination is a directory.
If the destination directory does not exit, the slash will cause the command to fail with an error message. If the destination directory does not exist (but the destination pathname is valid), the source directory will be moved to the destination directory with the new name.
34
touch create empty files or update file timestamps The r option tells rm to remove files recursively and thus it will delete directories and their contents. There is no way to undo the effects of rm, except to restore from a backup. rmdir will only remove empty directories. To remove a directory and its contents, use rm r.
35
Using Nautilus
Gnome graphical Filesystem browser Can run in Spatial or Browser mode Accessed via
Desktop icons
Typing CTRL-SHIFT-w closes all parent windows. Spatial or Browser Mode can be edited.
36
Home: Your home directory Computer: Root Filesystem, network resources and removable media. File Browser option on Application menu.
Left-button: Move on same filesystem, copy on different system. Ctrl-Left-button : Always Copy Ctrl-Left-button: Ask whether to copy, move or create symbolic link (alias) Right-click or rename, cut, copy or paste.
37
Context Menu
Files can contain many types of data. Check file type with file before opening to determine appropriate command or application to use. Syntax
38
Syntax:
-A Show all characters, including control characters and non-printing characters. -s Squeeze multiple adjacent blank lines into a single blank line. -b Number each (non-blank) line of output.
39
cat [options] [files..] Contents of the files are displayed sequentially with no break. Files display concatenated.
Syntax:
/text n
v
On less
move ahead one full screen move ahead one line move ahead (down) half a screen move to the top of the file. search forward for text repeat last search quit move back one full screen move back one line move back (up) half a screen move to the bottom of the file search backward for text repeat last search, but in opposite direction. open file in a text editor (vi by default)
41
less options
-c -r
-s
-e
-m
Clear before displaying. Screen is redrawn instead of scrolled between screens. Display raw control characters. (May cause display problems) squeeze multiple blank lines into a single blank line Exit the second time it reaches the end of the line. Verbose prompt, like more (by default it uses a colon).
42
Unit 4
43
bash introduction
Bourne Again Shell Successor to sh, the original Unix shell Developed for the GNU Project The de factor standard Linux Shell Backward-compatible with Bourne shell (sh)- the original (Standard) UNIX shell
44
Implements many of the best features from earlier shells: sh, csh, ksh , tcsh Command line completion Command line editing Command line history Sophisticated prompt control
45
The use of wildcards, or metacharacters, allows one pattern to expand to multiple filenames by a process called globbing. RHEL uses UTF-8 encoding of characters, which means that each capital letter comes directly after the corresponding lower-case letter. Use echo ?o* to find the effect of wild characters.
46
* - matches zero or more characters ? matches any single character [a-z] matches a range of characters [^a-z] matches all except the range.
Tilde(~)
!! to repeat last command !c to repeat last command that started with c !n to repeat a command by its number in history output !?abc to repeat last command that contains (as opposed to started with) abc !-n to repeat a command entered n commands back. User ^old^new to repeat the last command with old changed to new. May refer to your home directory ( ~/.bash_profile) May refer to another users home directory. (~gmustafa/.bashrc)
47
Parameter/Variable: ($)
$ cd $HOME/public/html
To see a list of variables and their values, run the set command. Curly braces: { }
A string is created for every pattern inside the braces regardless if any file exists.
$ rm hello.{c,o}
Command Output or $ ()
$ echo Hostname: `hostname` $ echo Hostname: $(hostname) Substitute result of arithmetic expression in a command line. $echo Area : $[$X * $Y] $echo Area : `expr $X\*$Y`
49
Arithmetic - $[]
Backslash (\) is the escape character and makes the next character literal.
Used as last character on line to Continue command on next line $find / -name foo* $find / -name foo\*
50
$echo *** SALE ***` $ (dollar sign variable expansion (backquotes), command substitution \ (backslash), single character inhibition ! (exclamation point), history substitution
Double quotes inhibit file name generation expansion, but not other types.
51
History Tricks
Use the up and down arrow keys to scroll through previous commands. Type <CTRL-R> to search for a command in command history.
{reverse-i-search)`:
You can ignore repeated duplicate commands and repeated lines that only differ in pre-pended spaces by adding the following in your .bashrc
<ESC>, (the escape key followed by a period) <ALT -.> (hold down the alt key while pressing the period)
export HISTCONTROL=ignoreboth
52
line. <Ctrl-e> moves to the end of line. <Ctrl-u> deletes to beginning of line <Ctrl-k> deletes to end of line. <Ctrl-arrow> moves left or right by word. * Inherited from emacs.
<Ctrl-a>
moves to beginning of
53
By default, bash uses emacs-style keybindings for command editing. Type set o vi to change keybinding to vi-style. Make vi the default by adding the command above to $HOME/.inputrc
54
gnome-terminal
Applications/System Tools/Terminal Graphical terminal emulator with support for maintaining multiple tabbed shells simultaneously. <Ctrl-Shift-t> Open a new tab <Ctrl-PgUp/PgDn> Next/Prev tab <Ctrl-Shift-c/v> Copy/Paste
55
Unit 5
56
Standard input keyboard is default Standard Output Terminal windows is default Standard Error Terminal window is default.
Error messages could be saved in a file with the normal output going to the monitor. 0,1,2 are called file descriptor.
57
Shell redirection operators allow standard I/O channels to be redirected to/from a file. Pipes allow standard I/O channels to connected to the input or output of programs.
Although it is also possible to pipe standard error into a file using some fairly complex syntax, this is generally not done.
58
Redirecting > command >> command < command 2> command 2>> command
Directs standard output of command to file Appends standard output of command to file. command receives its input from file. Error messages from command are directed to file. Error messages from command are appended to file.
Piping
| command1 | command 2
Pipes the standard output of command1 into the standard input of command2.
59
Overwriting or Appending
If the target file of file redirection with > already exists, the existing file will be overwritten. To append data to an existing file, use >> to redirect instead of >. Redirect standard output with >
Standard output is displayed on the screen. Redirecting further standard error, appending to the same file, with 2>>.
61
Each I/O Channel can be redirected to different files, or to the same file:
find / -name passwd > alloutput 2>&1 (or) find / -name passwd &> alloutput
62
Redirecting Input
Redirecting Standard Input with < Some commands only operate on Standard Input
tr doesnt accept filenames as argumentsit requires its input to be rediected from somewhere. tr A-Z a-z <.bash_profile
Pipes ( the | character) let you redirect output from one command to become the input to another command.
$ ls /usr/lib | less
On Pipes
And
Expect the output of every program to become the input to another, as yet unknown, program. And then Use pipes to do complex jobs on the fly. Any command that writes to standard output can be used on the left-hand side of a pipe. Any command that reads from standard input can be used on the right-hand side of a pipe. Multiple commands can be chained together with pipes.
65
Lets you tee a pipe: redirect output to a file while still piping it to another program. $set | tee set.out | less In example, output from set is written to file set.out while also being piped to less.
Command | tee stage1.out | sort | tee stage2.out | uniq c | \ tee stage3.out | sort r | tee stage4.out | less
66
Unit 6
67
Users and groups are used to control access to files and resources. User log in to the system by supplying their user name and password. Every file on the system is owned by a user and associated with a group. Every process has a owner and group affiliation, and can only access the resources its owner or group can access.
68
Users
Every user of the system is assigned a unique User ID number( the uid). Users names and uids are stored in /etc/passwd Users are assigned a home directory and a program that is run when they log on (Usually a shell). User cannot read, write or execute each others files without permission.
69
Groups
Users are assigned to groups with unique group ID numbers (the gid). gids are stored in /etc/group. Each user is given their own private group.
All users in a group can share files that belong to the group. The primary group can be changed using the newgrp command.
70
The root user: a special administrative account. Sometimes called the superuser. root has complete control over the system.
Every file and directory has permissions set that determine who can access it. Permission are set for:
The owner of the file (called the user, arguably a misnomer) The group members All others
Permissions that are set are called read, write, and execute permissions.
72
Permission Types
A file may be removed by anyone who has write permission to the directory in which the file resides regardless of the ownership or permissions on the file itself. The first character of the long listing is the file type.
permission to read a file or list a directorys content wpermission to write to a file or create of remove files from a directory. x permission to execute a program or change into a directory and do a long listing of the directory. - no permission (in place of r, w, or x)
73
When a process accesses a file the user and the group of the process are compared with the user and group of the file.
Every process runs as a under the authority of a particular user and with the authority of one or more groups, this is called the processs security context.
74
If the user matches, the user permission apply. If the group matches, but the user doesnt, the group permission apply. If neither match, the other permissions apply.
Examples:
First digit specifies owners permissions. Second digit specifies group permissions. Third digit represents others permissions.
4(for read) 2(for write) 1(for execute)
Example:
Unit 7
77
vi: the visual editor, standard Linux and Unix editor vim: the vi improved editor, standard Red Hat editor On Redhat operating systems, the vi command invokes vim. Derived from earlier Unix editors
ed ex vi vim
78
To start vim: vi filename If the file exists, the file is opened and the contents are displayed. If the file doesnt exit, vi creates it when the edits are saved for the first time. To use vi instead:
unalias vi or \vi
79
vi r myfile
vi x myfile
file is non-modifiable file is only modifiable by using the exclamation point :w! do not use a swap file for backup (useful for floppies) recover data from a swap file after a crash encrypt file when saving, decrypt when editing
80
Command Mode
Insert mode
ex mode
Cursor Movement
h left j down k up l right w word ahead b word back ( Sentence Back ) Sentence forward { Paragraph above } Paragraph below Arrow keys (, , etc) also work. A word is defined as a series of letters of the alphabet and numbers uninterrupted by white space or punctuation. If the cursor is on a punctuation character, the word is terminated by white space or a letter of the alphabet or number.
82
a i o
append after the cursor. insert before the cursor. open a line below.
A append to end of line. I insert at beginning of line. O Open a line above. Pattern such as these permeate the vi and vim commands.
83
<Esc> Takes you from insert mode back to command mode Hint: When in trouble, press <Esc> and then press <Esc> again. By doing this, you can guarantee that, regardless of the mode you were in, you will be now in command mode.
84
A line is yanked into a buffer, presumably to be put (or pasted) back into the document at another location.
85
Put (Paste)
Use p or P to put (paste) copied or deleted data For line oriented data:
p puts the data below the current line P puts the data above the current line
p puts the data after the cursor. P puts the data before the cursor.
Jus as in vi and vim, we yank instead of copy, we put instead of paste; take data from a buffer and place it in the documents.
86
Undoing Changes
u U
undo most recent change undo all changes to the current line since the cursor landed on the line. <Ctrl-r> redo last undone change. The u will not undo a previous u; that is, it will not toggle a change, but rather undo several pervious changes. To undo all successive changes to the current line, use the U command. To redo a change undone by a u command, use the <Ctrl-r> command.
87
/text ?text n N
search downwards for text Search upwards for text continue search in the same direction. Continue search in the opposite direction.
88
Command-Mode Tricks
dtc 5dd
x rc r
delete from cursor to the letter c (does not span lines) delete five lines( a number can precede any of the two character change, delete, yank or put commands). delete a character replace a character with c replace character-for-character until <Esc>
89
Forcing Changes: Save changes Exit :wq! Do not Exit :w! Abandon changes, staying in the editor, through :e! and force a write through :w!. Finally, force a write through :w1!. Finally, force a write and quit with :wq!. The last command will forcibly write the file, but only quit if the forcible write was successful.
90
Printing in Linux
Printers may be local or networked. Print requests are sent to queues. Queued jobs are sent to the printer on a first come first server basis. Jobs may be canceled before or during printing. It support CUPS IPP (Common Unix Printing Subsystem), lpd( Linux printing daemon), windows, Netware and JetDirect printers.
91
Printing commands
lpr
lpq View the contens of the queue. lprm Remove a job from the queue. System V printing commands such as lp, lpstat and cancel are also supported. A user may only remove his own print jobs from the queue.
92
Printing Utilities
enscript Converts text to PostScript a2ps Converts text to PostScript ggv(GNOME GhostView) PostScript and PDF Viewer xpdf PDF viewer. ps2pdf PostScript to PDF converter. pdf2ps PDF to PostScript converter. mpage Prints multiple pages per sheet.
93
Section 2
Red Hat Enterprise Linux: A Deeper Look
94
Unit 8
95
Disk drives are divided into partitions. Partitions are formatted with filesystems, allowing user to store data.
A filesystem is a data structure written to the media that allows users to store and access files. Ext2 at 1993, ext3 has Journaling to improve filesystem data integrity. EA Extended Attributes ACLs POSIX Access Control Lists
Default filesystem: ext3, the Third Extended Linux Filesystem. Other common filesystems: Ext2 and msdos (typically used for floppies) Iso9660 (typically used for CDs)
96
Inodes
An inode table contains a list of all files in an ext2 or ext3 filesystem. An inode (index node) is an entry in the table, containing information about a file (the metadata), including:
File type, permissions, link count, UID, GID The files size and various time stamps Pointers to the files data blocks on disk. Other metadata about the file
Directories
The computers reference for a file is the inode number. The human way to reference a file is by the file name. A directory is a mapping between the human name for the file and the computers inode number. When a filename is referenced by a command or application, Linux references the directory in which the file resides, determines the inode number associated with the file name, looks up the inode information in the inode table, and , if the user has permission, returns the contends of the file. The ls i command displays the inode number.
98
cp and inodes
The cp command:
Allocates a free inode number, placing a new entry in the inode table. Creates a directory entry, referencing the files human file name to the inode number. Copies data into the new file.
When a file is copied to a new name in the same directory, the directory and the inode table get a new entry.
99
mv and inodes
If the destination of the mv command is on the same filesystem as the source, the mv command:
Creates a new directory entry with the new file name Deletes the old directory entry with the old file name Has no impact on the inode table (except for a time stamp) or the location of data on the disk. No data is moved!
100
rm and inodes
The rm command:
Decrements the link count, thus freeing the inode number to be reused. Places data block on the free list. Removes the directory entry.
Data is not actually removed, but will be overwritten the data blocks are used by another file.
101
A symbolic link points to another file ls l displays the link name and the referenced file.
Syntax:
File type: l for symbolic link The content of a symbolic link is the name of the file that it references.
Removing a symbolic link removes the actual link itself, not the underlying file
102
ln -s filename [linkname]
Symbolic link is a separate file from the original. The first character of along listing for a symlink is the letter l. Symlink has permission of 777. Permission of symlink are irrelevant; the permissions set on the file pointed to by the symlink control access rights. The size of the symlink is always the number of characters in the path name.
103
Hard Links
One physical file on the system. Each link reference the files inode. File is present in the filesystem as long as at least one link remains. Cannot span drives or partitions. Syntax:
A hard link is a path name that references an inode: that is , all files are hard linked at least once.
104
ln filename [linkname]
On Hard Links
The file names must be on the same filesystem: because they share an inode number and an inode table is unique to a file system, both must be on the same file system. It is not possible to use the ln command to create additional hard links to directories.
105
d l b c p s
regular files directory symbolic link block special file character special file named pipe socket
106
Extra Filetypes
A file that passes data between processes. It stores no data itself, but passes between one process writing data into the named pipe and another process reading data from the named pipe. A named pipe can be created using the mknod command:
named pipe:
ls l /dev | less
A stylized mechanism for interprocess communication. It is extremely rare for a user or even a system administrator to explicitly create a socket.
107
socket:
mknod
mypipe p
Miscellaneous tools
108
Unit 9
109
Local Variables Aliases and functions The set and shopt commands
The shell can also configure other commands or applications through environment variables.
110
Variables
A variable is a label that has a value. Used to configure the shell or other programs. Variables are resident in memory. Two types: local and environment Local Variables are used only by the shell. Environment variables are passed onto other commands. Display variables and values using: set to display all variables. env to display environment variables. The set, env and echo commands can be used to display all variables, environment variables, and a single variable value, respectively. set | less env | less echo $HOME
111
Data in Shell scripts and environment settings stored in variables. Conventionally all upper-case. Setting variable value:
$FAV_COLOR=blue
For a list of variables that configure the shell, see the Shell Variables section of the bash man page.
112
HISTFILESIZE Determines how many commands to be saved in the history file on logout. COLUMNS Sets the width of the terminal( xterm, gonme-terminal or kterm) LINES Sets the height of the terminal ( xterm, gonme-terminal or kterm) HISTFILE Specifies the file in which history commands are stored on logout HISTSIZE Specifies the number of history commands to keep while operating interactively.
113
PS1 sets the prompt Uses escape sequences to insert variable information in the prompt.
For a complete list of these prompting escape sequences, see the PROMPTING section of the bash man page.
114
\d the date \h short hostname (not the FQDN) \t the current time \u user name (useful if you have multiple accounts) \w the current working directory. \! The history number of the current command. \$ show $ if you are non-privileged user and a # if you are a privileged user, useful if you sometimes become superuser.
Aliases
Aliases let you create shortcuts to commands $ alias dir=ls laF Use alias by itself to see all set aliases. Use alias follow by an alias name to see alias value. $ alias dir $alias dir=ls laF In this case, if you ever want to use the rm command itself, instead of your alias, you can precede the command with a blackslash. \rm r Junk
115
set b
set shopt
syntax. For a complete list of set values, see the set command under the SHELL BUILTIN COMMANDS section of the bash man page. Also in this section is a list of items configurable through the shopt command.
report termination of background commands immediately, rather than waiting for next prompt. unset variables generate an error. do not clobber with > and >& operators. use vi syntax on bash command line instead of default emacs
116
Shell variables exit only in current shell instance. Environment variables passed to subshells. Shell variables can be exported into environment
To blank the value of an environment variable, use the unset built-in command. $unset EDITOR
117
LESS
The less command has many options; to force a set of options to always be used, set the LESS variable. For example: LESS=-emqs
Path to users home directory Identification of default language programs should use; example: en_US.UTF-8 for U.S. English. Users current working directory. Default editor programs should invoke for text editing. Options to pass to the less command. path to loging shell Username of user X Display Name Name of visual editor
118
TERM Environment variable setting the terminal type. reset Command (not variable) used to reset a terminal should the screen become corrupted. If a terminal is improperly set, the computer will not be able to properly display data. For more complex adjustments to your terminal settings, set the stty command. The stty command can perform a number of terminal settings to your system.
119
PATH
When a command is executed and the path is not specified, then the shell will look in these directories in the given order, stopping on first match, to find the command.
which
Use the built-in shell type command to ask the shell what is using to fulfill the following commands.
120
A simplification:
Split the line into words Function and alias expansion Curly brace string expansion Tilde expansion Parameter and variable expansion Split the line into words again File glob expansion File redirection Run the command!
121
Configure the shell by setting local variables or running the set and shopt commands. Configure other programs through environment variables Establish aliases Run program on Startup.
122
Login Shells
Login shells are first shells started (i.e. when you log in) Shells launched from a login shell typically are not login shells. Login shells and non-login shells run different startup scripts. A login shell is a shell that someone started by login onto the system. A non-login shell is a shell started up in some other way, perhaps by a user or a program issuing the bash command.
123
Login Shells
/etc/profile
~/.bash_profile
/etc/profile.d ~/.bashrc
/etc/bashrc
/etc/bashrc
/etc/profile.d
Local variable settings, particularly PS1 Environment variable settings, such as PATH or LESS. Aliases, or perhaps unalias to remove undesired aliases set globally in earlier scripts. A umask can be setted.
124
/etc/profile
System-wide startup script for login shells. Parsed by all users with Bourne-style shells, including bash and sh. Usually sets default PATH variable, user limits, and other variables and settings. Bash only sources /etc/profile if the shell is a login shell. This script will set a series of variables including PATH, USER, LOGNAME, MAIL, HOSTNAME, HISTSIZE and INPUTRC. It will also run scripts in the /etc/profile.d directory.
125
/etc/profile.d
Some application-specific startup scripts in this directory. Scripts called by a for-loop in /etc/profile Scripts set up variables and run initialization procedures. Two copies of each script are listed in this directory, a Bourne Shell style with a .sh suffix, and a C shell style with a .csh suffix.
126
For user-specific settings Common to place variable settings, aliases Commands that place output to the screen, such as the date command, should go in .bash_profile, not .bashrc. The ~/.bash_profile file is only called by login shells. Never put any command that may echo something to the screen in the ~/.bashrc file; such command belong in the ~/.bash_profile file only.
127
~/.bash_logout
Resides in home directory Executed when exiting a login shell. Useful for running programs automatically at logout Example uses:
Make backup of files Delete temporary files Display Date and time of logout.
128
/dev/sdaX
Icon created in Computer window Mounted under /media/<Device ID>
Most vendors give USB disks a label. fstab-sync reads that label and automatically puts an entry in the /etc/fstab for it, mounting it in /media/<label> Like other disks, you can use fdisk and mke2fs to create partitions and filesystems on USB disks.
129
DOS floppies can be accessed with mtools. mount and unmounts device transparently. Uses DOS naming conventions.
For a complete list of the mtools commands, run mtools and/or consult the mtools info page.
130
$fdformat /dev/fd0H1440 $mkfs t ext2 /dev/fd0 $mke2fs /dev/fd0 $mkfs t vfat /dev/fd0 mformat a:
A filesystem, one of :
Only the superuser or a non-privileged user logged into the system console can run these commands. Others do not have permission to do this.
131
tar standard Linux archiving command. Originally, tar was used to create archieves on tap devices, hence its name- which stand for tape achieve.
132
Creating an Archive
Syntax:
133
Inspecting Archives
Syntax:
First form displays a list of all files in the archive. The v causes a long listing (like ls l) of each file in the archive.
134
Extracting an Archive
Syntax
Results in smaller file size Text files can be compressed over 75% Binary files usually dont compress much, if any. tar archives are often compressed. When text files often have patterns that lead to compression ratios of up to 75%, binary files rarely compress well with 0-25% being, typical. In fact, it is possible for a compressed binary file to actually be larger than the original.
136
Compression Utilites
gzip, gunzip
bzip2, bunzip2
The gunzip command can also uncompress files, compressed with the traditional Unix compress command, making compress essentially obsolete. zip
Compatible with DOS/Windows PKzip/Winzip utilites and can compress more than one file into a single file.
137
On Compression
The traditional compress command can reduces the text file by more than half. The Standard gzip command does a better job, reducing the file to less than 1/3 of the original size. Finally, the newer bzip2 command reduces the file to /14 of the original file. The c option to the gzip command leaves the original compressed file alone, but sends and uncompressed copy of the file to standard output. The d option decompresses a file, making gzip d file.gz equivalent to gunzip file.gz
138
Compressing Archives
Often tar archives are compressed tar can compress/uncompress archives. Compression switches- use during creation and extraction
139
Low level format requied File system not needed Use tar to write to the floppy. /dev/fd0 is the destination Floppy cannot be mounted tar czvf /dev/fd0 mydir
140
Example:
Unit 11
141
User names map to user ID numbers. Group names map to group ID numbers. Data Stored on the hard disk is stored numerically. The user name and group affiliation of the file are not stored; rather, the user ID number and the group ID number are stored.
142
The /etc/passwd file contains seven fields: user name, password placeholder (for historical reasons), uid number, gid number of the users primary group, GECOS filed (usually containing the users real name), home directory, and shell to be run when the user logs in. The /etc/group file contains four fields: group password placeholder, gid number, and a comma separated list of group members. The /etc/shadow file is referenced when someone logs in: the file contains a mapping of a user name to a password. For a complete list of fields, see the man page $man 5 shadow
143
Server programs such as web or print servers typically run as unprivileged users, not as root.
Running programs in this way limits the amount of damage any single program can do the system. These accounts exist primarily so that server programs can run as non-privileged users or as particular groups.
144
su su su username su - username
145
Login/reboot history
Default Permissions
Default permission for files is 666 Default permission for directories is 777 umask is subtracted from default to determine new file/directory permissions. Non-privileged users umask is 002 Files will have permission of 664. Directories will have permission of 775. Roots umask is 022. Executive privilege is always denied a newly-create file, regardless of the umask in effect. Execute privilege always has to be explicitly granted to a file. Execute permission is given to a directory upon creation, unless explicitly denied by the umask.
147
Special Permissions
Special permissions: a fourth permission set (in addition to user/group/other) Applicable in four cases:
suid for an executable (4) sgid for an executable (2) sgid for a directory (2) sticky bit for all directory (1)
In a long listing, the suid permission is displayed as a lower case s where the x would otherwise be located for the user permission (an upper case S would be present if the underlying executable permission is not set.) Commands running with the sgid permission run with the group affiliation of the group of the command.
149
suid: Command run with permission of the owner of the command, not the executor of the command. Like passwd. sgid: command runs with group affiliation of the group of the command.
Sticky bit: files in directories with the sticky bit set can only be removed by the owner and root, regardless of the write permissions of the directory. Sgid : files created in directories with the sgid bit set have group affiliations of the group of the directory.
150
On Sticky Bits
The sticky bit for a directory sets a special restriction on deletion of files: with the sticky bit set, only the owner of the file, and the superuser, can delete files within the directory. The sgid permission for a directory means that files created in the directory will inherit its group affiliation from the directory, rather than inheiriting it from the user. Often both the sticky bit and the sgid permission will be set on a group directory.
151
Section 3
152
Unit 13
153
head
154
tail
$tail /etc/passwd
$tail n 5 /etc/passwd
tail is often used by the system administrator to read the most recent entries in the log files. Use -f to follow the end of a text file as it changes.
$tail f make.out
wc
$wc story.txt
Use l for only line count Use w for only word count Use c for only character count
156
sort
Common options
The argument to the k can be two numbers separated by a dot. In this case, the number before the dot is the field number and the number after the dot is the character within that field with which to begin the sort.
157
Reverses sort to sort descending Numeric sort ignore (fold) case of characters in strings. unique (remove duplicate lines in output). use x as field separator. sort from field POS1 Sort using fields POS1 ending at POS2.
uniq
Removes successive, duplicate lines in a file. Can use in conjunction with sort to remove all duplicates ( or use sort u) Use c to count number of occurrences of duplicate data. To print only unique line occurrence in a file (removing all duplicate lines), input to uniq must first be sorted. Since uniq can be given fields or columns on which to base its decisions, these are the fields or columns upon which its input must be sorted.
158
On uniq
Use u to output only the lines that are truly unique only occurring once in the input. Use d to output only print one copy of the lies that are repeated in the input. Use c to produce a frequency listing. Each line will be prepended with a number indicating how many times it appears in the input. Use fn or -sn to avoid comparing the first n fields or characters in each line respectively. $cut d: -f7 /etc/passwd | sort | uniq
159
cut
$cut f4 results.data -f specifies field or column -d specifies field delimiter (default is TAB)
-c cuts by characters
paste paste files together tr character translator paste combines files horizontally. It takes a line from each file and pastes them together to standard output, separated by a tab. Use d option to change the output of the output delimeter.
tr is used to translate characters; that is, given two ranges of characters, any time a character in range 1 is found, it is translated into the equivalent character in range 2. This command is commonly used in shell scripts to ensure that data is in an expected case.
161
$diff area.c /tmp/area.c 33c33 < x = y +2; ---> x = y+4; 33c33 indicates line where files differ < indicates line in first file. > indicates line in second file.
162
$aspell l will non-interactively list the misspelled words in a file read from standard input. Visit http://aspell.sourceforge.net
163
$look must
Formatting Tools
expand expand tabs into spaces fmt - reformat text into paragraph.
fmt formats its input paragraphs of the line width you specify with wn. You can also request uniform spacing (with two spaces after each sentence) with u. fmt interprets blank line in its input as paragraph delimiters.
pr
164
165
Unit 12
166
File Repositioning
G 1G
go to last line in the file go to first line in file (any number can be given and curser will jump to that line) <Ctrl-f> go forward one full screen <Ctrl-b>go back one full screen <Ctrl-d>go down half a screen <Ctrl-u>go up half a screen This is useful when an error message tells you that an error exists on the particular line of a file. You can use the G command preceded by that number to jump right to the offending line.
167
Screen Repositioning
H M L z<Enter> z-
go to first line on screen(high) go to middle line on screen (middle) go to last line on screen(low) Make current line first line on screen. make current line last line on screen.
168
Filtering
The output of a command can be placed in the file. The data in the file can be used as input of a command. Examples:
!!date
!}sort
!}fmt -66
current line only. Change lines 1 through 12 for changes in the entire file. from current line(.) to current line plus 10 lines (.+10) Example: :%s/Ohiho/Iowa/g :%s/dev/had /dev/sdag :%s/\/dev\/had/\/dev/\sda/g
170
Visual Mode
v character-oriented highlighting V line-orienting highlighting <Ctrl-v> block orienting highlighting Visual Keys can be used in conjunction with movement keys: w, ), }, arrows, etc. Highlighted text can be deleted, yanked, changed, filtered, search/replaced, etc. change d delete y yank(copy)
171
:r newfile :r !date :1,20w xfile :.,$w yfile :1,20w >>zfile :n otherfile :n! otherfile :n#
172
Configure permanetly
:set ignorecare or :se ic or :se noic :set number or :se nu or :se nonu
showmatch or :se sm or :se nosm autoindent or :se ai or :se noai textwidth=65 (vim only) wrapmargin=15 :set wrapmargin=0
173
Add the advanced material from the appendix to your skill base. Learn more configuration features. Play with filters. :help
174
Cursor Movements
0 start of current line $ end of current line ^ first nonblank character of current line. e end of next word gg top of current file n% go to a line n percent through the current file n| go to column n of the current line Read the material in :help. Learn to maneuver around the online help. Place the cursor over one of the |tags| and go to that with <Ctrl-]> keystroke, returning to the previous screen with :n#. This is a rich resource well worth being mined extensively.
175
Unit 14
176
Regular expressions are a pattern matching engine Used by many tools, including: grep, sed, less , vi, awk Values:
Two types:
Basic Extended
177
grep : the general regular expression processor, which analyzes the contents of files a line at a time, returning line that match a pattern. Sed : the stream editor, returns the contents of a file (or stream of data), performing a specified search and replace instruction. less : which uses regular expression in search commands. vi : which uses regular expression for searches (like less) or search and replace (like sed). awk : a data oriented programming language.
178
grep
-v return lines that do not contain pattern -n precede returned lines with line numbers. -c only return a count of line with the matching pattern. -l only return the names of the files that have at least one line containing the pattern. -r perform a recursive search of files, starting with the named directory. -i perform a case-sensitive search.
180
Using cron
Must edit and install your cron table file Cron table file cannot be edited directly Edit the file and then install with crontab or Edit the file through crontab Syntax:
Cron table files( crontabs ) are stored in /var/spool/cron, which is not accessible by non-privileged users.
181
Using sed
$ sed s/dog/cat/g pets $ sed 1,50s/dog/cat/g pets $ sed /digby/,/duncan/s/dog/cat/g pets $ sed e s/dog/cat/ e /s/hi/lo pets $ sed f myedits pets
182
Unit 15
183
find
Can execute commands on found files May only search directories where the user has read and execute permission. Find has a huge amount of options that can be provided to describe exactly what kind of file should be found. You can search bases on file name, file size, last modified time stamp, inode number, and many, many more. find allows you to perform arbitrary actions on arbitrary files.
Slower but more accurate than slocate CWD is used if no starting directory given All files are matched if no criteria given.
184
Case-insensitive search for files named show.png, Snow.png, SNOW.PNG, etc. Search for files owned by the user gmustafa and the group gmustafa
The regex option in find does not work quite the way one would expect. regex applies the regular expression to the name of the file, including the absolute path to the file.
185
Criteria and ANDed together by default. Can be ORd or negated with o and not Parentheses can be used to determine logic order, but must be escaped in bash.
Logical ANDs have a higher priority than a logical OR, and a logical NOT has a higher priority than an AND or an OR. To force precedence of an expression, you can enclose options that should be grouped together in parentheses.
$ $ $ $ $
user gmustafa not group root -user gmustafa o user root not\(-user gmustafa o user root \) / -name *.png user gmustafa mtime+12 / -name *.png not user abcd
186
Can match ownership by name or id (-user, -group, -uid, -gid) $ find / -owner gmustafa gid 500 Can match octal or symbolic permissions $ find perm 755 # if anyone can write $ find perm +2 #if anyone can write $ find perm -2 #if everyone can write $ find perm +o+w # if other can write Use not to test denied permissions. A numeric permission preceded by will match files that have at least one bit (user, group or other) for that permission set. Symbolic permission by a + will match any file where all criteria are met. To match files where someone has been denied a permission, simply write a search for files where the permission is granted and then negate it. In other words, to find files where neither the user nor the group have read access you could use something like: find not perm +ug+r
187
Files with a size of exactly 10 Megabytes. Files with a size over 10 Megabytes
$ find / -atime 5
$ find / -mtime +5
$ find / -ctime -5
188
If your exec command does not include {}, find will still execute the command once for each file that is found.
189
-atime : when file was last read -mtime : when file data last changed -ctime : when file metadata last changed
More of *time
While the values passed to atime, -ctime and mtime are measured in days, there are also corresponding criteria that perform searches in minutes: -amin, -cmin and mmin. You can match access times relative to the timestamps of other files using -anewer, -cnewer and newer, which test mtimes.
To match files older than recent_file.txt you would simply negate the mnewer criteria.
Would list all files with mtimes more recent that that of recent_file.txt.
The metadata, including all three timestamps, for a file can be manually examined using the stat command.
191
-ok options, which caused find to ask for each file. When a character is prepended with a backslash (\), bash is instructed to treat it literally, so typing \; at bashs command prompt will send; to find after bash has done its interpretations.
192
Unit 16
193
What is a process?
Exec thread PID Priority Memory context Environment File descriptors Security credentials
194
Once process forks a child, pointing to the same pages of memory, and marking the area as read-only. Then the child execs the new command, causing a copy-on-write fault, thus copying to a new area of memory. A process can exec, without forking
195
Process Ancestry
init is the first process started at boot time always has PID 1 Except init, every process has a parent. Processes can be both a parent and a child at the same time. pstree shows the process ancestry for all process running on the system.
196
Process States
-R
-S
process in the run queue. It is waiting for its turn to run or it is executing. Process is not executing, not it is ready to run. It is waiting for an event to occur or a signal to arrive to wake it up. Process is not executing because it has been stopped. Process is sleeping and can not be woken up until an even occurs. It can be woken by a signal. Typically, the result of an I/O Operations. Just before a process dies, it sends a signal to its parent and waits for an acknowledgement before terminating. Even if the parent process does not immediately acknowledge the signal, all resources except for the process identity number (PID) are released. Zombie process are cleared from the system during the next system reboot and do not adversely affect system performance.
-T
-D
Uninterruptible sleep
-Z
197
Viewing process
The options described above are based on output conforming to the UNIX98 Standard. -l long listing. Includes more information such as the process owners UID. $ ps alx | grep lpd $ pgrep lpd The above two command are identical.
a Processes by all users x process from all terminal u show process owner w include command arguments f show process ancestry.
198
Syntax:
$killall Read man 7 signal. kill can send many signals, but processes only respond to the signals they have been programmed to recognize. Signals may be sent to processes interactively using top and gnome-system-monitor.
199
Terminating Processes
Can attempt to interrupt with <Ctrl-c> (INT) or send a TERM signal If all else fails, send a KILL signal. Using KILL signals on a route basis may cause zombie process and lost data.
200
Display real-time process information Allow sorting, killing and renicing Command-line: top GUI: gnome-system-monitor Read: man top and help of gnomesystem-monitor
201
When a command is entered, the shell will not process further input until the process is complete and the shell prompt is redisplayed. i.e. job control. The typeahead buffer allows you to type other command, but they will not be processed until the pending process completes, or returns.
202
Processes are scheduled with a Defult priority of 0 Priority value can range from -20 (highest priority) to 19 (lowest).
Non-privileged users may not set niceness value to less than zero; that is , they may not request a higher than normal priority for their processes. This is a function reserved for the superuser.
203
Once a priority value is raised, a non-privileged user can not lower it.
The p options is not necessary. If you wish to change the priority of an entire process group, use the g option. Use u option, to modify the priority of all the process of a particular user.
$ renice 15 p < PID of users offending, greedy process> $ renice -15 p <PID of some users critical process>
$ renice 15 u gmustafa
204
Running a command in the background allows another process to run concurrently on the same terminal. Launch a program as a background process by appending an ampersand (&) to the end of a command: $firefox & When a process is started in the background, a new bash sub-shell is created. The bash program is then replaced with the command being executed (the fork then exec procedure). Background processes can be managed like any other process.
205
Suspending a Process
Foreground jobs can be suspended: temporarily halted without being killed Suspend a foreground process with <Ctrl-z> Suspended jobs can be:
When the job resumes, it will continue executing from the point at which it was suspended, it will not have to start over from the beginning.
206
jobs displays all process running in the background or that suspended. The number in brackets is a job number, used to kill jobs or bring them back to the foreground. Job numbers are referenced with %.
207
When a command is suspended or backgrounded, it can be brought back to the foreground with fg. Suspended jobs can be resumed in the background with bg. Syntax:
$ fg $ bg
[%job_number] [%job_number]
208
Compound Commands
A subshell group will combine the commands so they are treated them as one unit. When a group of commands is placed inside parentheses, a new subshell is spawned and output can be redirected as if it were one command.
209
Syntax:
$ at time
<commands>
Commands will be executed at the time indicated Non-redirected output is mailed to the user.
$ $ $ $
at at at at
Cron jobs are listed in a crontab file The cron mechanism is controlled by a daemon called crond. This daemon wakes up every minute or so and determines if any entry users cron tables need to be executed. If the time passed for an entry to be started, it is started. A cron job can be scheduled as often as once a minute or as infrequently as once a year.
211
Using cron
Must edit and install your cron table file Cron table file cannot be edited directly
Edit the file and the install with crontab Or Edit the file through crontab $ crontab [-u user] file $ crontab [-l | -r | -e]
212
Syntax
Fields are minute, hour, day of month, month, and day of week. Comment line begin with #
213
Unit 17
Network Clients
214
Epiphany
Uses same rendering engine as Firefox Fully Gnome-complaint, but fewer features. KDE web browser/file manager Supports tabs, popup-blocking, etc. Uses khtml rendering engine.
215
Konqueror
links
Provided by the elinks rpm Full support from frames and ssl Examples
216
wget
Retrieves files via HTTP and FTP Non-interactive useful in shell scripts. Can follow links and traverse directly trees on the remote server useful for mirroring web and FTP sites. Read info wget and wget help.
217
218
Public-key encryption technology replacement for various unsecured services. Allows secure access to remote systems
$ ssh gmustafa@oss2 -or$ ssh -l gmustafa oss2 gmustafa@oss2s password: $ ssh oss2 ls /tmp $ ssh oss1 df -h
219
scp requires that the destination be a directory if the source is a directory or consists of more than one file.
220
Also available is sftp, an interactive file-transfer program similar to a simple ftp client. The remote hosts sshd needs to have support for sftp in order for the sftp client to work.
telnet : login names and password pass over the network in clear text r services (rsh, rlogin, rcp): generally insecure authentication mechanism. telnet client can be used to connect to services running on arbitrary ports Examples: testing your mail server
$ telnet localhost 25
rsync
A drop-in replacement for rcp copying to or from remote systems. Can use ssh for transport $ rsync e ssh mysong.mp2 172.27.12.13:/home/gmustafa Faster than scp copies difference in like files. Useful options to rsync -e Use an external, rsh-compatible program to connect (usually ssh) -a Recurse subdirectories, preserving permissions, ownership, etc. -r Recurse subdirectories without preserving permissions, etc. --partial continue partially downloaded files --progress print a progress bar while transferring -p Same as partial progress Read man 1 rsync
222
lftp
gFTP
Applications/Internet/gFTP Graphical FTP Client Allows Drag-and-Drop transfers Anonymous or Authenticated access Optional secure transfer via ssh (sftp).
224
smbclient
Access a share
$smbclient L server1
CIFS is most commonly implemented as Microsoft Windows Network Neighborhood. Useful options include:
-w workgroup or domain -U Username -N Suppress password prompt (otherwise you will be asked for a password).
225
ping
tracroute
Detects if it is possible to communicate with another system. Many systems no longer respond to pings. Displays the computers through which a packet must pass to reach another system. The mtr command is a repetitive version of traceroute, giving continually updated connection time statistics. Performs hostname to IP address translation, as well as the reverse. Performs a service similar to host in greater datail. Provides a service similar to host in greater detail. A graphical front end of tools listed above (as well as some others) in a single, simple interface. gnome-nettool can be run from the command line or by selecting its icon from the internet section of the Application Menu. Note that, this tool may no be installed by default. 226
mrt
host
dig
netstat
gnome-nettool (GUI)
Unit 18
227
Scripting Basics
Shell scripts are text files that contains a series of commands or statements to be executed. Shell scripts are useful for:
Automating commonly used commands. Performing system administration and troubleshooting Creating simple applications Manipulations of text or files. Application prototyping
228
Step 1: Use a text editor such as vi to create a text file containing commnds.
Create shell script which is self documenting. If you enter this by pressing \ key followed by the Enter key on the most keyboards. This will enable you to enter one command that spans multiple lines.
229
Place the script file in a directory in the executable path ORSpecify the absolute path or relative path to the script on the command line.
230
Generating Output
$ echo Welcome to Redhat Linux paradise! $ echo n please enter the file name:
$ printf the result is %0.2f\n $RESULT Syntax similar to C printf() function Does not automatically put a newline at the end of the output.
231
Handling Input
read reads from standard input and assigns one word to each variable. Any leftover words are assigned to the last variable. A word is defined as a character string surrounding with white space such as spaces and tabs. Can be changed by IFS. IFS=: If there are more words than variables, the last variable is assigned all the remaining words.
232
#!/bin/bash echo n Enter name (First Last): read FIRST LAST printf Your First name is %s and your last name is %s \n \ $FIRST $LAST The (-p) option is used to display a prompt string. Place quotes around the string if you need to prompt the user with a multiple-word command #!/bin/bash read p Enter Several Values: value1 value2 value3 echo value 1 is $value1 echo value 2 is $value2 echo value 3 is $value3
233
Exit Status
0 for success, 1 to 255 for failure Exit status of most recently executed command is kept in the $? Variable just like return values form shell functions.
Shell scripts may set an exit status with the exit command.
Control Structures
Sequential structures the program flows one line after another Selection structures code execution based on a logical decision.
235
Conditional Execution
Commands may be executed conditionally, based on the exit stats of the previous command.
Examples:
&& ||
if selection structures execute the body of the structure only if the condition tested is true.
if [condition]; then
do something
fi
237
File Tests
File tests:
-f tests to see if file exist and is a regular file -d tests to see if a file exists and is a directory -x tests to see if a file exits and is executable if [-f $HOME/lib/functions ]; then
source ~/lib/functions
fi
238
-d FILE -e FILE -f FILE -h FILE -L FILE -r FILE -s FILE -w FILE -x FILE -O FILE -G FILE group. Type help test at
True True True True True True True True True True True
if if if if if if if if if if if
file is a directory file exists. file exits and is a regular file file is a symbolic link. file is a symbolic link. the file is readable by you. file exists and is not empty. the file is writable to you. the file is executable by you. the file is effectively owned by you. the file is effectively owned by your
String Tests
-z returns true if the string is empty -n returns true if the string is not empty. Operators such as =, !=, < and > may be used to compare strings as well. if [ $(id u) =0]; then echo You are logged in as root fi
240
-z STRING -n STRING STRING1=STRING2 STRING1!=STRING2 STRING1<STRING2 STRING1>STRING2 -o OPTION ! EXPR EXPR1 a EXPR2 EXPR1 o EXPR2 ARG1 OP ARG2
True if string is empty. True if string is not empty. True if the strings are equal. True if the strings are not equal. True if STRING1 sorts before STRING2 lexicographically. True if STRING1 sorts after STRING2 lexicographically. True if the shell option OPTIOIN is enabled. True if EXPR is false. True if both EXPR1 AND EXPR2 is true. True if either EXPR1 OR EXPR2 is true. Arithmetic tests. OP is one of the eq, -ne, -lt, -le, -gt, or ge.
241
if/else selection structures execute the body of the if structure only if the condition tested is true, otherwise the else is executed.
if [ condition] ; then
Do something
else
Do something else
fi
242
The case statement provides an alternative method for performing selections that may be cleaner than multiple if/elif/else tests.
case variable in
pattern1)
pattern2)
esac
243
The for repetition structure provides a method for iterating, or looping, through a list of values and executing commands on each of these values.
do
commands
done
244
For loops are useful for iterating through numeric sequences Use bash notation for simple sequences
for I in {0..10}
The while loop structure provides a useful method for performing a set of commands while a condition remains true. The syntax is: while condition do commands done While loops are known as sentinel repetition structures. An until loop works in exactly the same way, except that it continues to execute as long as the command following the until statement executes successfully; that is, it will stop the loop when the command succeeds.
246
continue stops the current execution of the loop and reexamines the initial condition, possibly restarting the loop. break stops the processing the loop entirely, jumping past the done statement. exit exits from the shell scripts entirely You may provide an exit status. Without an explicit exit status, the exit command will exit with at status of zero, indicating success.
247
Positional parameters are special variables that hold the command-line arguments to the script. The positional parameters are available are $1, $2, $3, etc. These are normally assigned to more meaningful variable names to improve clarity. $* holds all command-line arguments. The variable $0 is reserved and specifies the program name as it was executed on the command line. Variables above $9 require special handling and so they must be enclosed in curly braces, e.g. ${11}. All positional parameters are read only variables.
248
Causes problem when parameters have spaces Example : script.sh arg 1 arg 2
$* contains arg 1 arg 2 Automatically assigns VAR with $1, $2, etc..
Handles spaces in parameters gracefully. VAR would be set to arg 1, then arg 2
249
Space treated as filename-delimiter by default my file interpreted as two files: my and file
IFS environment variable can be used to specify an alternate (or no) delimiter.
#Backup old delimiter #Set empty delimiter #Do Stuff #Restore IFS
250
Shell scripts may include shell functions. Shell functions may improve program readability. They also help to remove repetitious code from the scripts. Shell functions must be declared before they are used. If there is a section of shell code that is trying to accomplish the same task and it is five or more lines long then it is good candidate for being put in a separate function. This is known as functional decomposition and will improve the program readability. If there are a piece of shell code that you are likely to use again then it is also good candidate for being put in a separate function. This is called code reuse. Shell functions must be declared in the shell script before they may be used. This can be done by including the function before it gets called in the script or by sourcing another shell script using the keyword source or the (.) operator.
251
Arguments may be passed to a shell function by using their own set of positional parameters ($1, $2 etc.)
The value of $filename will be available as $1 inside the body of myFunction. Functions may return the values by using the return keyword which sets the value of the special variable $?. local make the variables unavailable outside the function. This is called information hiding and is useful for keeping function compartmentalized. In addition it will prevent us from accidentally overwriting a global variables. Functions may return vales using the return keyword which sets the value of the special variable $?.
myFunction $filename
252
Scripts can be typed at the bash prompt. If/else/for/while statements cause extended prompt Can also separate lines with semicolon (;) Define functions for code reuse Store common functions in ~/.bashrc Bash built-in fc for long commands
In order to debug a shell script invoke the shell interpreter with debug options or change the shebang to include the debug options.
Appendix:
255
Special features of vim File and screen repositioning Setting marks in text Mapping complex commands to keystrokes (:map) Advanced read and save
256
vim -o fn1 fn2 start <Ctrl-w>s <Ctrl-w>v <Ctrl-w>n file <Ctrl-w>q <Ctrl-w>[hjkl]
with multiple windows split file horizontally split file vertically open new window, new quit out of a window move to different window. resize window show help for windows.
257
:set textwidth=65
gq} 1GgqG
258
qc q @c
record keystrokes saving to buffer c stop recording keystrokes play back keystrokes from buffer c
Set a mark in text with a single quote followed by a letter of the alphabet specifying the mark name. Example: delete some lines
Move the curson to the first line to delete Set mark a: a Move the cursor to the last line to delete. Set mark b: b Delete from marks a to b: :a,bd
260
You can create your own commands with the :map command Place: map commands in ~/.vimrc or ~/.exrc file Example: mapping <Ctrl-x> to : syntax off : map <Ctrl-x> : syntax off :map <Ctrl-x> :syntax off :map <Ctrl-l> :w!<CR>:!aspell check %<CR>:e!%<CR> The <Ctrl-l> keystroke may not be entered into the file. To force it to be read, run <Ctrl-v> before the <Ctrl-l>: <Ctrlv><Ctrl-l> The <Ctrl-v> forces the <Ctrl-l> to be taken literally. The sequence <CR>, meaning carriage return should be entered just as shown, with the four separate characters.
261
Miscellaneous Commands
Returns to your previous location J Join with the next file. ~ Change character case >> Indent << Unindent.
262
What next?
LUGs (bdlug) http://www.redhat.com/training http://www.redhat.com/training/developter/courses/ http://www.redhat.com/training/architect/courses http://www.redhat.com/training/developter/courses/ http://www.idiom.com/free-compilers http://www.redhat.com/apps/support/knowldedgeba se http://www.tldp.org http://www.linuxquestions.org
263
Motto
264