Conquering the Bash Shell

Post on 05-Jan-2016

39 views 0 download

description

Conquering the Bash Shell. Overview. Shell Evironment Linux Command Prompt Viewing System Information Advance Shell Usage and Script. Shell Environment. 2 user interfaces on Linux GUI hosted by X CLI called the shell The most common way to access the shell is via a terminal window - PowerPoint PPT Presentation

Transcript of Conquering the Bash Shell

®

Red Hat® INTERNET SYSADMIN

Conquering the Bash Shell

® Overview

Shell Evironment Linux Command Prompt Viewing System Information Advance Shell Usage and Script

® Shell Environment

2 user interfaces on Linux GUI hosted by X CLI called the shell

The most common way to access the shell is via a terminal window Start > System Tools > Terminal

Example :$ w The w command tells Linux to display the

system status and a list of all system users

® Shell Environment (cont…)

Example :$ date The date command tell linux to show

the current date How to correct commands

Backspace key erases characters Left key does not erase characters Delete key to delete unwanted

characters

® Shell Environment (cont…)

Reissue previous command using bash's history

Scroll back (the Up arrow key) or Back down (the Down arrow key)

Bash's history is saved in the user's home directory, ~/.bash_history

Up to 500 command Make command completion

Type a part of your command Press Tab key

MOC Classroom
Grammar correction.Tab key correction.

® Shell Environment (cont…)

Useful Control keystrokes Ctrl-C — cancels execution Ctrl-D — terminate console input Ctrl-Z — suspends the currently

executing program Special characters

# — Marks the line as a comment ; — Separates commands

® Linux Command Prompts

Command Prompt Forms Getting Help Working with Directories Working with Files Working with Compressed Files

® Command Prompt Forms

Commands and Arguments in Linux command [options] [arguments]

2 kinds of commands External commands

Stored in /bin, /usr/bin, or /usr/local/bin System administration commands

stored in /sbin or /usr/sbin - included by default in the path of the root user

Linux commands are case sensitive

MOC Classroom
's'

®

Options modify the way that a command works

Example w -h w informit w -h informit

When your command is too long Type a backslash (\) at the end of a line, then Press Enter, then Continue your command in the new line

Command Prompt Forms (cont…)

® Getting Help

2 way of getting help in Linux man command apropos command

Both of them access a help database that describes commands and their options

Each Linux command is described by a special file called a manual page $ man w To quit from mode man, type “q”

® Getting Help (cont…)

The apropos command displays just a one-line summary of each $ apropos samba

Before using apropos, run this $ makewhatis

MOC Classroom
Perbaian grammar

® Working with Directories

Displaying the working directory$ pwd

Changing the working directory$ cd /bin (go to bin directory)$ cd (go to your home directory)$ cd .. (go to the parent directory)

® Working with Directories (cont…)

Display directory contents$ ls$ ls –l$ ls /bin$ ls –al

Use less to show output one page at a time$ ls | less space moves one page forward b moves one page backward

Pandu E Poluan
chdir, mkdir moved to separate page.adds info on less

® Working with Directories (cont…)

Creating a directory$ mkdir office

# mkdir /tmp/documents Removing a directory

$ rmdir unwanted

® Working with Files

Displaying the contents of a file# cat /etc/passwd # less /etc/passwd

Removing a file# rm badfile

Copying a file# cp /etc/passwd sample

Renaming or moving a file# mv old new

Finding a file# find . -name 'missing' -print # find / -name '*iss*' -print # locate pass# which ls

® Working with Files (cont…)

To link a new name to an existing file or directory # ln -s old new

To run the program # bigdeal or# ./bigdealor# /home/bob/bigdeal

® Working with Compressed Files

To compress files# gzip bigfile.gz

To expand a compressed file # gunzip bigfile

To create a tarfile # tar -cvf backup.tar /home/informit

To list the contents of a tarfile # tar -tvf tarfile | less

To extract the contents of a tarfile # tar -xvf tarfile

® Further with File Permissions

To set the access modes of a directory or file

# chmod nnn directory-or-file To assign newuser as the owner of the

file hotpotato # chown newuser hotpotato

To assigns newgroup as the new group of the file hotpotato

# chgrp newgroup hotpotato To change user login group to the group

named secondgroup# newgrp secondgroup

® Viewing System Information

CLI also provide command to troubleshoot system problems and identify resource bottlenecks Each supports options and arguments

to customize its operation and output Example :

du, df, free, top, ps, uptime

Pandu E Poluan
Reformat. Fix grammar

® Advance Shell Usage and Script

Filename Globbing Shell Aliases Jobs Shell Scripts Redirection and Piping Filter & Regular Expressions Shell Variables The Search Path Quoted Strings

® Filename Globbing

Globbing can make the command writing more quick and effecient

Example : Common command :

$ ls -l file1 file2 file3 file04 Using File Globbing :

$ ls -l file* Another form of file globbing

List file2 and file3 $ ls -l file[2-3]

Pandu E Poluan
Delete wrong item

® Shell Aliases

Make commands easier to use Establish abbreviated command

names Pre-specify common options and

arguments for a command Form :

alias name='command' Example :

$ alias lihat='ls -l'$ unalias lihat

® Jobs

Jobs are command execution process 2 mode of jobs :

Foreground Background

4 activities related with jobs : Run jobs in the background Notify when a job ends Bring jobs to the foreground Stop and Suspend jobs

® Jobs (cont…)

Example : Run jobs in the background

$ lpr mydata & Notify when job ends

$ notify %2 Bring jobs to the foreground

$ fg %2 Stop and Suspend jobs

$ kill %2

® Shell Scripts

Definition : A file that contains a set of commands

to be run by the shell when invoked Example :

file name : deleter echo -n Deleting the temporary files rm -f *.tmpecho Done.

Running it$ sh deleter

® Shell Scripts (cont…)

2 kinds of scripts : Standard Scripts ~provided by Linux User Scripts ~ created by user

® Redirection

3 standard data streams stdin (<)

Where program read their input stdout (>)

Where program write their output stderr (2>)

Where program write the error message

Pandu E Poluan
stdput: mistype

®

Example : Redirect number of lines, words and

character on /etc/passwd into total$ wc /etc/passwd > total

Redirect an error message to /dev/null $ grep localhost * 2> /dev/null

Tell wc to read from a file, rather than the console $ wc < /etc/passwd

Redirection(cont…)

® Filter

Filters are commands that Read data Perform operations on that data Send the results to the standard output

Example :$ cat complist | lpr

$ ls –al | grep root

Pandu E Poluan
Capitalization

® grep Command

Form : grep pattern filenames-list

Example :$ grep 'text file' preface

$ grep data preface intro

$ grep data *

$ grep while *.c

® Regular Expressions

Usually, combined with grep, sed, gawk command

Notation : ^, $, *, ., [] Example :

$ ls -l | grep '^d‘

$ grep ‘t$‘ file1

$ ls –l | grep ‘file[1-3]’

® Shell Variables

Provides a convenient way of transferring values from one command to another

® Shell Variables (cont…)

To list shell variables : $ set | less

To export a variable :$ export variable-name

Example :$ cd ${HOME}

$ cd ${HOME}/work

$ echo ${HOME}

Pandu E Poluan
Reformat.

® The Search Path

The paths that are looked in by shell when issuing an external command

Use a colon (:) to separate each path of the search path /usr/bin:/bin:/usr/local/bin

To make additional path :$ PATH=${PATH}:/home/bill

$ PATH=/home/bill:${PATH} To find location of program file :

$ which program-name

® Quoted Strings

Ensures that the shell doesn’t misinterpret a command argument, file name, variable

Example$ echo $PATH

$ echo ’$PATH’$ echo My home directory contains

‘ls ~|wc -?‘ files.

Pandu E Poluan
misspell 'variabel'.

® Summary

In this module, you have learned about : Shell Command Linux Command Prompt Viewing System Information Advance Shell Usage and Script