티스토리 뷰

IoT 과정

Functions and Function Libraries

gaelim 2017. 7. 7. 15:59
반응형

Function

$ function hello
> {
>   print Hello World
> }
$ hello
Hello World

$ hello ()
> {
>   print hello world
> }


Function 을 잘 못 작성했다고 생각하면, cmd 입력창에서 cmd 모드로 들어가서 j, k를 이용 명령어를 탐색한다. 그렇다면 다음과 같이 찾을 수 있을 것이다.

$ function hello^J{^J  print Hello World^J}

^J 는 new line character 이다. 이 상태에서 v를 누르면 (계속해서 모드는 유지) vi 편집기로 편리하게 수정할 수 있다. 그다음 :wq 를 이용하여 잘 저장하고 수행할 수 있다.


Function Return Value

기본적으로 function과 variable은 성질이 비슷하기 때문에, shell memory에 저장된다. 따라서 터미널을 종료하게 된다면, 다 사라진다. return value를 주기 위해 exit [#] 을 사용할 경우, 현재 shell 에서 exit 하기 때문에 원치 않는 종료가 발생할 수 있다. return cmd를 수행하면 된다.

hello ()
{
  print hello world
  return 1
}


Script 안에서의 Function 의 사용 예는 /sbin/init.d 디렉토리 안의 파일들을 참조하면 좋다

Script 안에서 작성된 function들은 항상 위에 존재하도록 작성해야한다. 그래야 스크립트 메인 바디가 자신의 메모리에서 참조할 수 있다.
Script 안에서 작성된 function들은 script가 수행되기 위해 뜬 자식 쉘 메모리에 저장되서 사용된다. (앞에서 이미 한 번 말 했다.)

이미 작성된 function 들을 확인하는법은 typeset -f

Command Processing Order (Review from Earlier Module)

1 Command is an alias
2 Command is a function
3 Command is an in-build command
4 Command is found in the $PATH directory list
5 AN error message is displayed if the command name is no found



반응형

'IoT 과정' 카테고리의 다른 글

Enterprise Linux System - Adnministration 시작  (0) 2017.07.17
Managing Input and Output  (0) 2017.07.07
Array Variable  (0) 2017.07.07
Program Loops  (0) 2017.07.07
Shell Patterns  (0) 2017.07.07