Shell programming: Difference between revisions

From 탱이의 잡동사니
Jump to navigation Jump to search
No edit summary
 
Line 128: Line 128:


'''Access_command <<END_DELIMITER''' 와 같이 시작되며, 이후에 '''END_DELIMITER''' 문장이 나오기 전까지 중간에 있는 모든 command 들을 Local 에서 실행하는 것이 아닌, Remote 로 전송하게 된다.
'''Access_command <<END_DELIMITER''' 와 같이 시작되며, 이후에 '''END_DELIMITER''' 문장이 나오기 전까지 중간에 있는 모든 command 들을 Local 에서 실행하는 것이 아닌, Remote 로 전송하게 된다.
<source lang=bash>
<pre>
COMMAND <<InputComesFromHERE
COMMAND <<InputComesFromHERE
command 1
command 1
Line 134: Line 134:
...
...
InputComesFromHERE
InputComesFromHERE
</source>
</pre>


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

Latest revision as of 15:45, 15 October 2018

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>

for in range <source lang=bash> for var in $(seq 1 10) 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

Advanced

Here Documents

단순히 명령어를 실행하는 것이 아니라, 특정 세션에 접속한 후, 명령어를 전송해야 하는 경우들이 있다(ssh, ftp, sftp, .. ). 이런 경우, Here Document 문법을 사용하면 된다. Redirection 과 사용법이 비슷하다.

Access_command <<END_DELIMITER 와 같이 시작되며, 이후에 END_DELIMITER 문장이 나오기 전까지 중간에 있는 모든 command 들을 Local 에서 실행하는 것이 아닌, Remote 로 전송하게 된다.

COMMAND <<InputComesFromHERE
command 1
command 2
...
InputComesFromHERE

See also

References

<references />