264 lines
5.3 KiB
Plaintext
264 lines
5.3 KiB
Plaintext
====================================
|
|
While statements
|
|
====================================
|
|
|
|
while something happens; do
|
|
echo a
|
|
echo b
|
|
done
|
|
|
|
---
|
|
|
|
(program
|
|
(while_statement
|
|
(command (command_name (word)) (word))
|
|
(do_group
|
|
(command (command_name (word)) (word))
|
|
(command (command_name (word)) (word)))))
|
|
|
|
====================================
|
|
While statements with IO redirects
|
|
====================================
|
|
|
|
while read line; do
|
|
echo $line
|
|
done < <(cat file)
|
|
|
|
---
|
|
|
|
(program
|
|
(while_statement
|
|
(command (command_name (word)) (word))
|
|
(do_group
|
|
(command (command_name (word)) (simple_expansion (variable_name))))
|
|
(file_redirect (process_substitution
|
|
(command (command_name (word)) (word))))))
|
|
|
|
====================================
|
|
For statements
|
|
====================================
|
|
|
|
for a in 1 2 $(seq 5 10); do
|
|
echo $a
|
|
done
|
|
|
|
---
|
|
|
|
(program
|
|
(for_statement
|
|
(variable_name)
|
|
(word)
|
|
(word)
|
|
(command_substitution (command (command_name (word)) (word) (word)))
|
|
(do_group
|
|
(command (command_name (word)) (simple_expansion (variable_name))))))
|
|
|
|
====================================
|
|
If statements
|
|
====================================
|
|
|
|
if cat some_file | grep -v ok; then
|
|
echo one
|
|
elif cat other_file | grep -v ok; then
|
|
echo two
|
|
else
|
|
exit
|
|
fi
|
|
|
|
---
|
|
|
|
(program
|
|
(if_statement
|
|
(pipeline
|
|
(command (command_name (word)) (word))
|
|
(command (command_name (word)) (word) (word)))
|
|
(command (command_name (word)) (word))
|
|
(elif_clause
|
|
(pipeline
|
|
(command (command_name (word)) (word))
|
|
(command (command_name (word)) (word) (word)))
|
|
(command (command_name (word)) (word)))
|
|
(else_clause
|
|
(command (command_name (word))))))
|
|
|
|
====================================
|
|
If statements with conditional expressions
|
|
====================================
|
|
|
|
if [ "$(uname)" == 'Darwin' ]; then
|
|
echo one
|
|
fi
|
|
|
|
---
|
|
|
|
(program
|
|
(if_statement
|
|
(bracket_command
|
|
(string (command_substitution (command (command_name (word)))))
|
|
(raw_string))
|
|
(command (command_name (word)) (word))))
|
|
|
|
====================================
|
|
Case statements
|
|
====================================
|
|
|
|
case "opt" in
|
|
a)
|
|
echo a
|
|
;;
|
|
|
|
b)
|
|
echo b;;
|
|
esac
|
|
|
|
case "$Z" in
|
|
ab*|cd*) ef
|
|
esac
|
|
|
|
---
|
|
|
|
(program
|
|
(case_statement (string)
|
|
(case_item (word)
|
|
(command (command_name (word)) (word)))
|
|
(case_item (word)
|
|
(command (command_name (word)) (word))))
|
|
(case_statement (string (simple_expansion (variable_name)))
|
|
(case_item (word) (word)
|
|
(command (command_name (word))))))
|
|
|
|
===============================
|
|
Subshells
|
|
===============================
|
|
|
|
(
|
|
./start-server --port=80
|
|
) &
|
|
|
|
---
|
|
|
|
(program
|
|
(subshell (command (command_name (word)) (word))))
|
|
|
|
===============================
|
|
Function definitions
|
|
===============================
|
|
|
|
do_something() {
|
|
echo ok
|
|
}
|
|
|
|
function do_something_else() {
|
|
echo ok
|
|
}
|
|
|
|
function do_yet_another_thing {
|
|
echo ok
|
|
} 2>&1
|
|
|
|
---
|
|
|
|
(program
|
|
(function_definition
|
|
(word)
|
|
(compound_statement (command (command_name (word)) (word))))
|
|
(function_definition
|
|
(word)
|
|
(compound_statement (command (command_name (word)) (word))))
|
|
(function_definition
|
|
(word)
|
|
(compound_statement (command (command_name (word)) (word)))
|
|
(file_redirect (file_descriptor) (word))))
|
|
|
|
=========================================
|
|
Variable declaration: declare & typeset
|
|
=========================================
|
|
|
|
declare var1
|
|
typeset -i -r var2=42 var3=10
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command (variable_name))
|
|
(declaration_command (word) (word)
|
|
(variable_assignment (variable_name) (word))
|
|
(variable_assignment (variable_name) (word))))
|
|
|
|
=========================================
|
|
Variable declaration: readonly
|
|
=========================================
|
|
|
|
readonly var1
|
|
readonly var2=42
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command (variable_name))
|
|
(declaration_command (variable_assignment (variable_name) (word))))
|
|
|
|
=========================================
|
|
Variable declaration: local
|
|
=========================================
|
|
|
|
local a=42 b
|
|
local -r c
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command
|
|
(variable_assignment (variable_name) (word))
|
|
(variable_name))
|
|
(declaration_command
|
|
(word)
|
|
(variable_name)))
|
|
|
|
=========================================
|
|
Variable declaration: export
|
|
=========================================
|
|
|
|
export PATH
|
|
export FOOBAR PATH="$PATH:/usr/foobar/bin"
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command (variable_name))
|
|
(declaration_command
|
|
(variable_name)
|
|
(variable_assignment (variable_name) (string (simple_expansion (variable_name))))))
|
|
|
|
===========================================
|
|
Expressions passed to declaration commands
|
|
===========================================
|
|
|
|
export "$(echo ${key} | tr [:lower:] [:upper:])=${p_key#*=}"
|
|
|
|
---
|
|
|
|
(program
|
|
(declaration_command
|
|
(string
|
|
(command_substitution
|
|
(pipeline
|
|
(command (command_name (word)) (expansion (variable_name)))
|
|
(command (command_name (word)) (concatenation (word)) (concatenation (word)))))
|
|
(expansion (variable_name) (word)))))
|
|
|
|
=========================================
|
|
Unset commands
|
|
=========================================
|
|
|
|
unset A
|
|
unset "$variable_name"
|
|
unsetenv -f ONE TWO
|
|
|
|
---
|
|
|
|
(program
|
|
(unset_command (variable_name))
|
|
(unset_command (string (simple_expansion (variable_name))))
|
|
(unset_command (word) (variable_name) (variable_name)))
|