Shell programming: Difference between revisions

From 탱이의 잡동사니
Jump to navigation Jump to search
No edit summary
Line 79: Line 79:
fi
fi
</source>
</source>
=== comparison ===
두 개의 값을 비교할 때는 다음을 참조하자.
'''String'''
<pre>
Str1 = Str2 Returns true if the strings are equal
Str1 != Str2 Returns true if the strings are not equal
-n Str1 Returns true if the string is not null
-z Str1 Returns true if the string is null
</pre>
'''Numeric'''
<pre>
expr1 -eq expr2 Returns true if the expressions are equal
expr1 -ne expr2 Returns true if the expressions are not equal
expr1 -gt expr2 Returns true if expr1 is greater than expr2
expr1 -ge expr2 Returns true if expr1 is greater than or equal to expr2
expr1 -lt expr2 Returns true if expr1 is less than expr2
expr1 -le expr2 Returns true if expr1 is less than or equal to expr2
! expr1 Negates the result of the expression
</pre>
'''File Conditionals'''
<pre>
-d file True if the file is a directory
-e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
-f file True if the provided string is a file
-g file True if the group id is set on a file
-r file True if the file is readable
-s file True if the file has a non-zero size
-u True if the user id is set on a file
-w True if the file is writable
-x True if the file is an executable
</pre>


== See also ==
== See also ==

Revision as of 13:01, 8 September 2015

Overview

리눅스 쉘 프로그래밍 정리

Special variables

쉘 프로그램에서는 미리 예약되어 있는 특수 변수들이 있다.

$0 	The filename of the current script.

$n 	These variables correspond to the arguments with which a script was invoked. 
Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on).

$# 	The number of arguments supplied to a script.

$* 	All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.

$@ 	All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.

$? 	The exit status of the last command executed.

$$ 	The process number of the current shell. For shell scripts, this is the process ID under which they are executing.

$! 	The process number of the last background command

$0 : 현재 실행 중인 스크립트의 filename 을 지칭한다.

$n : 스크립트 실행시, 함께 넘겨진 인자를 순서대로 나타낸다.

$# : 스크립트 실행시, 함께 넘겨진 인자의 갯수를 나타낸다.

$* : 스크립트 실행시, 함께 넘겨진 모든 인자를 나타낸다. 만약 인자가 두개였다면, $* 는 $1 $2 와 같다.

$@ : 스크립트 실행시, 함께 넘겨진 모든 인자를 나타낸다. 만약 인자가 두개였다면, $* 는 $1 $2 와 같다.

$? : 마지막으로 실행한 명령행의 결과값을 나타낸다.

$$ : 현재 실행중인 쉘의 Process ID 를 나타낸다.

$! : 마지막으로 실행한 background command 의 Process ID 를 나타낸다.

Statements

for

<source lang=bash>

  1. !/bin/sh

for var in A B C do

   echo "var is $var"

done </source>

while

while 은 조건이 만족하는 동안 루프를 반복한다. <source lang=bash>

  1. !/bin/sh

a=0 while [ $a -lt 10 ] do

   a=`expr $a + 1`
   echo $a

done </source>

if

if 는 지정된 조건을 충족할 때만 실행된다. <source lang=bash>

  1. !/bin/sh

if [ "$#" -gt 0 ] then

   echo "There's Beans"

fi

if [ "$1" = "cool" ] then

   echo "Cool Beans"

fi </source>

comparison

두 개의 값을 비교할 때는 다음을 참조하자.

String

Str1 = Str2 	Returns true if the strings are equal
Str1 != Str2 	Returns true if the strings are not equal
-n Str1 	Returns true if the string is not null
-z Str1 	Returns true if the string is null

Numeric

expr1 -eq expr2 	Returns true if the expressions are equal
expr1 -ne expr2 	Returns true if the expressions are not equal
expr1 -gt expr2 	Returns true if expr1 is greater than expr2
expr1 -ge expr2 	Returns true if expr1 is greater than or equal to expr2
expr1 -lt expr2 	Returns true if expr1 is less than expr2
expr1 -le expr2 	Returns true if expr1 is less than or equal to expr2
! expr1 	Negates the result of the expression

File Conditionals

-d file 	True if the file is a directory
-e file 	True if the file exists (note that this is not particularly portable, thus -f is generally used)
-f file 	True if the provided string is a file
-g file 	True if the group id is set on a file
-r file 	True if the file is readable
-s file 	True if the file has a non-zero size
-u 	True if the user id is set on a file
-w 	True if the file is writable
-x 	True if the file is an executable

See also

References

<references />