Useful special characters a Sound an audible alert.

b Insert a backs.p.a.ce.

c Suppress the trailing newline (same as -n -n).

f Form feed.

Name exit Syntax exit[n]

Description Exit a sh.e.l.l script with status n n. The value for n n can be 0 (success) or nonzero (failure). If can be 0 (success) or nonzero (failure). If n n is not given, the exit status is that of the most recent command. is not given, the exit status is that of the most recent command.

Example if!test-fsomefile then echo"Error:Missingfilesomefile"

exit1 fi

Name for Syntax forxinlist do commands done Description a.s.sign each word in list list to to x x in turn and execute in turn and execute commands commands. If list list is omitted, it is a.s.sumed that positional parameters from the command line, which are stored in is omitted, it is a.s.sumed that positional parameters from the command line, which are stored in [email protected] [email protected], are to be used.

Example forfilenameinbigfile*;do echo"Compressing$filename"

gzip$filename done

Name function Syntax functionname { commands } Description Define function name name. Positional parameters ($1, $2 $2, ...) can be used within commands commands.

Example #functionmyfunc { echo"parameteris$1"

} #myfunc1 parameteris1 #myfunctwo parameteristwo

Name getopts Syntax getoptsstringname[args]

Description Process command-line arguments (or args args, if specified) and check for legal options. The getopts getopts is used in sh.e.l.l script loops and is intended to ensure standard syntax for command-line options. The is used in sh.e.l.l script loops and is intended to ensure standard syntax for command-line options. The string string contains the option letters to be recognized by contains the option letters to be recognized by getopts getopts when running the script. Valid options are processed in turn and stored in the sh.e.l.l variable when running the script. Valid options are processed in turn and stored in the sh.e.l.l variable name name. If an option letter is followed by a colon, the option must be followed by one or more arguments when the command is entered by the user.

Name if Syntax ifexpression1 then commands1 elifexpression2 then commands2 else commands fi Description The if if command is used to define a conditional statement. There are three possible formats for using the command is used to define a conditional statement. There are three possible formats for using the if if command: command: if-then-fi if-then-else-fi if-then-elif-then-...fi

Name kill Syntax kill[options]IDs Description Send signals to each specified process or job ID, which you must own unless you are a privileged user. The default signal sent with the kill kill command is command is TERM TERM, instructing processes to shut down.

Options -l List the signal names.

-s signal signal or or -signal -signal Specify the signal number or name.

Name read Syntax read[options]variable1[variable2...]

Description Read one line of standard input, and a.s.sign each word to the corresponding variable, with all remaining words a.s.signed to the last variable.

Example echo-n"Enterlast-name,age,height,andweight>"

readlastnameeverythingelse echo$lastname echo$everythingelese The name entered is placed in variable $lastname $lastname; all of the other values, including the s.p.a.ces between them, are placed in $everythingelse $everythingelse.

Name return Syntax return[n]

Description This command is used inside a function definition to exit the function with status n n. If n n is omitted, the exit status of the previously executed command is returned. is omitted, the exit status of the previously executed command is returned.

Name seq Syntax seq[OPTION]...LAST seq[OPTION]...FIRSTLAST seq[OPTION]...FIRSTINCREMENTLAST Description Print a sequence of numbers. This is useful in for for and and while while loops. loops.

Frequently used options -w Equalize the output"s width by padding with leading zeros.

-f or or --format=FORMAT --format=FORMAT Use the printf-style floating-point FORMAT.

Example year=$(date+%Y)#getcurrentyear formonthin$(seq-w112) { monthname=$(date-d"${year}-${month}-01"+%B) echo"Month$monthis$monthname"

} Month01isJanuary Month02isFebruary Month03isMarch Month04isApril Month05isMay Month06isJune Month07isJuly Month08isAugust Month09isSeptember Month10isOctober Month11isNovember Month12isDecember

Name shift Syntax shift[n]

Description Shift positional parameters down n n elements. If elements. If n n is omitted, the default is 1, so is omitted, the default is 1, so $2 $2 becomes becomes $1 $1, $3 $3 becomes becomes $2 $2, and so on.

Name source Syntax sourcefile[arguments]

.file[arguments]

Description Read and execute lines in file file. The file file does not need to be executable but must be in a directory listed in does not need to be executable but must be in a directory listed in PATH PATH. The dot syntax is equivalent to stating source source.

Name test Syntax testexpression [expression]

Description Evaluate the conditional expression and return a status of 0 (true) or 1 (false). The first form explicitly calls out the test test command. The second form implies the command. The second form implies the test test command. The s.p.a.ces around command. The s.p.a.ces around expression expression are required in the second form. are required in the second form. expression expression is constructed using options. Some Linux distributions have a version of is constructed using options. Some Linux distributions have a version of test test at at /usr/bin/test /usr/bin/test. If that is the case, the built-in bash bash version of version of test test will usually take precedence. will usually take precedence.

Frequently used options -d file file True if file file exists and is a directory exists and is a directory -e file file True if file file exists exists -f file file True if file file exists and is a regular file exists and is a regular file -L file file True if file file exists and is a symbolic link exists and is a symbolic link -n string string True if the length of string string is nonzero is nonzero -r file file True if file file exists and is readable exists and is readable -s file file True if file file exists and has a size greater than zero exists and has a size greater than zero -w file file True if file file exists and is writable exists and is writable -x file file True if file file exists and is executable exists and is executable -z string string True if the length of string string is zero is zero file1 - -ot file2 file2 True if file1 file1 is older than is older than file2 file2 string1 = = string2 string2 True if the strings are equal string1 != != string2 string2 True if the strings are not equal Example To determine if a file exists and is readable, use the -r -r option: option: iftest-rfile then echo"fileexists"

fi Using the [ ] [ ] form instead, the same test looks like this: form instead, the same test looks like this: if[-rfile]

then echo"fileexists"

fi

Name until Syntax until test-commands do commands done Description Execute test-commands test-commands (usually a (usually a test test command), and if the exit status is nonzero (that is, the test fails), perform command), and if the exit status is nonzero (that is, the test fails), perform commands commands and repeat. Opposite of and repeat. Opposite of while while.

Name while Syntax while test-commands do commands done Description Execute test-commands test-commands (usually a (usually a test test command), and if the exit status is nonzero (that is, the test fails), perform command), and if the exit status is nonzero (that is, the test fails), perform commands commands and repeat. Opposite of and repeat. Opposite of until until.

Example Example13-7 shows a typical script from a Linux system. This example is shows a typical script from a Linux system. This example is /etc/rc.d/init.d/sendmail /etc/rc.d/init.d/sendmail, which is the script that starts and stops Sendmail. This script demonstrates many of the built-in commands referenced in the last section.

Example13-7.Sample sendmail startup script #!/bin/bash # #sendmailThissh.e.l.lscripttakescareofstartingandstopping #sendmail.

# #chkconfig:23458030 #description:SendmailisaMailTransportAgent,whichistheprogram #thatmovesmailfromonemachinetoanother.

#processname:sendmail #config:/etc/mail/sendmail.cf #pidfile:/var/run/sendmail.pid

#Sourcefunctionlibrary.

./etc/rc.d/init.d/functions

#Sourcenetworkingconfiguration.

[-f/etc/sysconfig/network]&&./etc/sysconfig/network

#Sourcesendmailconfigureation.

if[-f/etc/sysconfig/sendmail];then ./etc/sysconfig/sendmail else DAEMON=no QUEUE=1h fi [-z"$SMQUEUE"]&&SMQUEUE="$QUEUE"

[-z"$SMQUEUE"]&&SMQUEUE=1h

#Checkthatnetworkingisup.

["${NETWORKING}"="no"]&&exit0

[-f/usr/sbin/sendmail]||exit0

RETVAL=0 prog="sendmail"

start(){ #Startdaemons.

echo-n$"Starting$prog:"

iftest-x/usr/bin/make-a-f/etc/mail/Makefile;then makeall-C/etc/mail-s>/dev/null else foriinvirtusertableaccessdomaintablemailertable;do if[-f/etc/mail/$i];then makemaphash/etc/mail/$i/dev/null2>&1 daemon/usr/sbin/sendmail$(["x$DAEMON"=xyes]&&echo-bd) $([-n"$QUEUE"]&&echo-q$QUEUE)$SENDMAIL_OPTARG RETVAL=$?

echo [$RETVAL-eq0]&&touch/var/lock/subsys/sendmail

if!test-f/var/run/sm-client.pid;then echo-n$"Startingsm-client:"

touch/var/run/sm-client.pid chownsmmsp:smmsp/var/run/sm-client.pid if[-x/usr/sbin/selinuxenabled]&&/usr/sbin/selinuxenabled;then /sbin/restorecon/var/run/sm-client.pid fi daemon--checksm-client/usr/sbin/sendmail-Lsm-msp-queue-Ac -q$SMQUEUE$SENDMAIL_OPTARG RETVAL=$?

echo [$RETVAL-eq0]&&touch/var/lock/subsys/sm-client fi

return$RETVAL }

reload(){ #Stopdaemons.

echo-n$"reloading$prog:"

/usr/bin/newaliases>/dev/null2>&1 if[-x/usr/bin/make-a-f/etc/mail/Makefile];then makeall-C/etc/mail-s>/dev/null else foriinvirtusertableaccessdomaintablemailertable;do if[-f/etc/mail/$i];then makemaphash/etc/mail/$i

killprocsendmail-HUP RETVAL=$?

echo if[$RETVAL-eq0-a-f/var/run/sm-client.pid];then echo-n$"reloadingsm-client:"

killprocsm-client-HUP RETVAL=$?

echo fi return$RETVAL }

© 2024 www.topnovel.cc