참고 : http://wiki.redjini.com/linux/script
사용법
1 2 3 | function name(){ shell command } |
예제
1 2 3 4 5 | function my_function(){ echo "my function" } my_function |
지역변수 (Local variables)
1 2 3 4 5 6 7 8 9 10 11 12 13 | value=1234; function my_function(){ local value=5678 echo "function: value= ${value}" } echo "before : value= ${value}" # call my_function my_function echo "after : value= ${value}" |
실행결과
before : value= 1234
function: value= 5678
after : value= 1234
function: value= 5678
after : value= 1234
인자 : parameters
1 2 3 4 5 6 7 | function my_function(){ local value=$1 echo "my_function= ${value}" } # call my_function my_function 5678 |
결과 전달받기
1 2 3 4 5 6 7 8 9 10 | #!/bin/sh function my_function(){ local value=$1 echo "my_function= ${value}" } # call my_function result=$(my_function 5678) echo "my_function [ ${result} ]" |
1 2 3 4 5 6 7 8 9 10 | function my_function(){ echo "argv: $1 $2" return 10 } # call my_function my_function "myname" "acepro" result=$? echo "my_function [ ${result} ]" |
1 2 3 4 5 6 7 8 9 10 | function my_function(){ echo "argv: $1 $2" eval "$3='result'" } # call my_function result= "" my_function "myname" "acepro" result echo "my_function [ ${result} ]" |
"프로그래밍 / Linux & Shell Script" 분류의 다른 글
[shell script] bash read command (0) | 2014/02/11 |
[linux] shell command 로그 남기기 (0) | 2014/01/27 |
[linux] Linux Console에서 BEEP 사운드 끄기 (0) | 2014/01/27 |
[shell] netstat (0) | 2014/01/22 |
[shell script] 배열(Array) 사용하기 (0) | 2014/01/10 |
[linux] 메모리 (0) | 2013/12/17 |
[shell script] Here Documents (0) | 2013/11/26 |
[shell script] directory exists check (0) | 2013/11/20 |
[shell script] 실행 경로 구하기 (0) | 2013/10/24 |
[shell] yum 장애 조치 (0) | 2009/04/21 |