В началоUnix Toolbox → 20. SHELLS
Gentoo-doc HOME Пред.: 19. DISK QUOTAВ началоУровень выше: Unix ToolboxСлед.: 21. SCRIPTING

20. 20. SHELLS

Most Linux distributions use the bash shell while the BSDs use tcsh, the bourne shell is only

used for scripts. Filters are very useful and can be piped:

grep Pattern matching

sed Search and Replace strings or characters

cut Print specific columns from a marker

sort Sort alphabetically or numerically

uniq Remove duplicate lines from a file

For example used all at once:

# ifconfig | sed 's/ / /g' | cut -d" " -f1 | uniq | grep -E "[a-z0-9]+" | sort -r

# ifconfig | sed '/.*inet addr:/!d;s///;s/ .*//'|sort -t. -k1,1n -k2,2n -k3,3n -k4,4n

The first character in the sed pattern is a tab. To write a tab on the console, use ctrl-v ctrl-tab.

20.1. 20.1 bash

Redirects and pipes for bash and sh:

# cmd 1> file # Redirect stdout to file.

# cmd 2> file # Redirect stderr to file.

# cmd 1>> file # Redirect and append stdout to file.

# cmd &> file # Redirect both stdout and stderr to file.

# cmd >file 2>&1 # Redirects stderr to stdout and then to file.

# cmd1 | cmd2 # pipe stdout to cmd2

# cmd1 2>&1 | cmd2 # pipe stdout and stderr to cmd2

Modify your configuration in ~/.bashrc (it can also be ~/.bash_profile). The following entries are useful, reload with ". .bashrc".

# in .bashrc

bind '"\e[A"':history-search-backward # Use up and down arrow to search

bind '"\e[B"':history-search-forward # the history. Invaluable!

set -o emacs # Set emacs mode in bash (see below)

set bell-style visible # Do not beep, inverse colors

# Set a nice prompt like [user@host]/path/todir>

PS1="\[\033[1;30m\][\[\033[1;34m\]\u\[\033[1;30m\]"

PS1="$PS1@\[\033[0;33m\]\h\[\033[1;30m\]]\[\033[0;37m\]"

PS1="$PS1\w\[\033[1;30m\]>\[\033[0m\]"

# To check the currently active aliases, simply type alias

alias ls='ls -aF' # Append indicator (one of */=>@|)

alias ll='ls -aFls' # Listing

alias la='ls -all'

alias ..='cd ..'

alias ...='cd ../..'

export HISTFILESIZE=5000 # Larger history

export CLICOLOR=1 # Use colors (if possible)

export LSCOLORS=ExGxFxdxCxDxDxBxBxExEx

20.2. 20.2 tcsh

Redirects and pipes for tcsh and csh (simple > and >> are the same as sh):

# cmd >& file # Redirect both stdout and stderr to file.

# cmd >>& file # Append both stdout and stderr to file.

# cmd1 | cmd2 # pipe stdout to cmd2

# cmd1 |& cmd2 # pipe stdout and stderr to cmd2

The settings for csh/tcsh are set in ~/.cshrc, reload with "source .cshrc". Examples:

# in .cshrc

alias ls 'ls -aF'

alias ll 'ls -aFls'

alias la 'ls -all'

alias .. 'cd ..'

alias ... 'cd ../..'

set prompt = "%B%n%b@%B%m%b%/> " # like user@host/path/todir>

set history = 5000

set savehist = ( 6000 merge )

set autolist # Report possible completions with tab

set visiblebell # Do not beep, inverse colors

# Bindkey and colors

bindkey -e Select Emacs bindings # Use emacs keys to edit the command prompt

bindkey -k up history-search-backward # Use up and down arrow to search

bindkey -k down history-search-forward

setenv CLICOLOR 1 # Use colors (if possible)

setenv LSCOLORS ExGxFxdxCxDxDxBxBxExEx

The emacs mode enables to use the emacs keys shortcuts to modify the command prompt line.

This is extremely useful (not only for emacs users). The most used commands are:

C-a Move cursor to beginning of line

C-e Move cursor to end of line

— Shells —

47

M-b Move cursor back one word

M-f Move cursor forward one word

M-d Cut the next word

C-w Cut the last word

C-u Cut everything before the cursor

C-k Cut everything after the cursor (rest of the line)

C-y Paste the last thing to be cut (simply paste)

C-_ Undo

Note: C- = hold control, M- = hold meta (which is usually the alt or escape key).

Пред.: 19. DISK QUOTAВ началоУровень выше: Unix ToolboxСлед.: 21. SCRIPTING
В началоUnix Toolbox → 20. SHELLS