link

fajne metacharcters Bash rozroznia:
blank - pusty znak - spacja, tab
word/token - ciagn znakow traktowany jako jednosc
name - ciagn znakow alfanumerycznych oraz underscores
metacharacters: znaki specjalne. Rozdzielaja slowa gdy nie sa cytowane
"| & ; ( ) < > space tab" control operators - pelnia funkcje
"|| & && ; ;; ( ) | |& " bash

[root@localhost ~]# echo "zmienna to \$SHELL, a jej output to \"`echo $SHELL`\"" zmienna to $SHELL, a jej output to "/bin/bash" [root@localhost ~]#

metacharacters blank (spacja,tab) | & ; ( ) < >
control operators newline || | && & ;; ; \ ( ) |&
escape characters / - nast. znak
" - wszystkie poza $ ' \
' - wszystkie wewnatrz
ANSI - \n \t \NNN
pipelines | - stdout, |& - stderr
list of commands ; - foreground, sequentially
& - background, asynchronously,
&& - and CMD1 exit status = -> CMD2
|| - or CMD1 exit status !0 -> CMD2

simple command - word metacharacter word control operator
pipeline - simple command - control operator |, |& - simple command
line of commands - pipeline - separated by ; & && || - pipeline - terminated by ; & newline
compound - reserved word/control operator - ... - corresponding reserved word/control operator

dla looping - reserved word: until, while, for
dla conditional - reserved word: if, case, select, ((arithmetic))
dla grouping - reserved word: ( ), { }

wordblankwordcontrol operator
= cd|blank|/|newline = cd /

CMD1 | CMD2
CMD1 |& CMD2 = CMD1 2>&1 CMD2

kazdy pipe wykonuje sie w swoim subshellu

! cd / - daje exit status 1

exit status = return status - wartosc zwaracana przez polecenie po jego wykonaniu
signal - kernel komunikuje sie z procesami poprzez sygnaly

metacharacters:
blank (spacja,tab) | & ; ( ) < >
ma specjalna wlasciwosc w bash, dlatego gdy chcemy go uzyc bez specjalnych lasciwosci, to go cytujemy. to znak, ktory oddziela slowa gdy jest niezacytowany

metacharacters zacytowane - slowo (word) = token
metacharacters niezacytowane - operator (oddziela slowa) = token

token - sekwencja znakow traktowana przez shella jako osobna jednostka


control operator- token (slowo lub operator; sekwencja znakow interpretowana przez shella jako jednostka) pelniacy funkcje kontrolna.
control operators:
newline || | && & ;; ; \ ( ) |&


Jezeli mamy slowo z metacharacter, jak usunac jego specjalne znaczenie? Poprzez cytowanie na 3sposoby
\ - escape character - dla nastepnego znaku, poza newline
' ' - dla kazdego znaku wewnatrz. zawsze musi byc zamkniety, wewnatrz nie moze byc samego znaku '
" " - dla wszystkich znakow wewnatrz, poza: $ ' \ (i! gdy history expansion is enabled)
$ ' - zachowuja swoje znaczenie
\ - zachowuje znaczenie gdy \$ \' \" \\, \newline:
przy " ", specjalne znaczenia maja znaki * i @

Czyli, jezeli chcemy uzywac zmiennych, uzywamy " ".
gdy chcemy dac haslo, uzywamy ' '

A double quote may be quoted within double quotes by preceding it with a backslash


pipe - sekwencja komend oddzielonych operatorami | albo |&
|& - command 1 STERR jako input command 2, skrot dla 2>&1|

listy komend - sekwencja kilku pipe
oddzielonych operatorami ; & && ||
konczonych operatorami ; & newline

zakonczone & - shell wykona komende w subshell
oddzielone ; - komendy wykonane beda sekwencyjnie

AND = && - CMD2 wykona sie, gdy CMD1 exit status 0
OR = || - CMD2 wykona sie, gdy CMD1 exit status !0

komendy zlozone - rozpoczynaja sie i koncza zarezerwowanym slowem/operatorem
1. petle - pomocnicze komendy builtin to break (przerywa), continue (resume next iteration)
until
until TEST-COMMANDS; do CONSEQUENT-COMMANDS; done
tak dlugo, jak test=!0, wykonuj consequent.
consequent zwraca exit status jako return status dla test
inaczej:
TEST exit status !0 - wykona CONS (ostatnia CONS wysyla return status)
TEST exit status 0 - nie wykona CONS
while
while TEST-COMMANDS; do CONSEQUENT-COMMANDS; done
tak dlugo bedzie wykonywal CONS, dopoki TEST uzyska exit status 0.
CONS wysyla return status
inaczej:
TEST exit status 0 - wykona CONS (ostatnia CONS wysyla return status)
for
for NAME [ [in [WORDS ...] ] ; ] do COMMANDS; done
WORDS = 1,2,3
1 - wykona COMMANDS
2 - wykona COMMANDS
3 - wykona COMMANDS
inna forma:
for (( EXPR1 ; EXPR2 ; EXPR3 )) ; do COMMANDS ; done
for (( EXPR1 ; EXPR2 ; EXPR3 )) ; do COMMANDS ; done
First, the arithmetic expression EXPR1 is evaluated according to
the rules described below (*note Shell Arithmetic::). The
arithmetic expression EXPR2 is then evaluated repeatedly until it
evaluates to zero. Each time EXPR2 evaluates to a non-zero value,
COMMANDS are executed and the arithmetic expression EXPR3 is
evaluated. If any expression is omitted, it behaves as if it
evaluates to 1. The return value is the exit status of the last
command in LIST that is executed, or false if any of the
expressions is invalid.
i tyle go widzieli...


konstrukcje warunkowe
if
case

The syntax of the `if' command is:

if TEST-COMMANDS; then
CONSEQUENT-COMMANDS;
[elif MORE-TEST-COMMANDS; then
MORE-CONSEQUENTS;]
[else ALTERNATE-CONSEQUENTS;]
fi

`((...))' albo let "EXPRESSION"
(( EXPRESSION ))
value !0 - return 0; else 1

[root@localhost ~]# ((5-4))
[root@localhost ~]# echo $?
0
[root@localhost ~]# ((5-5))
[root@localhost ~]# echo $?
1
[root@localhost ~]#

[root@localhost ~]# echo $((4+5))
9



`[[...]]'
[[ EXPRESSION ]]
==' and `!=
=~ string


`( EXPRESSION )'
Returns the value of EXPRESSION. This may be used to
override the normal precedence of operators.

`! EXPRESSION'
True if EXPRESSION is false.

`EXPRESSION1 && EXPRESSION2'
True if both EXPRESSION1 and EXPRESSION2 are true.

`EXPRESSION1 || EXPRESSION2'
True if either EXPRESSION1 or EXPRESSION2 is true.
The `&&' and `||' operators do not evaluate EXPRESSION2 if the
value of EXPRESSION1 is sufficient to determine the return value
of the entire conditional expression.

Grupowanie komend jako jednostka
() - wykonywanie komend w subshell
{} - wykonywanie komend w bierzacym shellu

coproc
funkcje (3.3)

zmienne
PARAMETER=value
ZMIENNA=wartosc
NAZWA=wartosc

[root@localhost ~]# PIES="kot"
[root@localhost ~]# echo $PIES
kot
[root@localhost ~]#

positional parameters
[root@localhost ~]# set 1="bleble"
[root@localhost ~]# echo $1
1=bleble



specjalne zmienne:
positional parameters
$*
"$*"' is equivalent to "$1C$2C...
"$@"' is equivalent to `"$1" "$2" ...
$# - liczba positional parameters
$? - exit status ostatniej komendy
$- - nie mam pojecia
Expands to the current option flags as specified upon
invocation, by the `set' builtin command, or those set by the
shell itself (such as the `-i' option).
$$ - proces id of shell
$! - proces id ostatniej komendy z bg
$0 - nazwa shella
$_ - pokazuje opcje ostatniej komendy

brace expansions
[root@localhost ~]# echo a{d,c,b}e
ade ace abe


The following table shows how Bash treats unquoted tilde-prefixes:

`~'
The value of `$HOME'

`~/foo'
`$HOME/foo'

`~fred/foo'
The subdirectory `foo' of the home directory of the user `fred'

`~+/foo'
`$PWD/foo'

`~-/foo'
`${OLDPWD-'~-'}/foo'

`~N'
The string that would be displayed by `dirs +N'

`~+N'
The string that would be displayed by `dirs +N'

`~-N'
The string that would be displayed by `dirs -N'


3.5.4 Command Substitution
$(COMMAND)
or
`COMMAND`


arithmetic
$(( EXPRESSION ))

process substitution
<(LIST)
or
>(LIST)

pattern matching
* - any string
? - jeden znak
[...] - wszystkie w
[a-cd-w]

`?(PATTERN-LIST)'
Matches zero or one occurrence of the given patterns.

`*(PATTERN-LIST)'
Matches zero or more occurrences of the given patterns.

`+(PATTERN-LIST)'
Matches one or more occurrences of the given patterns.

`@(PATTERN-LIST)'
Matches one of the given patterns.

`!(PATTERN-LIST)'
Matches anything except one of the given patterns.

3.6 redirections

`/dev/stdout'
File descriptor 1 is duplicated.

`/dev/stderr'
File descriptor 2 is duplicated.

There are two formats for redirecting standard output and standard
error:
&>WORD
and
>&WORD
Of the two forms, the first is preferred. This is semantically
equivalent to
>WORD 2>&1
3.6.5 Appending Standard Output and Standard Error
--------------------------------------------------

This construct allows both the standard output (file descriptor 1) and
the standard error output (file descriptor 2) to be appended to the
file whose name is the expansion of WORD.

The format for appending standard output and standard error is:
&>>WORD
This is semantically equivalent to
>>WORD 2>&1

3.6.6 Here Documents
--------------------

This type of redirection instructs the shell to read input from the
current source until a line containing only WORD (with no trailing
blanks) is seen. All of the lines read up to that point are then used
as the standard input for a command.

The format of here-documents is:
<<[-]WORD
HERE-DOCUMENT
DELIMITER

do 3.7