Cílem cvičení je vyzkoušení praktických důsledků zpracování (interpretace) příkazové řádky.
\ ' "
␣ ⇥ ↵ | & ; ( ) < >
~printf␣'|%s|\n'␣␣␣a␣␣b␣c␣↵
int main (int argc, char * argv[]) { ...
\ ' "
~printf '%s je %s\n' \\ 'zpetne lomitko' \' apostrof \" uvozovka
~printf '%s\n' "text 'v' uvozovkach"
~printf '%s\n' "text \"v\" uvozovkach"
~printf '%s\n' 'text "v" apostrofech'
~printf '%s\n' 'text \'v\'apostrofech'↵
'
~printf '.%s.\n' text\
na\
nekolik\
radek
~printf '=%s=\n' $'a\tb\nc'
~printf '=%s=\n' $'\'\"'
~printf '%s\n' $'\U1f607' $'\U1f608'
~printf '%s\n' '<CTRL-V><ESC>cx<CTRL-V><TAB>y'
~cd /
/#touch file
/cd
~touch file
~printf '%s\n' a #b c
~printf '%s\n' a#b c
~printf '%s\n' 'a #b c' "#"
~printf '%s\n' a \
#b \
↵ ; | &
|
; & || &&
~set -x
~cd /tmp; ls | wc -c; cd
+ cd /tmp
+ wc -c
+ ls
1234
+ cd
~cd /tmp & ls | wc -c & cd
[1] 5654
+ cd /tmp
[2] 5656
+ wc -c
+ ls
+ cd
[1]- Done cd /tmp
~65432
~set +x
~cd ..;cd ..;cd ..;cd ..;pwd|wc -c;cd
2
~cd foo || mkdir foo || echo "Adresar 'foo' nelze vytvorit"
bash: cd: foo: No such file or directory
~ls -d foo
foo
~rmdir foo && echo "Adresar 'foo' smazan"
Adresar 'foo' smazan
~rmdir foo && echo "Adresar 'foo' smazan"
rmdir: failed to remove 'foo': No such file or directory
~touch foo
~cd foo || mkdir foo || echo "Adresar 'foo' nelze vytvorit"
bash: cd: foo: Not a directory
mkdir: cannot create directory 'foo': File exists
Adresar 'foo' nelze vytvorit
~rm foo
{,} ~ $ ${} $() $(())
"$..."
, výsledek podléhá dalšímu zpracování
~mkdir files; cd files
~/filestouch {a..z}.{txt,jpg} {001..020}; ls
~/filesmv ~/files/x.{jpg,jpeg}; ls
~/filesprintf '%s = %s\n' '~' ~ '~+' ~+ '~-' ~- '~root' ~root
~/filesA=~+:~-; B=~+,~-; C='~'; declare -p A B C
~/filesmin=1; max=10; printf '%s\n' {$min..$max}
~/filest='Text na nekolik slov'
~/filesprintf '%s.\n' "$t"
~/filesprintf '%s.\n' "$ticek"
~/filesprintf '%s.\n' "${t}icek"
~/filesdate +%T
~/filescas=$(date +%T)
~/filesdeclare -p cas
~/filesu=$(getent passwd "$USER"); declare -p u
~/filesu=$(getent passwd); declare -p u
~/filesx=2 y=3
~/filesprintf '%03d\n' $((x+y*x))
~/filesz=$((x+y)); declare -p x y z
~/files((z=x+y)); declare -p x y z
~/files((z++)); declare -p x y z
IFS
␣ ⇥ ↵
~/filest='Text na nekolik slov'
~/filesprintf '%s.\n' $t
~/filesprintf '%s.\n' "$t"
~/filesset -- 'a b' c 'd e f'
~/filesprintf '.%s.\n' $#
~/filesprintf '.%s.\n' $*
~/filesprintf '.%s.\n' $@
~/filesprintf '.%s.\n' "$*"
~/filesprintf '.%s.\n' "$@"
~/filesset | grep ^IFS=
~/filesoldIFS=$IFS
~/filesa='␣␣␣a:b::c␣␣␣'
~/filesprintf '%s|\n' $a
~/filesIFS=:
~/filesprintf '%s|\n' $a
~/filesIFS=
~/filesprintf '%s|\n' $a
~/filesunset IFS
~/filesprintf '%s|\n' $a
~/filesIFS=$oldIFS
.
se standardně neuvažují? * []
~/filesls ~/filesls [ps0]*
~/filesls 01? ~/filesls [p-s]*
~/filesls ??? ~/filesls [^p-s]*
~/filesls 0?0 ~/filesls *[02468]
~/filesls * ~/filesls *[^02468]
~/filesls *.txt ~/filescd
~/filesls *j* ~ls -ld */
~/filesls a* ~ls -ld */*
~/filesPATTERN=*
~/filesls $PATTERN
~/filesls "$PATTERN"
~/filesls f*o?o[0-9]
~/filestouch .a ..b ...c
~/filesls -ld .*
~/filesls -ld .[^.]*
~/filesls -ld .[^.]* ..?*
~/filesset -f
~/filesls -ld *
~/filesls -ld .*
~/filesls -ld .[^.]*
~/filesls -ld .[^.]* ..?*
~/filesset +f
~ls > list; wc -l list
~ls >> list; wc -l list
~date >list; wc -l list
~echo 1; echo 2 > list; echo 3
~echo 1; echo 2> list; echo 3
~ls / foo bar | wc -l
~ls / foo bar >list | wc -l
~ls / foo bar >list 2>/dev/null | wc -l
~rm list
~ls / foo bar >/dev/null 2>/dev/null | wc -l
~ls -l list
~ls / foo bar >/dev/null 2>/dev/null | wc -l >/dev/null
~wc -l /etc/passwd
~cat /etc/passwd | wc -l
~wc -l </etc/passwd
~wc <<KONEC
jedna dva
tri
KONEC
~printf '%s\n' "$PATH" | wc -c
~wc -c <<<"$PATH"
~N=/dev/null
~ls / foo bar
~ls / foo bar >"$N"
~ls / foo bar >"$N" 2>"$N"
~N='> /dev/null'
~ls -d
~ls -d $N
~ls -d "$N"
~ls -l list > list
~less list
~ls > l?st
~ls > files/00?
~> list
~ls -l list
~date >x
~less x
~x=soubor
~date >"$x"
~less "$x"
~x=$( date )
~declare -p x
~unset x
~date >x
~x=$( <x )
~declare -p x
fray1:~declare -p PATH
declare -x PATH="/opt/local/bin:/opt/java/jdk/bin:/usr/bin:..."
fray1:~oldIFS=$IFS IFS=:
fray1:~find $PATH -name grep 2>/dev/null
/usr/bin/grep
fray1:~IFS=$oldIFS
fray1:~find /usr -name grep 2>/dev/null
/usr/gnu/bin/grep
/usr/bin/grep
/usr/xpg4/bin/grep
fray1:~type grep
grep is /usr/bin/grep
fray1:~grep --version
grep: illegal option -- version
Usage: grep [-c|-l|-q] -bhinsvw pattern file . . .
fray1:~/usr/xpg4/bin/grep --version
/usr/xpg4/bin/grep: illegal option -- version
Usage: grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]... [-f pattern_file]... [file...]
...
fray1:~/usr/gnu/bin/grep --version
/usr/gnu/bin/grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
...
~printf '%s\n' '#!/bin/bash' date 'sleep 3' pwd >skript
~bash skript
~bash <skript
~printf '%s\n' '#!/bin/bash' date 'sleep 3' pwd | bash
~chmod +x skript
~./skript
~declare -p PAGER
~PAGER='wc -l' man ls
~declare -p PAGER
~export PAGER='wc -l'; man ls
~declare -p PAGER
~A=5; bash -c 'declare -p A'; declare -p A
~A=7 bash -c 'declare -p A'; declare -p A
~alias
...
~alias ls
alias ls='ls --color=auto'
~unalias ls
~alias ll='ls -l'
~ll
~'ll'; \ll
bash: ll: command not found
bash: ll: command not found
~alias lc='ls | wc -l'
~lc
123
fray:~cd /bin
fray:/binpwd
fray:/binman pwd
fray:/binpwd --help
fray:/binhelp pwd
fray:/bin/bin/pwd --help
fray:/bin/bin/pwd
Pro shell je podstatné, jak vypadá řádka před náhradami,
nikoli po nich. To umožňuje předvídatelnost chování.
~cmd='date' ~cmd='echo $((1+2))'
~$cmd ~$cmd
... $((1+2))
~cmd='date >d' ~cmd='x=5'
~$cmd ~$cmd
date: invalid date '>d' bash: x=5: command not found
~cmd='date | wc -l' ~alias ll='ls -l'
~$cmd ~cmd='ll'
date: invalid option -- 'l' ~$cmd
Try 'date --help' for more information. bash: ll: command not found
~type date
date is /bin/date
~type ls
ls is aliased to `ls --color=auto'
~type quote
quote is a function
quote ()
{
local quoted=${1//\'/\'\\\'\'};
printf "'%s'" "$quoted"
}
~type cd
cd is a shell builtin
~type for
for is a shell keyword
Odstranění komentářů (Comments)
#
uvozuje komentář