문자열 길이
- ${#string}
- expr length $string
문자열 바꾸기
stringZ=abcABC123ABCabc
echo ${stringZ/abc/xyz} # xyzABC123ABCabc
# Replaces first match of 'abc' with 'xyz'.
echo ${stringZ//abc/xyz} # xyzABC123ABCxyz
# Replaces all matches of 'abc' with # 'xyz'.
echo ---------------
echo "$stringZ" # abcABC123ABCabc
echo ---------------
# The string itself is not altered!
# Can the match and replacement strings be parameterized?
match=abc
repl=000
echo ${stringZ/$match/$repl} # 000ABC123ABCabc
# ^ ^ ^^^
echo ${stringZ//$match/$repl} # 000ABC123ABC000
# Yes! ^ ^ ^^^ ^^^
echo
# What happens if no $replacement string is supplied?
echo ${stringZ/abc} # ABC123ABCabc
echo ${stringZ//abc} # ABC123ABC
# A simple deletion takes place.
[출처 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Bash/AdvancedBashScripting]/Chapter10]
=================================================================================
# 일련변호 만들기1
echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
echo {0..20}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#일련번호 만들기2
seq 1 10
# read file.
File=/etc/fstab
{
read line1
read line2
} < $File
echo "First line in $File is : "
echo "$line1"
echo "Second line in $File is : "
echo "$line2"
exit 0
# exit status variable
$?
#process ID variable
$$
# command group
()
# block of code
{}
# copies "file22.txt" to "file22.backup"
cp file22.{txt,backup}
# place for text
ls . | xargs -i -t cp ./{} $1
# test
[ ]
#test
[[ ]]
# array element
[]
# range of characters
[]
# integer expansion.
[...]
#integer expansion
(( ))
#redirection
command &> filename : redirects both the stdout and th stderr of command to filename
command >&2 : redirects stdout of command to stderr
[ i ] <> filename : opens file filename for reading and writing. and assigns file descriptor i to it. if filename does not exist, it is created.
<< : redirection used in a hear document.
<<< : redirection used in a hear string.
<,> : ascii comparison.
# 문자열 비교하기.
s1=a
s2=a
if [[ "$s1" < "$s2" ]] ; then # [] 으로 테스트를 하면 에러이다. [[ ]] 으로 테스트를 해야 함.
echo s2 is bigger than s1
else
echo s2 is not bigger than s1
fi
# 1에서 100까지 루프 순환하기.
for i in {0..100}
do
echo $i
done
#redirection from / to stdin or stdout
echo "whatever" | cat -
# note that $variable is actually a simplified form of ${variable}.
# number of argument passed.
$#
$*
$@ : same as $* , but each parameter is a quoted string. that is, the parameters are passed on intact, without interpretation or expansion.
$RANDOM : generate random integer.
# 실행결과를 변수에 담기.
a=$(ls)
# 실행메시지 출력하지 않게 하기.
if cmp a b &> /dev/null #suppress output.
# 파일 테스트.
-e : file exists.
-a : file exists
-f : file is a regular file (not a directory or device file)
-s : file is not zero size
-d : file is a directory
-b : file is a block device
-c : file is a character device
-p : file is a pipe
-h : file is a symbolic link
-L : file is a symbolic link
-S : file is a socket
-t : file is associated with a terminal device
-r : file has read permission
-w : file has write permission
-x : file has execute permission
-g : set-group-id flag set on file or directory
-u : set-user-id flag set on file
-k : sticky bit set
f1 -nt f2 : file f1 is newer than f2
f1 -ot f2 : file f1 is older than f2
f1 -ef f2 : files f1 and f2 are hard links to the same file.
# 비교 연산자.(숫자)
-eq : is equal to
if [ "$a" -eq "$b" ]
-ne : is not equal to
if [ "$a" -ne "$b" ]
-gt : is greater than
if [ "$a" -gt "$b" ]
-ge : is greater than or equal to
if [ "$a" -ge "$b" ]
-lt : is less than
if [ "$a" -lt "$b" ]
-le : is less than or equal to
if [ "$a" -le "$b" ]
< : is less than
(( "$a" < "$b" ))
<= : is less than or equal to
(( "$a" <= "$b" ))
> : is greater than
(( "$a" > "$b" ))
# 문자열 비교
= : is equal to
if [ "$a" = "$b" ]
== : is equal to
if [ "$a" == "$b" ]
!= : is not equal to
if [ "$a" != "$b" ]
< : is less than
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
> : is greater than. in ASCII alphabetical order.
if [[ "$a" > "$b" ]]
if [ "$a" \> "$b" ]
-z : string is null, that is , has zero length
-n : string is not null.
# similar to the let command, the (( ... )) construct permits arithmetic expansion and evaluation.
# c-style manipulation of variable.
(( a = 23 ))
(( a++))
((++a))
((a--))
((--a))
#internal field separator
$IFS
$BASH_VERSION
$CDPATH
$DIRSTACK
$EDITOR
$EUID
$FUNCNAME
$FLOBIGNORE
$GROUPS
$HOME
$HOSTNAME
$HOSTTYPE
$LINENO
$MACHTYPE
$OLDPWD
$OSTYPE
$PATH
$PIPESTATUS
$PPID
$PS1 : this is the main prompt.seen at the command-line
$PS2 : the secondary prompt. seen when additional input is expected. is displays as ">"
$PS3 : the tertiary prompt, displayed in a select loop
$PS4 :
$PWD
$REPLY
$SECONDS : the number of seconds the script has been running
$SHLVL : shell level, how deeply bash is nested.
# 문자열 조작.
#문자열 길이.
${#string}
expr length $string
expr "$string":'.*'
# length of matching substring at beginning of string
expr match "$string" "$substring"
expr "$string":'$substring'
stringZ=abcABC123ABCabc
# |------|
# 12345678
echo `expr match "$stringZ" 'abc[A-Z]*.2'` # 8
echo `expr "$stringZ" : 'abc[A-Z]*.2'` # 8
#index
expr index $string $substring
stringZ=abcABC123ABCabc
# 123456
echo `expr index "stringZ" C12` # 6
#substring extractin
${string:position}
${string:position:length}
expr substr $string $position $length
#substring removal
${string#substring} : deletes shortes match of $substring from front of $string
${string##substring} : deletes longest match of $substring from front of $string.
${string%substring} : deletes shortes match of $substring from back of $string
${string%%substring} : deletes longest match of $substring from back of $string
# variable length / substring removal
${#var} : string length
# variable expansion / substring replacement
${var:pos}
${var:pos:len}
${var/Pattern/Replacement}
${var//Pattern/Replacement} : global replacement. all matches of pattern, within var replaced with replacement.
${var/#Pattern/Replacement} : if prefix of var matches Pattern, then substitute replacement for Pattern
${var/%Pattern/Replacement} : if suffix of var matches pattern, then substitute replacement for pattern.
${!varprefix*}, ${!varprefix@} : matches names of all previously declared variables beginning with varprefix.
# loops and branches
# for
for arg in [ list ] ; do
done
# while
while [ condition ] ; do
done
#until
until [ condition-is-true ]
do
command(s)...
done
[출처] abs - advanced bash scripting. 정리.|작성자 대운
'Academy I > Tech Academy' 카테고리의 다른 글
[REGEX]Regular expression (0) | 2015.02.02 |
---|---|
Awk (0) | 2015.02.02 |
Regular expression (0) | 2015.02.02 |
assert와 매크로 함수를 이용한 디버깅 정보 얻기 (0) | 2015.01.22 |
Advanced Bash-Scripting - 특수문자 (0) | 2015.01.22 |
Advanced Bash-Scripting - 예제 (0) | 2015.01.22 |
crontab command (0) | 2015.01.15 |
sudo 로 root 권한을 얻지 못하는 문제 (0) | 2015.01.14 |