Este documento es una traducción de Introduction to Bash Shell Scripting de Donald A Kassebaum, siéntase libre de adicionar más ejemplos o complementar con lo que considere de utilidad.
Introducción a la creación de guiones de Bash
Contents
- Referencias
- Comienzo
- Variables de Shell
- Algunos comandos útiles en los scripts
- Coloreado de scripts
- Funciones de shell y scripts
- Operaciones con variables
- El versátil 'expr'
- Control de flujo
- Interfaces de usuario
- Operaciones de E/S en scripts
- Manejo de procesos
- Ejemplos de scripts o funciones útiles
Referencias
Learning the Bash Shell, Cameron Newham & Bill Rosenblatt (O'Reilly & Associates, Inc)
Linux in a Nutshell, Jessica Perry Hekman (O'Reilly & Associates, Inc)
The UNIX Programming Environment, Brian W Kernigham & Rob Pike (Prenice Hall)
Unix Power Tools, Jerry Peek, Tim O'Relly, & Mike Lookides (O'Reilly & Associates,Inc)
Advance Bash Scripting Guide : Appendix B has some good Reference Cards
Comienzo
Obtención de Ayuda
man |
Get off information about commands |
man -k |
Get off information via keyword |
info |
Read info documents |
help |
Show help on shell built-in commands |
command --help |
Help on command .i.e. cp --help Will give help on cp |
bash -c "help" |
Short help on bash |
bash -c "help set" |
Short help on bash options |
Caracteres especiales
Space |
Argument separator |
\ |
Quote a single character A \ followed by a carriage return extend the current line |
' |
Quote rext with spaces in it i.e. 'Hello world' |
$' |
Quote allows string expansion, backslash-escaped characters |
" |
Quote rext, expands variables and command substitution |
` |
Command substitution i.e. echo "The date is `date`" |
$ |
Denote a shell variable |
# |
Comments the rest of the line |
; |
Commands separator |
(commands) |
Run multiple commands in a subshell |
Control-C |
Interrupt a command |
Control-D |
Sends End of File from terminal |
Control-U |
Erases the entire command line |
Control-\ |
Is a stronger terminate than Control-c |
Control-Z |
Suspend process |
Redirección, tuberías y filtros
- |
Standard In for some commands |
0 |
Standard In |
1 |
Standard Out |
2 |
Standard Error |
>2 &1 |
Redirect standard error to standard out |
&> |
Redirect standard error & standard out |
< |
Redirect input |
> |
Redirect output |
>> |
Concatenate output to file |
<< |
Here Is File for scripts |
| |
Pipe |
tee |
Command write to file and standard out |
tee |
-a FILE #Allow appending to FILE |
xargs |
Build command from standardin |
Comodines
* |
Wildcard for any character(s) |
? |
Wildcard for single character |
[set] |
Wildcard for character in set |
[^set] |
Wildcard for not the character in the set |
[!set] |
Wildcard for not the character in the set |
{ab,dc} |
Wildcard for alternate between commas |
All |
wildcard work with existing files |
Only |
{} alternate work to create files |
Control de procesos
(command1; command) |
Run command1, then command2 in subshell |
command1&&command |
Run command1, then command2 if command1 successes |
command1||command |
Run command1, then command2 if command1 fails |
Expresiones Regulares
- Definición
- caracteres de patrones de texto y metacaracteres
A continuación algunos metacaracteres.
\ |
Escape character |
Metacaracteres con un solo caracter |
|
. |
Matches any one character |
[...] |
Matches any one character in a set |
[^...] |
Matches any one character not in the set |
Cuantificadores |
|
* |
Matches the previous character zero or more times |
\{n\} |
Matches the previous character n times |
\{n,m\} |
Matches the previous character at least n & at most m |
\{n,\} |
Matches the previous character n or more times |
Anclas |
|
^ |
Matching at the start the line |
$ |
Matching at the end of line |
Agrupamiento \( \) |
|
Comandos que usan expresiones regulares
- awk
- Pattern scanning and text processing language
- ed
- Line-oriented text editor
- egrep
- extended grep
- emacs
- Emacs full screen text editor
- ex
- Line-oriented text editor
- expr
- Command evaluates an expression
- fgrep
- Grep from patterns in a file
- gawk
- GNU pattern scanning and processing language
- grep
Searches file for pattern (also see fgrep & egrep)
grep [OPTIONS] PATTERN [INPUT-FILE...] -E same as egrep |
|
-c |
Count |
-e |
pattern (for multiple pattern on line) |
-f |
same as fgrep |
-i |
Ignore case |
-l |
Only list files containing pattern |
-q |
Quit (No output, only Return Code) |
-v |
Invert sense mode |
- perl
- Perl scripting
- python
- Python scripting
- sed
- Applies a set of user-specified editing command to a file
sed [OPTIONS] 'sed_command' [INPUT_FILE...] -n Suppress automatic printing |
|
-e |
expression - sed_command |
substitute |
other_text for some_text sed 's/some_text/other_text/g' FILE > NEWFILE |
multiple |
changes sed -e 's@abc@def@g' -e 's@xyz@mno@g' FILE |
out line with faq in them sed -n '/faq/p' FILE |
|
change |
Page ### to (Page ###) at end of line sed 's/Page [0-9]+$/(&)/' file # & replace the match |
delete |
blank lines sed '/^[ \t]*$/d |
- tcl
- Tool command language
- vi
- Full screen text editor
Variables de Shell
Variables de shell embebidas
CDPATH |
Path of shortcuts for cd (like PATH) |
COLUMNS |
Numbers of columns on the display |
EDITOR |
Path for editor |
HISTSIZE |
Number of commands in command history (default 500) |
IFS |
Input Field Separator |
LINES |
Numbers of lines on the display |
OFS |
Output Field Separator |
SECONDS |
Seconds that this shell is running |
SHELLOPT |
Colon separate list of shell options |
Variables de Ambiente
export var |
Will make a variable an environment variable |
HOME |
User's home directory |
LOGNAME |
User's name |
Name of user's mailbox |
|
PATH |
List of directories to be search by the shell to find programs whose names are type as commands |
PS1 |
String that is used by the shell prompt |
PWD |
Name of current directory |
SHELL |
Name of current shell |
TERM |
The kind of terminal being used |
Environment variable are global to shell and subshells
Variables de usuario
Pueden ser en mayúsculas o minúsculas
var=value |
Definir una variable |
var="" |
Definir la variable von valor nulo |
local var |
Definir la variable en alcance local |
Variables posicionales
- o $0 Name of function or script being called o $1 ... $9 Replace by arguments to shell or function o ${n} Replace by n-th arguments to shell or function required if number of argument is over 9
Variables especiales
- o $? Exit status or return code of last command o $# Number of arguments o $@ Argument 1 thru n with Input Field Separator o $* "$1" $2" ... $n o $! Process id of last background process o $$ Process id of shell running this script o $- The current shell flags
Algunos comandos útiles en los scripts
- basename Strip directory and option suffix
- declare Built-in command declares variable
- dirname Strip non-directory part
- echo Built-in command display message to standard out
- echo -n Built-in command display message to standard out without newline
- echo -e Builtin Command display message to standout with escape sequences
- enable Built-in command to enable/disable (-n) built-in commands
- env Built-in command displays environment variables
- eval Built-in command evaluate arguments before executing results
- exec Built-in command runs command as the shell process
- exit Built-in command exits shell
- expr Evaluates an expression and output its value
- false Built-in command always return false condition
- local Built-in command make variable local
- read Built-in command reads data into variable from standard in
- seq Print a sequence of numbers
- o seq [OPTION]... LAST o seq [OPTION]... FIRST LAST o seq [OPTION]... FIRST INCREMENT LAST o -w equalize width by padding with leading zeroes
- sleep Command causes execution to stop for a specified number of seconds
- test Built-in command tests for various conditions, such as existence of a file, useful for controlling conditional script execution
- time Built-in command times commands
- times Print accumulated user and system times.
- true Built-in command always return true condition
- type Built-in command show what word is
- wait wait for the completion of background processing; is used to ensure that critical processing is complete before proceeding in a script
Coloreado de scripts
- tput sgr0 #Reset text attributes to normal without clearing screen
- Escape sequence to change colors #\e[${forground};${background}m example white on black sequence is \e[37;40m #Forground colors Backgrounfd Color black=30; bgblace=40 red=31; bgred=41 green=32; bggreen=42 yellow=33; bgyellow=43 blue=34; bgblue=44 magenta=35; bgmagenta=45 cyan=36; bgcyan=46 white=37; bgwhite=47
- Turn text attribute off \e[0m
- Change text attribute bold \e[1m
- Change text attribute underline \e[4m
- Change text attributer everse \e[7m
Funciones de shell y scripts
Funciones
- o Function must be sourced just like .bashrc o type function will list the function o function functionname { shell commands } o functionname () { shell commands } o Example - a helpful function function givehelp { exec $1 --help | more; }
Scripts
- o #!/bin/bash Script name run script is run if execute bit is set o #!/bin/bash -x As above, but script lines are displayed o bash -x script As above o /usr/bin/env bash Use the environment bash for the script
Opciones de depuración para los scripts
- o set -o OPTION Command line Action set -o noexec sh -n Don't run command, just check for syntax o set -o verbose sh -v Echo commands before running them o set -o xtrace sh -x Echo commands after running them o set +o OPTION Turns OPTION off
Operaciones con variables
Uso de variables
- o $var Value of variable var o "$var" Is null if variable is undefined avoids some shell syntax errors if variable is undefined o ${var} Value of variable var avoids confusion when concatenating with text o ${#var} Gives the length of the string contained in var
o ${var:FIRST:N} Extract string from var starting at FIRST position and continuing for N-1 characters. Note FIRST starts at 0.
Paso de una variable a un programa o un script
- o echo $var | command
Arreglos
- o declare -a name Declare an array o name[index]=value Just assigning a value defines it o Index starts at zero o No maximum limit o Need not be contiguous
Establecer valores para las variables al ejecutar un comando
o var=command var is set to output of command o var=$(command) same as above
Operaciones aritméticas
- o $(( expression )) Almost the same as 'expr expression' o $[ expression ] Same as above o ( expression ) Groups expression within $(( ... )) o expr expression Note must quote *
Operadores aritméticos
- o + add o - substract o * multiply o / divide o % remainder o # number conversion (Only in bash not expr)
Operaciones de prueba
- [ -option arg ] Same as 'test -option arg
-option arg Same as 'test -option arg
- [ arg1 -option arg2 ] Same as 'test arg1 -option arg2'
arg1 -option arg2 Same as 'test arg1 -option arg2'
- See test command for test complete list of options
- o string1 = string2 String 1 equals string 2 o string1 !=string2 String 1 not equals string 2 o -n string String is not zero length o -z string String is zero length o -d FILE File is a directory o -e FILE File exists o -f FILE File exists and is a regular file o -r FILE File exists and is readable o -s FILE File exists and has length greater than zero o -w FILE File exists and is writable o -x FILE File exists and is executable o num1 -eq num2 Number 1 equals number 2 o num1 -ne num2 Number 1 not equals number 2 o num1 -lt num2 Number 1 less than number 2 o num1 -le num2 Number 1 less than or equals number 2 o num1 -gt num2 Number 1 greater than number 2 o num1 -ge num2 Number 1 greater than or equals number 2
Operaciones en cadenas
- ${varname:-word} Return var if exists and is not null, else word
- ${varname:+word} Return word if var exists and is not null, else null
- ${varname:?mess} Return var if exists and is not null, else display mess and return from script with error
Operaciones con patrones
- ${variable#pattern} If the pattern matches the beginning of the variable value, delete the shortest part that matches and return the rest
- ${variable##pattern} If the pattern matches the beginning the of variable value, delete the longest part that matches and return the rest
- ${variable%pattern} If the pattern matches the end of the variable value, delete the shortest part that matches and return the rest
- ${variable%%pattern} If the pattern matches the end of the variable value, delete the longest part that matches and return the rest
El versátil 'expr'
- expr string : regrep Anchored pattern match
- expr match string pattern Same as above
- expr substr string pos {len] Substring beginning at pos of length len
- expr length string Length of string
Control de flujo
if - Información general
- if command Test return code of command
if command1 && command2 Test return code of command1 and command2 if command1 || command2 Test return code of command1 or command2 command can be condition i.e. [ $# -eq 0 ]
if/then/else or if/then/elif..
if condition
then statements...
[else statements...]
fi
if condition
then statements...
[elif condition
then statements...]
[else] statements...]
fi
Valor de retorno de una función
- return [ numeric expression or variable ] This is the return code
for
for name [ in list ] do
statements
done
for variable = start to end do
statements
done
while/until
while condition do
statements
done
until condition do
statements
done
break/continue
break [n] Break out of loop & select
- continue [n] Continue next iteration of loop
case
case expression in
pattern1[|pattern11] } statements ;;
pattern2[|pattern21] } statements ;;
...
esac
Interfaces de usuario
select
- select name [in list] do
- statements that can use $name
- Generate a menu of each item in the list formatted with a number for each choice
- Prompt the user for the number
- Store the selected choice in name. The number is stored in REPLY
- execute the statements in the do
- Repeat the process again
Opciones por la línea de comandos
- shift Shift 1st argument form the argument list
- getopts Used to process command line options
- OPTIND Variable contains number of options
- OPTARG Default variable for option
- Example while getopts ":ab:c" opt; do case $opt in a) process_option_a;; b) process_option_b $OPTARG is the option's argument;; c) process_option_c;; esac done shift $(($OPTIND - 1))
Operaciones de E/S en scripts
- printf Built-in command for formatted output
- read Built-in command to read one line into variable(s) read #Everything entered goes to REPLY read var #Everything is read into var read a b #Read 1st word in to a and rest into b read -t 300 var #Read with 300 second timeout
Example how to use read from $file while read line; do do_something_with_line done <$file
Manejo de procesos
Señales
- Name Number Control Character
- o EXIT 0 Used to trap exiting script o HUP 1 Logout o INT 2 Control-C o QUIT 3 Control-\ o KILL 9 can not be ignored or trapped o TERM 15 Default kill o TSTP 24 Control-Z
Trampas
- o trap "" signal-list Ignore signal o trap "cmds" signal-list Execute commands if signal is caught o trap signal-list Reset signal to original condition o trap : signal-list (undocumented) ignore signal, pass to child Signal are normally not passed to subprocesses o Examples trap 'rm tmpfile; exit' 0 1 2 #remove tmpfile on exit, logout, interrupt trap "echo 'You hit Control-C'" INT while true ; do sleep 60 done o Example parent child process #!/bin/bash #parent echo parent running trap 'echo parent exiting; exit' 0 trap :2 child sleep 1000 #!/bin/bash #child echo child started. pid is $$ trap 'echo child got signal 2; exit' INT sleep 1000
Ejemplos de scripts o funciones útiles
# Function top5 Example how to set defaults # Usage top5 {n} #list n processes
function top5 {
ps -ef | head -${1:-5}
}# Function hereis Example of HERE IS FILE and handling arguments
# Usage hereis word1 word2 ...
function hereis {
for name in "$@"
do
cat <<MSG This is an example of an HERE IS FILE. One argument is ${name}. The date is `date`.
MSG
done
}# Function pick Return selected items by user
# Usage: .e.g var=`pick *`
function pick {
for name in $@ ; do #for each item in argument list
echo -n "$name (y/n/q)?" >/dev/tty #ask user to select
read ans #read answer from standard in
case $ans in #Check choices
y*) echo $name;; #selected
q*) break;; #skip rest of arguments
*) continue;; #skip item
esac
done
}# Function acal Display a nicer calendar
# but will accept Alphabetic month
function acal {
m=""
case $# in
0) cal; return;; #no arguments
1) m=$1; y=`date +%Y`;; #1 argument
2) m=$1; y=$2;; #2 arguments
esac
case $m in
Jan*|jan* ) m=1;;
Feb*|feb* ) m=2;;
Mar*|mar* ) m=3;;
Apr*|apr* ) m=4;;
May|may } m=5;;
Jun*|jun* ) m=6;;
Jul*|jul* ) m=7;;
Aug*|aug* ) m=8;;
Sep*|sep* } m=9;;
Oct*|oct* ) m=10;;
Nov*|nov* ) m=11;;
Dec*|dec* ) m=12;;
[1-9]|1[0-2] ) ;; #numeric month
*) ) y=$m; m="";;
esac
cal $m $y
}## Function selectex - Example select
#
function selectex () {
choices="/bin /usr /home"
select selection in $choices; do
if [ $selection ]; then
ls $selection
break
else
echo 'Invalid selection'
fi
done
}# Function fwhich Which command in $PATH is executed
#
function fwhich {
if [[ $# -eq 0 ]] ;
then cat << EndOfHelp 1>&2; return 2
Usage fwhich command #Example of parsing the $PATH
Return 0 - found
Return 1 - not found
Return 2 - No arguments
EndOfHelp
fi
for path in `echo $PATH | sed 's/^:/.:/
s/::/.:/g
s/:$/:./
s/:/ /g'`
do
if [[ -x $path/$1 ]] ; # does executable file exists here?
then echo $path/$1 # found it
return 0
fi
done
return 1 # not found
}# Name: overwrite Copy standard input to output after EOF
function overwrite {
if [[ $# -lt 2 ]] ;
then echo "Usage: overwrite file command [args]" 1>&2; return 2
fi
file=$1; shift
new=/tmp/overwrite1$$; pld=/tmp/overwrite2$$
trap 'rm -f $new $old; return 1' 1 2 15 # clean up files
"$@" > $new
if [[ $? -eq 0 ]] ; # collect output
then # command completed successfully
cp $file $old # save original file
trap '' 1 2 15 # we are committed; ignore signals
cp $new $file # copy new file into file
rm -f $new $old # remove temp files
else
echo "overwrite: $1 failed, $file unchanged" 1>$2
return 1
fi
}# Name: zgrep
# Purpose: caseless grep of gzip files
# Usage: zgrep text files.gz
#
function zgrep {
if [ $# -eq 0 ] ; then
echo "Usage: zgrep grep_text files.gz"
return 2
fi
text=$1
shift
while [ $# -gt 0 ]
do
echo $1 gzip -cd $1 | grep -i $text
shift
done
}# Name: hgrep
# Purpose: highlighting grep
# Usage: hgrep pattern files
#
function hgrep {
if [ $# -lt 2 ] ; then
echo "Usage: hgrep pattern files"
return 2
fi
pattern=$1;shift
sep=$'\001' #note use of $' ' to create control characters
bold=$'\e[1m'; off=$'\e[0m'
underline=$'\e[4m'; reverse=$'\e[7m' #other choices of highlighting
sed -n "s${sep}${pattern}${sep}${reverse}&${off}${sep}gp" $*
} 
