출처 : http://invisiblepixels.org/lang/bash.html

 

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
String Tests
= equals
!= not equal to
< Sorts lower
> Sorts higher
-z zero characters long
-n 1 more characters long
string string is not null
Integer Tests
-eq equals
-ne not equal to
-lt less than <
-gt greater than >
-le less than or equal to
-ge greater than or equal to
Logic
&& logical AND
|| logical OR
-a boolean and
-o boolean or
File Tests
-e exists at all
-f is ordinary file
-d is directory
-r file is readable
-w file is writable
-x file is executable
-s non empty
File Comparisons
-nt is file newer (checks modification date)
-ot is file older (checks modification date)

Loops and Decisions
if case
if decision
then
    statements
elif decision
    then
        statements
else
    statements
fi
case string
in
regexp)
    statements
;;
regexp)
    statements
;;
*)
    statements
esac
for for var in list
do
    statements
done
while while decision
do
    statements
done
until until decision
do
    statements
done

Gathering Input read
getopts

Exit Codes Success: $? = 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
perm octal notes
r 4  
w 2  
x 1  
s-- 4--- SetUid.
-s- 2--- SetGid.
--t 1--- StickyBit.
Who function What
u
g
o
a
+ (add to existing)
- (remove from existing)
= (replace existing)
r
w
x
s
t

Functions
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
Posted by '김용환'
,