36
® Red Hat ® INTERNET SYSADMIN Conquering the Bash Shell

Conquering the Bash Shell

  • Upload
    hewitt

  • View
    39

  • Download
    0

Embed Size (px)

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

Citation preview

Page 1: Conquering the Bash Shell

®

Red Hat® INTERNET SYSADMIN

Conquering the Bash Shell

Page 2: Conquering the Bash Shell

® Overview

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

Page 3: Conquering the Bash Shell

® 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

Page 4: Conquering the Bash Shell

® 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

Page 5: Conquering the Bash Shell

® 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.
Page 6: Conquering the Bash Shell

® 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

Page 7: Conquering the Bash Shell

® Linux Command Prompts

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

Page 8: Conquering the Bash Shell

® 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'
Page 9: Conquering the Bash Shell

®

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…)

Page 10: Conquering the Bash Shell

® 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”

Page 11: Conquering the Bash Shell

® 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
Page 12: Conquering the Bash Shell

® 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)

Page 13: Conquering the Bash Shell

® 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
Page 14: Conquering the Bash Shell

® Working with Directories (cont…)

Creating a directory$ mkdir office

# mkdir /tmp/documents Removing a directory

$ rmdir unwanted

Page 15: Conquering the Bash Shell

® 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

Page 16: Conquering the Bash Shell

® 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

Page 17: Conquering the Bash Shell

® 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

Page 18: Conquering the Bash Shell

® 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

Page 19: Conquering the Bash Shell

® 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
Page 20: Conquering the Bash Shell

® 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

Page 21: Conquering the Bash Shell

® 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
Page 22: Conquering the Bash Shell

® 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

Page 23: Conquering the Bash Shell

® 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

Page 24: Conquering the Bash Shell

® 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

Page 25: Conquering the Bash Shell

® 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

Page 26: Conquering the Bash Shell

® Shell Scripts (cont…)

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

Page 27: Conquering the Bash Shell

® 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
Page 28: Conquering the Bash Shell

®

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…)

Page 29: Conquering the Bash Shell

® 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
Page 30: Conquering the Bash Shell

® grep Command

Form : grep pattern filenames-list

Example :$ grep 'text file' preface

$ grep data preface intro

$ grep data *

$ grep while *.c

Page 31: Conquering the Bash Shell

® Regular Expressions

Usually, combined with grep, sed, gawk command

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

$ ls -l | grep '^d‘

$ grep ‘t$‘ file1

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

Page 32: Conquering the Bash Shell

® Shell Variables

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

Page 33: Conquering the Bash Shell

® 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.
Page 34: Conquering the Bash Shell

® 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

Page 35: Conquering the Bash Shell

® 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'.
Page 36: Conquering the Bash Shell

® Summary

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