The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Поиск:  Каталог документации

3.2. Special characters used in shell scripts

#

Comments. Lines beginning with a # (with the exception of #!) are comments.

# This line is a comment.

Comments may also occur at the end of a command.

echo "A comment will follow." # Comment here.

Comments may also follow white space at the beginning of a line.

	# A tab precedes this comment.

;

Command separator. Permits putting two or more commands on the same line

echo hello; echo there

Note that the ; sometimes needs to be escaped (\).

.

"dot" command. Equivalent to source, explained further on

:

null command. Exit status 0, alias for true

Endless loop:

while :
do
   operation-1
   operation-2
   ...
   operation-n
done

Placeholder in if/then test:

if condition
then :   # Do nothing and branch ahead
else
   take-some-action
fi

Provides a placeholder where a binary operation is expected, see Section 3.3.1.

: ${username=`whoami`}
# ${username=`whoami`}   without the leading : gives an error
            

Evaluate string of variables using "parameter substitution", see Example 3-6:

: ${HOSTNAME?} ${USER?} ${MAIL?}

Prints error message if one or more of essential environmental variables not set.

${}

Parameter substitution.

See Section 3.3 for more details.

()

command group.
(a=hello; echo $a)

Note: A listing of commands within parentheses starts a subshell (see Section 3.16).

{}

block of code. This, in effect, creates an anonymous function.

The code block enclosed in braces may have I/O redirected to and from it.

Example 3-2. Code blocks and I/O redirection

#!/bin/bash

{
read fstab
} < /etc/fstab

echo "First line in /etc/fstab is:"
echo "$fstab"

exit 0

Example 3-3. Saving the results of a code block to a file

#!/bin/bash

#                rpm-check
#                ---------
# Queries an rpm file for description, listing, and whether it can be installed.
# Saves output to a file.
# 
# This script illustrates using a code block.

NOARGS=1

if [ -z $1 ]
then
  echo "Usage: `basename $0` rpm-file"
  exit $NOARGS
fi  

{ 
  echo
  echo "Archive Description:"
  rpm -qpi $1  #Query description.
  echo
  echo "Archive Listing:"
  rpm -qpl $1  #Query listing.
  echo
  rpm -i --test $1  #Query whether rpm file can be installed.
  if [ ! $? ]
  then
    echo "$1 can be installed."
  else
    echo "$1 cannot be installed."
  fi  
  echo
} > $1.test  # Redirects output of everything in block to file.

echo "Results of rpm test in file $1.test"

# See rpm man page for explanation of options.

exit 0
/{}

file pathname. Mostly used in 'find' constructs.

> >& >> <

redirection.

scriptname >filename redirects the output of scriptname to file filename. Overwrite filename if it already exists.

command >&2 redirects output of command to stderr.

scriptname >>filename appends the output of scriptname to file filename. If filename does not already exist, it will be created.

For a more detailed explanation, see Section 3.13.

<<

redirection used in "here document". See Section 3.22.

|

pipe. Passes the output of previous command to next one, or to shell. This is a method of chaining commands together.

echo ls -l | sh
passes the output of "ls -l" to the shell, with the same result as a simple "ls -l".

cat *.lst | sort | uniq
sorts the output of all the .lst files and deletes duplicate lines.

Note: If one of the commands in the pipe aborts, this prematurely terminates execution of the pipe. Called a broken pipe, this condition sends a SIGPIPE signal. (See Section 3.24 for more detail on signals.)

>|

force redirection (even if noclobber environmental variable is in effect). This will forcibly overwrite an existing file.

-

redirection from/to stdin or stdout.

(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xvfp -)
# Move entire file tree from one directory to another
# [courtesy Alan Cox, [email protected]]
#
# More elegant than, but equivalent to:
# cd source-directory
# tar cf - . | (cd ../target-directory; tar xzf -)

bunzip2 linux-2.2.15.tar.bz2 | tar xvf -
# --uncompress tar file--    | --then pass it to "tar"--
# If "tar" has not been patched to handle "bunzip2",
# this needs to be done in two discrete steps, using a pipe.
# The purpose of the exercise is to unarchive "bzipped" kernel source.

Note that the "-" is not itself a Bash operator, but rather an option recognized by certain UNIX utilities.

Where a filename is expected, redirects output to stdout (mostly seen with tar cf)

Example 3-4. Backup of all files changed in last day

#!/bin/bash

# Backs up all files in current directory
# modified within last 24 hours
# in a tarred and gzipped file.

if [ $# = 0 ]
then
  echo "Usage: `basename $0` filename"
  exit 1
fi  

tar cvf - `find . -mtime -1 -type f -print` > $1.tar
gzip $1.tar

exit 0
~

home directory. ~bozo is bozo's home directory, and ls ~bozo lists the contents of it. ~/ is the current user's home directory, and ls ~/ lists the contents of it.

White space

functions as a separator, separating commands or variables. White space consists of either spaces, tabs, blank lines, or any combination thereof. In some contexts, such as variable assignment, white space is not permitted, and results in a syntax error.

Blank lines

Blank lines have no effect on the action of a script, and are therefore useful for visually separating functional sections of the script.




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру