Вы находитесь на странице: 1из 7

Tips Madskull'a: bash

{ command1 & pid1=$! ;}; \


{ command2 & pid2=$! ;}; \
wait $pid1 $pid2

(SIGALRM)
TIME=10 # 10
trap 'exit' ALRM
( sleep $TIME && kill -ALRM "$$" ) &


func()
{
echo $1
echo "---"
for i in "$*"; do echo $i; done
echo "---"
for i in "$@"; do echo $i; done
echo "---"
for i in $*; do echo $i; done
}
func "aaa bbb" "ccc ddd"
# aaa bbb
# --# aaa bbb ccc ddd
# --# aaa bbb
# ccc ddd
# --# aaa
# bbb
# ccc
# ddd

array=("aaa bbb" "ccc ddd")


echo ${#array[@]} #
#2
for i in "${array[@]}"; do echo $i ; done
# aaa bbb
# ccc ddd
for i in "${array[*]}"; do echo $i ; done
# aaa bbb ccc ddd

for i in ${array[@]}; do echo $i ; done


# aaa
# bbb
# ccc
# ddd
for ((i=0; $i<${#array[@]}; i++)); do echo ${array[$i]}; done
#aaa bbb
#ccc ddd

trap: (updated)
trap 'echo ERR' ERR
# ,
trap 'echo EXIT' EXIT
# ,
# EXIT
trap 'echo SIGTERM; exit 1' SIGTERM
# kill -15
trap 'echo INT; exit 1' INT
# ^C kill -2
trap 'echo HUP; exit 1' HUP
# kill -1
#
trap '' INT # ^C
#
trapINT()
{
echo " !"
}
trap trapINT INT

bash-
# usage: progress <> <_>
PB_NOPERC=1
# ,
PB_NOITEM=1
# , _/
progress()
{
local width=50
local max=$1 curr=$2 pos=$width perc=100
if [ $curr -lt $max ]; then
pos=$((curr*width/max))
perc=$((curr*100/max))
fi
local str="\r[$(printf "%${pos}s" \
| sed 's/ /#/g')$(printf "%$((width-$pos))s" \

| sed 's/ /-/g')]"


[ -z "$PB_NOPERC" ] && str="$str $perc%"
[ -z "$PB_NOITEM" ] && str="$str ($curr/$max)"
echo -ne $str
[ $perc -eq 100 ] && echo
}


IFS=$'\n'
for str in `cat ini | sed 's/^ *//' | egrep -v '^$|^#'`; do
eval $(echo $str|sed 's/ *=/=/;s/= */=/')
done
echo $var
ini :
#
var = value



${#string}


${string:position}
# position
${string:position:length}
# position length
${string: -length}
# length


${string#substring}
#
${string##substring}
#
${string%substring}
#
${string%%substring}
#


${string/substring/replacement}
#
${string//substring/replacement}
#
${var/#Pattern/Replacement}

# var Pattern,
#
# (),
# Replacement.
#
${var/%Pattern/Replacement}
# var Pattern,
#
# (),
# Replacement.
#


: parameter "" .
${!ref} #
var='str'
ref='var'
echo ${!ref} # str
${parameter-default}, ${parameter:-default}
#
# -.
${parameter=default}, ${parameter:=default}
# ,
# -.
${parameter+alt_value}, ${parameter:+alt_value}
# , #
alt_value,
# -- null ("" ).
${parameter?err_msg}, ${parameter:?err_msg}
# parameter ,
# ,
# -- err_msg.
${!varprefix*}, ${!varprefix@}
# ,
# ,
# varprefix.

getopts
while getopts ":ab:c" Option
# .
# a, b, c -- ().
# : 'b' ,
#
# .
do
case $Option in
a)
# , 'a'.
b ) # , 'b',
# $OPTARG,
#

# .
...
c ) # , 'c'.
esac
done
shift $(($OPTIND - 1))

$# -
:
args=$#
last=${!args}

! -
var1="string"
var2="var1"
echo ${!var2} # string

$- , ( set).
$_ , .
$! PID , ,
$$ PID -.
$? .
$FUNCNAME
$LINENO .
$OLDPWD ("OLD-Print-Working-Directory")
$PIPESTATUS ().
$PIPESTATUS .
bash$ who | grep nobody | sort
bash$ echo ${PIPESTATUS[*]}

$PPID $PPID PID () .


$PWD ()

ECHO
BOLD='\e[1m'
UNDERLINE='\e[4m'
BLINK='\e[5m'
INVERSE='\e[7m'
BREAK='\e[m'
#Foreground colors:
BLACK='\e[0;30m'
RED='\e[0;31m'
GREEN='\e[0;32m'
BROWN='\e[0;33m'
BLUE='\e[0;34m'
MAGENTA='\e[0;35m'
CYAN='\e[0;36m'
GRAY='\e[0;37m'
DEF='\e[0;39m'
DGRAY='\e[1;30m'

LRED='\e[1;31m'
LGREEN='\e[1;32m'
YELLOW='\e[1;33m'
LBLUE='\e[1;34m'
LMAGENTA='\e[1;35m'
LCYAN='\e[1;36m'
WHITE='\e[1;37m'
# backgrounds colors
BLACK='\e[40m'
RED='\e[41m'
GREEN='\e[42m'
BROWN='\e[43m'
BLUE='\e[44m'
MAGENTA='\e[45m'
CYAN='\e[46m'
GRAY='\e[47m'
DEF='\e[49m'

- .
$OLDPWD.
~+ . $PWD.
:
: > file

:
echo 'str'\''ing' # str'ing


echo sed
\n ( )
\r
\t
\v
\b (backspace)
\a "" ()
\0xx ASCII- 0xx )


shopt -s expand_aliases

expand, unexpand
expand .
.
unexpand . ..
expand.
fold
, , , .

-s, ,
.
column
. , ,
, "", ,
.
colrm
. ()
stdout. ,
colrm 2 4 &gt;filename

2- 4- , filename.
readlink
, .
od
od (octal dump) ( )
, .
join
.