출처 : http://invisiblepixels.org/lang/bash.html
Contents:
History
!$ | just the last word of the previous command |
!! | previous command (same as up arrow) |
!string | most recent command starting with string |
!?string | most recent entry containing string |
^str1^str2 | rerun last command replacing str1 with str2 |
Built-in Variables
$# | no of args passed to the prog |
$* | reference all args passed as $1, $2, $3 |
$@ | same as $* but references as individual string "$1" "$2" "$3" |
$0 | is the name of the programme |
$$ | process id of programme being executed |
$! | process id of last prog sent to background for execution |
$? | exit status of last command not in bg |
$- | current shell options in effect. see SET |
IFS | Internal Field Separator. What separates words (doesn't have to be a space) |
Logical Comparisons
|
|
|
|
|
Loops and Decisions
|
|
Gathering Input read
getopts
getopts
Exit Codes Success: $? = 0
Failure: $? != 0
Failure: $? != 0
Redirection
** 1 = STDOUT 2 = STDERR ** | |
command 2>filename | redirects STDERROR |
command >&2 | redirect *to* STDERROR. ie echo hello >&2 sends hello to stderror |
command >foo 2>&1 | redirects STDOUT to foo and STDERROR to STDOUT (which in this case is foo) |
>&- | causes STDOUT to go nowhere (cyberspace) |
<&- | causes input to come from nowhere. ie there is no input |
Variable Manipulation
${var} | curlies delimit var name |
${var:-string} | returns the string if var is empty |
${var:=string} | returns the string if var empty AND var is assigned the value of string |
${#var} | returns the length of the contents of var. or if #@ then returns no of elements in $@ |
${var:?string} | returns string if var empty. also prints the name of var |
${var:+string} | returns string if variable is set. otherwise returns blank |
File Permissions
|
|
Functions
to terminate a function use the return n command, not exit. exit will kill the whole programme.
functionname() { local var1 statements }can pass parameters to it when calling it. ie functionname a b c
to terminate a function use the return n command, not exit. exit will kill the whole programme.
Arrays
Declare:
- (Note: in that the first two examples, because you have declared the indices, you can list the elements in any random order)
days[0]=Sunday days[1]=Monday days[2]=Tuesday
months=([01]=Jan [02]=Feb [03]=Mar)
- With no indices declared, the elements are automatically assigned consecutive indices starting from [0].
If you assign one of the elements an index, for example if you give autumn the index of [8]=autumn,
all the entries following autumn will be given consecutive indices, starting from that point.seasons=(summer autumn winter spring)
Accessing:
for counter in 1 2 3 4 do echo "${seasons[$counter]}" done
- echo ${seasons[@]} or echo $seasons[*]
The @ and * work the same way they do as $@ and $* (see top of page)
Length / Number of Elements:
- To find out the number of non-null elements in an array: length=${#array[@]}
If you've set up an array like this: seasons=(summer autumn [6]=winter spring),
even though winter has an index of [6] and spring [7], ${#array[@]} will only return a value of 4. - To find out the length of an individual element: length=${#array[5]}
'unix and linux' 카테고리의 다른 글
bash에서 스트링 비교하기 (0) | 2007.10.09 |
---|---|
Bash에서 숫자 비교 (0) | 2007.10.09 |
zagent 좀비 프로세스. (0) | 2007.10.08 |
포트 충돌 (zagent) (0) | 2007.10.08 |
리눅스에서 최대 파일 크기는? (0) | 2007.10.05 |