0% found this document useful (0 votes)
21 views2 pages

POSIX Shell Scripting Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

POSIX Shell Scripting Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

POSIX-2017

Shell & Utilities

Shell Command Language

FC LFLAGS OPTARG TMPDIR


1. Shell Introduction
FCEDIT LINENO OPTERR TZ
2. Quoting FFLAGS LINES OPTIND USER
| & ; < > ( ) $ ` \ " ' <space> <tab> <newline> GET LISTER PAGER VISUAL
* ? [ # ˜ = %
1. Escape Character (Backslash) GFLAGS LOGNAME PATH YACC
2. Single-Quotes HISTFILE LPDEST PPID YFLAGS
3. Double-Quotes
HISTORY MAIL PRINTER
3. Token Recognition HISTSIZE MAILCHECK PROCLANG
1. Alias Substitution HOME MAILER PROJECTDIR
4. Reserved Words
6. Word Expansions
! do esac in 1. Tilde Expansion
{ done fi then 2. Parameter Expansion
} elif for until ${parameter}
case else if while ${parameter:-[word]}
${parameter:=[word]}
5. Parameters and Variables ${parameter:?[word]}
1. Positional Parameters ${parameter:+[word]}
2. Special Parameters ${#parameter}
3. Shell Variables ${parameter%[word]}
${parameter%%[word]}
XBD Environment Variables ${parameter#[word]}
${parameter##[word]}
ARFLAGS IFS MAILPATH PS1 3. Command Substitution
$(command)
CC LANG MAILRC PS2 4. Arithmetic Expansion
CDPATH LC_ALL MAKEFLAGS PS3 $((expression))
5. Field Splitting
CFLAGS LC_COLLATE MAKESHELL PS4 6. Pathname Expansion
CHARSET LC_CTYPE MANPATH PWD 7. Quote Removal
COLUMNS LC_MESSAGES MBOX RANDOM
7. Redirection
DATEMSK LC_MONETARY MORE SECONDS [n]redir-op word
DEAD LC_NUMERIC MSGVERB SHELL 1. Redirecting Input
2. Redirecting Output
EDITOR LC_TIME NLSPATH TERM 3. Appending Redirected Output
ENV LDFLAGS NPROC TERMCAP 4. Here-Document
EXINIT LEX OLDPWD TERMINFO 5. Duplicating an Input File Descriptor
6. Duplicating an Output File Descriptor until compound-list-1
7. Open File Descriptors for Reading and Writing do
compound-list-2
8. Exit Status and Errors done
1. Consequences of Shell Errors
2. Exit Status for Commands 5. Function Definition Command
fname ( ) compound-command [io-redirect ...]
9. Shell Commands
1. Simple Commands 10. Shell Grammar
2. Pipelines 1. Shell Grammar Lexical Conventions
[!] command1 [ | command2 ...] 2. Shell Grammar Rules
3. Lists 11. Signals and Error Handling
command1 & [command2 & ... ]
command1 [; command2] ... 12. Shell Execution Environment
command1 [ && command2] ...
command1 [ || command2] ... 13. Pattern Matching Notation
1. Patterns Matching a Single Character
4. Compound Commands 2. Patterns Matching Multiple Characters
( compound-list ) 3. Patterns Used for Filename Expansion
{ compound-list ; }
14. Special Built-In Utilities
for name [ in [word ... ]] • break - exit from for, while, or until loop
do • colon - null utility
compound-list • continue - continue for, while, or until loop
done • dot - execute commands in the current environment
• eval - construct command by concatenating arguments
case word in
[(] pattern1 ) compound-list ;; • exec - execute commands and open, close, or copy file
[[(] pattern[ | pattern] ... ) compound-list ;;] descriptors
[[(] pattern[ | pattern] ... ) compound-list] • exit - cause the shell to exit
esac • export - set the export attribute for variables
• readonly - set the readonly attribute for variables
• return - return from a function or dot script
if compound-list • set - set or unset options and positional parameters
then • shift - shift positional parameters
compound-list
[elif compound-list • times - write process times
then • trap - trap signals
compound-list] ... • unset - unset values and attributes of variables and
[else functions
compound-list]
fi

while compound-list-1
do
compound-list-2
done

Common questions

Powered by AI

Alias substitution in POSIX shell occurs during token recognition before execution, where command words that match defined aliases are replaced by their respective command strings . This can redefine command behavior, providing shortcuts or modified versions of commands, which can streamline execution or introduce unintended behavior changes if not managed carefully .

Reserved words such as `if`, `then`, and `else` in POSIX shell scripts are used to control the logical flow of execution. The `if` statement evaluates a condition, and if true, the commands following `then` are executed. If false, the `else` block (if present) may execute alternative commands . This allows for conditional response execution and structured decision-making in scripts .

Pathname expansion in POSIX shell involves pattern matching with wildcards to generate a list of existing filenames that match the given pattern. It is commonly used for operations like batch processing files, searching directories, or managing files with similar naming conventions . Common wildcards include `*` for multiple characters and `?` for a single character match .

The `trap` command in POSIX shell scripting allows the user to specify actions to perform when the shell receives certain signals. It is a critical tool for ensuring scripts can handle interruptions gracefully and execute cleanup actions before exiting . It allows for setting traps to execute specific code blocks when signals are caught in the running shell process .

Command substitution $(command) in POSIX shell replaces the command with its output, thereby allowing the results of a command to directly fuel another command or assignment . Arithmetic expansion $((expression)), however, evaluates arithmetic expressions and substitutes the result, which is primarily used for mathematical operations within scripts .

Here-documents in POSIX shell allow for redirecting a block of input to a command from within the script itself, marking the beginning and end with a delimiter. This contrasts with traditional input redirection that typically reads data from files or standard input. Here-documents are useful for including multi-line strings or commands in a script without external file dependencies .

Environment variables such as PATH and IFS are vital in shell scripts as they configure the environment context in which the commands execute. PATH determines the directories the shell searches for executable programs, while IFS (Internal Field Separator) influences how the shell interprets fields within input . Proper management of these variables ensures scripts execute correctly and handle inputs appropriately .

The '!' operator in POSIX shell scripting negates the exit status of a command in a pipeline, effectively inverting its success or failure condition . By flipping the exit status, it changes how subsequent commands evaluate conditions, allowing for more complex control over script logic based on command outcomes .

Special parameters in POSIX shell scripting provide specific, predefined information to the shell, such as the number of positional parameters ($#) or the current process ID ($$). Positional parameters, on the other hand, represent arguments passed to the script (e.g., $1, $2, etc.) and are directly influenced by user input .

Single-quotes in POSIX shell scripting preserve the literal value of each character within the quotes. Double-quotes, however, still allow for variable and command substitution within the quoted text . This distinction is crucial for controlling how the shell processes special characters and variables, impacting how strings are interpreted and evaluated .

You might also like