참고 : http://wiki.redjini.com/linux/script

사용법

function name(){
  shell command
}

예제

function my_function(){
   echo "my function"
}

my_function

지역변수 (Local variables)

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

인자 : parameters

function my_function(){
   local value=$1
   echo "my_function= ${value}"
}

# call my_function
my_function 5678

결과 전달받기

#!/bin/sh
function my_function(){
   local value=$1
   echo "my_function= ${value}"
}

# call my_function
result=$(my_function 5678)

echo "my_function [ ${result} ]"

function my_function(){
    echo "argv: $1 $2"
    return 10
}

# call my_function
my_function "myname" "acepro"
result=$?

echo "my_function [ ${result} ]"

function my_function(){
    echo "argv: $1 $2"
    eval "$3='result'"
}

# call my_function
result=""
my_function "myname" "acepro" result

echo "my_function [ ${result} ]"

2014/01/09 18:23 2014/01/09 18:23
태그 : , , ,
글 걸기 주소 : 이 글에는 트랙백을 보낼 수 없습니다