Bash: Difference between revisions

From miki
Jump to navigation Jump to search
Line 426: Line 426:
'''for''' name ''[ '''in''' word ]'' ; '''do''' list ; '''done'''
'''for''' name ''[ '''in''' word ]'' ; '''do''' list ; '''done'''
'''for''' (( expr1 ; expr2 ; expr3 )) ; '''do''' list ; '''done'''
'''for''' (( expr1 ; expr2 ; expr3 )) ; '''do''' list ; '''done'''

The normal iteration sequence can be modified with keywords '''continue''' ''[n]'', '''break''' ''[n]''.


The first syntax enumerates a list, as in:
The first syntax enumerates a list, as in:

Revision as of 00:04, 10 November 2009

References

Other pages dedicated to Bash on this wiki:

External references:

help getopts
help test
help if

Shell

Commands

Listing directory content:

ls --full-time               # To list files full date & time
ls -d directory-name         # List directory entries, not content
                             #  eg. ls -d /etc/rc*
ls -lS | head                # List biggest files first - print only first 10 entries
ls -lt | head -n 20          # List most recent files first, print only 20 first entries

Keyboard shortcuts

Some of the most used keyboard shortcuts in Bash (excerpt from the manual pages).
C- refers to Ctrl key, M- refers to Meta key (typ. Alt+key, or Esc then key), S- refers to Shift key.

Customized keyboard shortcuts described below can be obtained by adding the following lines to e.g. your ~/.inputrc file:

"\eOA":history-search-backward     #Up arrow
"\e[A":history-search-backward     #Up arrow
"\eOB":history-search-forward      #Down arrow
"\e[B":history-search-forward      #Down arrow
"\C- ":dynamic-complete-history    #Ctrl-space
"\e ":dynamic-complete-history     #Esc-space
"\e[2~":paste-from-clipboard       # Insert
Note on defining new bindings
Binding by defaults are defined in file ~/.inputrc. Alternatively they can also be defined in the start file ~/.bashrc using command bind.
They are different method to define the binding keys
  • Specifying the key Keyname, without double quotes (but always within single quotes)
  • Specifying the key Keyseq, with double quotes (and single quotes)
  • Specifying the key Verbatim, with double quotes, and using control-V followed by key in Bash or Vi. Note: This is a good trick to know the key sequence of special keys like arrow key.
The binding keyseq differs depending on whether Readline is configured to support 8-bit input/output characters. Cygwin and openSUSE have different settings regarding this. To support 8-bit input/output (like in OpenSUSE), add the following to the ~/.inputrc:
set convert-meta off
set input-meta on
set meta-flag on
set output-meta on
bind 'tab:dynamic-complete-history'                #Keyname
bind '"\C-i":dynamic-complete-history'             #Keyseq
bind '"<Control-V><Tab>:dynamic-complete-history'  #Verbatim - <> means press the given key while editing

Commands for Moving

forward-word (M-f, C-→, M-→, S-→)
Move forward to the end of the next word. Words are composed of alphanumeric characters (letters and digits).
!!! C-→, M-→, S-→ not supported in Cygwin.
backward-word (M-b, C-←, M-←, S-←)
Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and digits).
!!! C-←, M-←, S-← not supported in Cygwin.
clear-screen (C-l)
Clear the screen leaving the current line at the top of the screen. With an argument, refresh the current line without clearing the screen.

Commands for Manipulating the History

reverse-search-history (C-r)
Search backward starting at the current line and moving 'up' through the history as necessary. This is an incremental search.
forward-search-history (C-s)
Search forward starting at the current line and moving 'down' through the history as necessary. This is an incremental search.
!!! Not supported in Cygwin.
history-search-forward ()
Search forward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search.
!!! Custom keyboard shortcut.
history-search-backward ()
Search backward through the history for the string of characters between the start of the current line and the point. This is a non-incremental search.
!!! Custom keyboard shortcut.
yank-last-arg (M-., M-_)
Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the "!$" history expansion had been specified.
history-expand-line (M-^)
Perform history expansion on the current line. See HISTORY EXPANSION below for a description of history expansion.
!!! AZERTY keyboard: press Alt-^ twice
operate-and-get-next (C-o)
Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored.

Commands for Changing Text

delete-char (C-d)
Delete the character at point. If point is at the beginning of the line, there are no characters in the line, and the last character typed was not bound to delete-char, then return EOF.
backward-delete-char (Rubout)
Delete the character behind the cursor. When given a numeric argument, save the deleted text on the kill ring.
quoted-insert (C-q, C-v)
Add the next character typed to the line verbatim. This is how to insert characters like C-q, for example.
transpose-chars (C-t)
Drag the character before point forward over the character at point, moving point forward as well. If point is at the end of the line, then this transposes the two characters before point. Negative arguments have no effect.
transpose-words (M-t)
Drag the word before point past the word after point, moving point over that word as well. If point is at the end of the line, this transposes the last two words on the line.
upcase-word (M-u)
Uppercase the current (or following) word. With a negative argument, uppercase the previous word, but do not move point.
downcase-word (M-l)
Lowercase the current (or following) word. With a negative argument, lowercase the previous word, but do not move point.
capitalize-word (M-c)
Capitalize the current (or following) word. With a negative argument, capitalize the previous word, but do not move point.
overwrite-mode
Toggle overwrite mode. With an explicit positive numeric argument, switches to overwrite mode. With an explicit non-positive numeric argument, switches to insert mode. This command affects only emacs mode; vi mode does overwrite differently. Each call to readline() starts in insert mode. In overwrite mode, characters bound to self-insert replace the text at point rather than pushing the text to the right. Characters bound to backward-delete-char replace the character before point with a space. By default, this command is unbound.

Killing and Yanking

kill-line (C-k)
Kill the text from point to the end of the line.
unix-line-discard (C-u)
Kill backward from point to the beginning of the line. The killed text is saved on the kill-ring.
kill-word (M-d)
Kill from point to the end of the current word, or if between words, to the end of the next word. Word boundaries are the same as those used by forward-word.
backward-kill-word (M-Rubout)
Kill the word behind point. Word boundaries are the same as those used by backward-word.
unix-word-rubout (C-w)
Kill the word behind point, using white space as a word boundary. The killed text is saved on the kill-ring.

Numeric Arguments

digit-argument (M-0, M-1, ..., M--)
Add this digit to the argument already accumulating, or start a new argument. M-- starts a negative argument.

Completing

complete (TAB)
Attempt to perform completion on the text before point. Bash attempts completion treating the text as a variable (if the text begins with $), username (if the text begins with ~), hostname (if the text begins with @), or command (including aliases and functions) in turn. If none of these produces a match, filename completion is attempted.
possible-completions (M-?)
List the possible completions of the text before point.
insert-completions (M-*)
Insert all completions of the text before point that would have been generated by possible-completions.
dynamic-complete-history (M-SPACE,C-SPACE)
Attempt completion on the text before point, comparing the text against lines from the history list for possible completion matches.
!!! Must use combination Esc + Tab. Custom combination in Cygwin.

Miscellaneous

prefix-meta (ESC)
Metafy the next character typed. ESC f is equivalent to Meta-f.
undo (C-_, C-x C-u)
Incremental undo, separately remembered for each line.
!!! AZERTY keyboard: press Control--.
revert-line (M-r)
Undo all changes made to this line. This is like executing the undo command enough times to return the line to its initial state.
set-mark (C-@, M-<space>)
Set the mark to the point. If a numeric argument is supplied, the mark is set to that position.
exchange-point-and-mark (C-x C-x)
Swap the point with the mark. The current cursor position is set to the saved position, and the old cursor position is saved as the mark.

History

Some examples (assume that magic-space is mapped to Space)

echo 12 34
!!             #execute previous command
!1             #execute command n°1
!-2            #execute penultimate command
echo 12 34
^12^56^        #execute previous command, replacing 12 with 56. Quick for !!:s/12/56/
!!<space>      #Example of magic space
ech !-2<space> #... also works anywhere in the current line

Script Quick Tutorial

Invocation

bash -l                                            #login shell
bash -i                                            #interactive shell
  • Login shell - bash starts by executing /etc/profile, and then either ~/.bash_profile, ~/.bash_login, or ~/.profile.
  • Interactive non-login shell - bash starts by executing ~/.bashrc.


  • Use command shopt to know whether current shell is interactive / login.
  • Interactive shells have i defined in $-, and also define variable PS1.

Environment

export MYVAR=myvalue && command-to-execute         #MYVAR defined for the current shell and invoked shell
MYVAR=myvalue && command-to-execute                #MYVAR defined for the current shell
MYVAR=myvalue command-to-execute                   #MYVAR only defined for the subsequent command

Quotation

echo "var is $var"
String enclosed in double quote is treated as a single argument. Variable and special characters are expanded.
echo 'it costs $10'
Same as double quote, but variables are not expanded.
x=`expr $x + 1`
Back-quote is replaced by the result of the invoked command.
x=$(expr $x + 1)
Same as back-quote, but somewhat clearer
x=$((x + 1))
Built-in Bash expression evaluator. Accepts operation such as + - * / % ! ~ && ....

Variable Expansion

% VAR=myvar.pattern && echo ${VAR%%.pattern}
myvar
Use ${i%%pattern} to delete given trailing pattern. Use a single % for shortest matching pattern.
memo: % at the end as in 100%.
% VAR=pattern.myvar && echo ${VAR##pattern.}
myvar
Use ${i##pattern} to delete given heading pattern. Use a single # for shortest matching pattern.
memo: # at the beginning as in #comment.
% VAR=some.find.text && echo ${VAR/find/replace}
some.replace.text
Use ${i/pattern/text} to replace given pattern with given text.

Positional Parameters

# !/bin/bash

function parsing ()
{
    echo "1 is '$1', 2 is '$2', 3 is '$3', 4 is '$4'"
}

echo -n 'parsing $*   -- ';  parsing $*
echo -n 'parsing $@   -- ';  parsing $@
echo -n 'parsing "$*" -- ';  parsing "$*"
echo -n 'parsing "$@" -- ';  parsing "$@"
echo
$ ./test.sh p1 p2 "p 3" 'p 4'
# parsing $*   -- 1 is 'p1', 2 is 'p2', 3 is 'p', 4 is '3'
# parsing $@   -- 1 is 'p1', 2 is 'p2', 3 is 'p', 4 is '3'
# parsing "$*" -- 1 is 'p1 p2 p 3 p 4', 2 is '', 3 is '', 4 is ''
# parsing "$@" -- 1 is 'p1', 2 is 'p2', 3 is 'p 3', 4 is 'p 4'

Redirection - Descriptors, Here Documents and Here Strings

Description Source Output
Descriptors - redirect input / output to files or file descriptor. These file names have special meaning: /dev/fd/fd (with 2nd fd an integer), /dev/stdin, /dev/stdout, /dev/stderr, /dev/tcp/host/port, /dev/udp/host/port
ls > /dev/stderr
ls > dirlist 2>&1
ls 2>&1 > dirlist   # WRONG
Here Documents - Use <<word to declare here documents.
Check BASH manpage for more information.
cat <<__EOF__
	First line
	Second line
	Third line
__EOF__
	First line
	Second line
	Third line
Here Documents - Use <<- to strip leading tab characters.
cat <<-__EOF__
	First line
	Second line
	Third line
__EOF__
First line
Second line
Third line
Here Strings - Use <<<word to declare here strings.
% cat <<<'Hello, World!'
% TEXT='Hello, World!' cat <<<$TEXT
% cat<<<"First line
> Second line
> Third line"
Hello, World!
Hello, World!
First line
Second line
Third line

If ... then ... [elif ...] ... else ... fi

(See [3] for in-depth discussion on Bash tests)

The basic syntax for if-then-else sequence control is

if list; then list; [ elif list; then list; ] ... [ else list; ] fi

The if/then construct tests whether the exit status of a list of command is 0 (i.e. success), and if so, executes another list of command. Test conditions can be written using shell built-in command test, or equivalently the square brackets [...]. Other possibilities are using the extended test command [[...]] (that performs parameter expansion and command substitution as well as arithmetic evaluation, see [4]), or the double-parens ((...)) expression (see [5]) that evaluates an arithmetic expression (returs true if expression is non-zero).

if mv $src $dst; then echo move is ok; else move is not ok; fi                 # Using list
if test -f /etc/foo; then echo file found; else echo file NOT found; fi        # test command
if [ -f /etc/foo ]; then echo file found; else echo file NOT found; fi         # [ is a synomym for test and a builtin
if [[ -f $file ]]; then echo file $file found; else echo file NOT found; fi    # Extended test construct - &&, ||, < and > works 
if (( 128 && 64 )); then echo result is non-zero; else echo result is zero; fi # Expands and evaluates arithmetic expression

Here's test's options (more info with help test):

-d FILE        # Check if the file is a directory
-e FILE        # Check if the file exists
-f FILE        # Check if the file is a regular file
-g FILE        # Check if the file hash SGID permissions
-r FILE        # Check if the file is readable
-s FILE        # Check if the file's size is not 0
-u FILE        # Check if the file has SUID permissions
-w FILE        # Check if the file is writeable
-x FILE        # Check if the file is executable
NBR1 -eq NBR2  # Check is NBR1 is equals to NBR2
NBR1 -ne NBR2  # Check if NBR1 is not equals to NBR2
NBR1 -ge NBR2  # Check if NBR1 is greater than or equal to NBR2
NBR1 -gt NBR2  # Check if NBR1 is greater than NBR2
NBR1 -le NBR2  # Check if NBR1 is less than or equal to NBR2
NBR1 -lt NBR2  # Check if NBR1 is less than NBR2
STR1 = STR2    # Check if STR1 is the same as STR2
STR1 != STR2   # Check if STR1 is not the same as STR2
-n STR         # Evaluates to true if STR is not null (can be used to test whether a variable is defined)
-z STR         # Evaluates to true if STR is null.

Conditions can be combined with && and || or with test operator -a and -o:

if [ $x -ge 5 ] && [ $x -le 10 ]; then ...; fi
if [ $x -gt 5 ] || [ $x -lt 10 ]; then ...; fi
if [ $x -ge 5 -a $x -le 10 ]; then ...; fi
if [ $x -gt 5 -o $x -lt 10 ]; then ...; fi
if [ -n "$DEFINED" ]; then echo DEFINED is defined; fi
if [ "$DEFINED" ]; then echo DEFINED is defined; fi          #also works

Some examples:

if [ "$name" -eq 5 ]; then echo one; else echo two; fi
if [ -d ~/var ]; then touch ~/var/done; else { mkdir ~/var; touch ~/var/done; } fi   # braces { ... } are optional here

Alternative: Using && and ||

Using && and ||, one can also build a if ... then ... else statement:

[ -f /etc/hosts ] && { echo one; echo two; } || { echo three; echo four; }

WHILE / UNTIL Loops

Syntax is

while list; do list; done
until list; do list; done

Some examples of While loops:

while true; do
   echo "Press CTRL-C to quit."
done

# Faster alternative using Bash built-in colon feature
while :; do
   echo "Press CTRL-C to quit."
done

# A more complete example
x=0;                                 # initialize x to 0
while [ "$x" -le 10 ]; do
    echo "Current value of x: $x"
    # increment the value of x:
    x=$(expr $x + 1)
    sleep 1
done

Some examples of until loops:

x=0
until [ "$x" -ge 10 ]; do
    echo "Current value of x: $x"
    x=$(expr $x + 1)
    sleep 1
done

FOR Loops

There are 2 syntax for for loops:

for name [ in word ] ; do list ; done
for (( expr1 ; expr2 ; expr3 )) ; do list ; done

The normal iteration sequence can be modified with keywords continue [n], break [n].

The first syntax enumerates a list, as in:

# Counting from 1 to 10...
for dots in 1 2 3 4 5 6 7 8 9 10; do echo -n "$dots... "; done;
echo 'Done !'

# Enumerating a list
for fruit in Apple Pear Cherry; do
  echo "The value of variable fruit is: $fruit"
  sleep 1
done

# Acting on a directory list:
for file in *; do
  echo "Adding .html extension to $file..."
  mv $file $file.html
  sleep 1
done

# On several lines
for f in $(seq 33296 33330); do
  wget "http://i.techrepublic.com.com/gallery/${f}.jpg"
done

# On a single line
for i in `seq 278 328`; do wget http://i.techrepublic.com.com/gallery/33$i.jpg; done

Looping on positional parameters

Several solutions:

  • for i in "$@" (note the quotes), explicitly refering to $@ as source list, and which assign i to each positional parameter.
  • for i, which Bash interprets as an implicit in "$@").
  • Use a while [ "$1" != "" ] loop and shift
#Illustrate correct and incorrect ways to loop through positional parameters
echo There are $# parameters
echo -n 'for i in $*  : '; for i in $*         ; do echo -n '-'$i'- '; done; echo
echo -n 'for i in $@  : '; for i in $@         ; do echo -n '-'$i'- '; done; echo
echo -n 'for i in "$*": '; for i in "$*"       ; do echo -n '-'$i'- '; done; echo
echo -n 'for i in "$@": '; for i in "$@"       ; do echo -n '-'$i'- '; done; echo
echo -n 'for i        : '; for i               ; do echo -n '-'$i'- '; done; echo
echo -n 'while loop   : '; while [ "$1" != "" ]; do echo -n '-'$1'- '; shift; done; echo
% loop one "or two"
There are 2 parameters
for i in $*  : -one- -or- -two-
for i in $@  : -one- -or- -two-
for i in "$*": -one or two-
for i in "$@": -one- -or two-
for i        : -one- -or two-
while loop   : -one- -or two-

case ... in ... esac

The syntax is

case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

Some example

x=5     # initialize x to 5
# now check the value of x:
case $x in
   0) echo "Value of x is 0."
      ;;
   5) echo "Value of x is 5."
      ;;
   9) echo "Value of x is 9."
      ;;
   *) echo "Unrecognized value."
esac

Functions

Functions are like aliases, but accept parameters:

function myfunc1() { echo "$2"; echo "$1"; }
myfunc2() { MYVAR="$1"; echo Another $MYVAR function; }       #The keyword ''function'' is optional

Interactivity

Read User's password

Reading input from user is done with the commands read. To prevent password echo on display, one can use the command stty:

###### Preventing echo with stty ######
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo

Preventing password echo using option -s:

###### Using -s ######
PASS="abc123"
read -s -p "Password: " mypassword
echo ""
[ "$mypassword" == "$PASS" ] && echo "Password accepted" || echo "Access denied"

Complete solution:

###### Complete solution ######
USER=wbi\\titeuf
echo "Mounting windows share... Please type password for user $USER..."
# time-out after 60sec, raw input no escaping, no echo, prompt
read -t 60 -r -s -p "Password: " PASSWORD
# delete prompt line
echo -e -n "\r"
mount -t smbfs -o username="$USER",password="$PASSWORD",iocharset=iso8859-1,codepage=cp437 //windows-host/C$ /mnt/c
mount -t smbfs -o username="$USER",password="$PASSWORD",iocharset=iso8859-1,codepage=cp437 //windows-host/D$ /mnt/d
mount -t smbfs -o username="$USER",password="$PASSWORD",iocharset=iso8859-1,codepage=cp437 //windows-host/F$ /mnt/f
# delete password variable
PASSWORD=------------------------------------
PASSWORD=abcdefghijklmnopqrstuvwxyz0123456789
PASSWORD=------------------------------------

Signal trapping

Use trap to trap signals send to a Bash script, and redirect execution to a given function/command.

#Trap Ctrl-C (signal SIGINT) by executing function "sorry"
trap sorry INT
sorry() { echo "I'm sorry Dave. I can't do that."; sleep 3; }

Signals can also be ignored or reset.

#reset the trap:
trap - INT
#do nothing when SIGINT is caught:
trap " INT

Exit Status

Use $? to read the exit status of the last command executed. Value is 0 if last command ran without error, and an integer value between 1-255 on error. The command exit nnn can be used to terminate a script or function and to deliver a given exit status.

One can negate a value with operator !, but don't forget the space!

true    # The "true" builtin.
! true  # Negating true
true
!true   # The '!' operator prefixing a command invokes the Bash history mechanism. It just repeats the previous command.

Some examples to test the exit status:

if [ $? -eq 0 ]; then echo "exit status is 0"; else echo "exit status is non-zero"; fi

SUCCESS=0
grep -q "$word" "$filename"    #  "q" - quiet mode
if [ $? -eq $SUCCESS ]; then echo "$word was found"; else echo "not found"; fi

if ( ps -as | grep -q "ssh-agent" ); then echo ssh-agent started; else echo ssh-agent not started; fi

Miscellaneous

Use eval to execute the output (stdout) of a process
eval `dircolors`
Use $$ as file name suffix or prefix to create unique name for temporary file.
$$ is actually process PID.
touch /tmp/mytmp.$$
echo some text>/tmp/mytmp.$$
cat /tmp/mytmp.$$