function function_name () {
commands
}
# 호출
function_name arg1 arg2 arg3 ...
$n
: n번째 인수
$#
: 함수의 인수 개수
$*
: 모든 인수 리스트
shift n
n 만큼 인수 리스트를 왼쪽으로 shift
#! /bin/bash
function new_hello_world() {
while : ;
do
arg_elem=$1
if [ "x${arg_elem}" = "x" ]; then
return 0
fi
echo "Hello world : $arg_elem"
shift 1
done
}
jsg@jsg-ubuntu:~$ . my_func.sh jsg@jsg-ubuntu:~$ new_hello_world Kim Lee Jung Park Hello world : Kim Hello world : Lee Hello world : Jung Hello world : Park
? () { echo "$*" | bc -l; }
jsg@jsg-ubuntu:~$ ? '34^3*2' 78608
#! /bin/bash
function sum_filesize() {
typeset -i sz_filetotal sz_file
sz_filetotal=0; sz_file=0; dest_extension=$1; shift 1
echo "Find files with extensions : $dest_extension";
echo "- dest dirs : $*"
while : ; do
if [ "x$1" = "x" ]; then
echo "+ Sum of filesize : $sz_filetotal"; return
fi
echo "+ Directory : $1"
for dfilename in $(find $1 -type f -name "*.$dest_extension"); do
if [ ${dfilename##*.} = $dest_extension ]; then
sz_file=$(stat -c "%s" $dfilename)
echo "+ Filename = $dfilename, size = $sz_file"
(( sz_filetotal+=sz_file ))
fi
done
shift 1
done
}
jsg@jsg-ubuntu:~$ sum_filesize sh project Find files with extensions : sh - dest dirs : project + Directory : project + Filename = project/final_01.sh, size = 643 + Filename = project/final_02.sh, size = 737 + Sum of filesize : 1380
#! /bin/bash
Hexcalc () {
HexInput=$*
if [ ${#HexInput} -gt 2 -a ${HexInput:0:2} == '0x' ]; then
HexInput=${HexInput:2}
fi
echo $((16#${HexInput}))
}
jsg@jsg-ubuntu:~$ Hexcalc A1 161