1. Bash substitusions
man bash
search for 'Parameter Expansion'
$ a='ABCfilename123123'
${#parameter} - parameter length
$ echo ${#a}
24
Prefix substitusion
$ echo ${a#ABC}
filename123123
## does not work always
Suffix substitusion
$ echo ${a%123}
ABCfilename123
%% does not work always
Pattern Substitusion
$ echo ${a/123/DFG}
ABCfilename123DFG
$ echo ${a//123/DFG}
ABCfilenameDFGDFG
If pattern starts with # it must match at the beginning.
If pattern starts with % it must match at the end.
Upper ^ / Lower , case conversions
${var^}
${var^^}
${var,}
${var,,}
2. Magic variables:
$# - Number of arguments supplied to the script
$$ - PID of the currently running programm
$? - Error report code of the last executed script/command
3. Working with the shell
3.1. forground jobs
3.2. background jobs
bg
fg
jobs
4. Codding style
4.1. empty var comparisons
VAR=
if [ -z $VAR ]; then vs. if [ -z "$VAR" ]; then
4.2. bracketing
if ( cmd ); then
vs.
if ( cmd );
then
for i in *; do
vs.
for i in *;
do
4.3. Script width
if [ ! -z "$var" ]; then
for i in $var; do
do_stuff ...
done
else
for i in *; do
do_stuff ...
done
fi
vs.
tst="$var"
if [ -z "$var" ]; then
tst='*'
fi
for i in $var; do
do_stuff ...
done
3. commands
cat
cut
head
tail
sort
uniq
wc
hackman@gamelon:~/lsa$ head -n-2 file1
1 adasdasda
2 klwrtwfgs
3 gwfehrtgwerf
4 .gwegrewg
5 warwerfdsa
hackman@gamelon:~/lsa$ head -n+2 file1
1 adasdasda
2 klwrtwfgs
hackman@gamelon:~/lsa$ tail -n+2 file1
2 klwrtwfgs
3 gwfehrtgwerf
4 .gwegrewg
5 warwerfdsa
6 okmqtfsd
7 gwqwezeee
hackman@gamelon:~/lsa$ tail -n-2 file1
6 okmqtfsd
7 gwqwezeee
man bash
search for 'Parameter Expansion'
$ a='ABCfilename123123'
${#parameter} - parameter length
$ echo ${#a}
24
Prefix substitusion
$ echo ${a#ABC}
filename123123
## does not work always
Suffix substitusion
$ echo ${a%123}
ABCfilename123
%% does not work always
Pattern Substitusion
$ echo ${a/123/DFG}
ABCfilename123DFG
$ echo ${a//123/DFG}
ABCfilenameDFGDFG
If pattern starts with # it must match at the beginning.
If pattern starts with % it must match at the end.
Upper ^ / Lower , case conversions
${var^}
${var^^}
${var,}
${var,,}
2. Magic variables:
$# - Number of arguments supplied to the script
$$ - PID of the currently running programm
$? - Error report code of the last executed script/command
3. Working with the shell
3.1. forground jobs
3.2. background jobs
bg
fg
jobs
4. Codding style
4.1. empty var comparisons
VAR=
if [ -z $VAR ]; then vs. if [ -z "$VAR" ]; then
4.2. bracketing
if ( cmd ); then
vs.
if ( cmd );
then
for i in *; do
vs.
for i in *;
do
4.3. Script width
if [ ! -z "$var" ]; then
for i in $var; do
do_stuff ...
done
else
for i in *; do
do_stuff ...
done
fi
vs.
tst="$var"
if [ -z "$var" ]; then
tst='*'
fi
for i in $var; do
do_stuff ...
done
3. commands
cat
cut
head
tail
sort
uniq
wc
hackman@gamelon:~/lsa$ head -n-2 file1
1 adasdasda
2 klwrtwfgs
3 gwfehrtgwerf
4 .gwegrewg
5 warwerfdsa
hackman@gamelon:~/lsa$ head -n+2 file1
1 adasdasda
2 klwrtwfgs
hackman@gamelon:~/lsa$ tail -n+2 file1
2 klwrtwfgs
3 gwfehrtgwerf
4 .gwegrewg
5 warwerfdsa
6 okmqtfsd
7 gwqwezeee
hackman@gamelon:~/lsa$ tail -n-2 file1
6 okmqtfsd
7 gwqwezeee
Последна модификация: Tuesday, 13 November 2012, 12:11 AM