카테고리 없음
(linux/bash) 쉘 스크립트 function 함수 관련
미친토끼
2025. 2. 1. 15:53
#!/bin/bash
#
round2() {
printf "%.${2}f" "${1}"
}
PI=3.14159
round2 ${PI} 0
echo
round_PI=$(round2 $PI 3)
echo $round_PI
floor() {
printf "%.0f" ${1}
}
ceil() {
echo ""
}
round() {
echo ""
}
echo "----------"
num=3.6
floor $num
echo "---------------"
function test {
echo "test will return 12"
return 12
}
test
echo "return value of test: $?"
function test() {
echo "hello"
}
echo "$(test) world!"
num1=1
num2=2
function test {
num1=3
local num2=4
echo "num1 in test: $num1"
echo "num2 in test: $num2"
}
echo "num1: $num1"
echo "num2: $num2"
test
echo "num1: $num1"
echo "num2: $num2"
function add_all() {
local result=0
for num in $@
do
let result+=num
done
echo $result
}
result=`(add_all 1 2 3 4 5)`
echo "result: $result"
my_function() {
echo "some result"
return 55
}
my_function
echo $?
my_function() {
func_result="some result"
}
my_function
echo $func_result
my_function() {
local func_result="iiisome result"
echo $func_result
}
echo $(my_function)
#echo $func_resul
echo "----------------"
simple_function() {
for ((i=0;i<5;++i)) do
echo " $i ";
done
echo
}
simple_function
function simple_inputs() {
echo "This is the first argument [$1]"
echo "This is the second arguement [$2]"
echo "Calling function with $# arguments"
}
simple_inputs one 'two three'
sum=0
function simple_outputs() {
let sum=$1+$2
}
simple_outputs 1 2
echo "Sum is $sum"
function simple_outputs() {
let sum=$1+$2
echo $sum
}
sum=$(simple_outputs 1 2)
echo "Sum is $sum"
function ref_outputs() {
declare -n sum_ref=$3
sum_ref=$(($1+$2))
}
ref_outputs 11 2 sum
echo "sum is $sum"
var="baeldung"
function var_scope() {
local var="lorem"
echo "variable inside function var_scope: [$var]"
}
var_scope
echo "Variable outside function var_scope: [$var]"
var="baeldung"
function var_scope2() {
echo "Variable inside function var_scope2: [$var]"
}
function var_scope() {
local var="lorem"
echo "Variable inside function var_scope: [$var]"
var_scope2
}
var_scope
sum=0
#function simple_subshell() {
# sum=$3
# let sum=$1+$2
#}
sum=0
function simple_subshell() {
declare -n sum_ref=$3
let sum_ref=$1+$2
}
simple_subshell 4 5 sum
echo "Sum is $sum"
simple_subshell 3 4 sum
echo "Sum is $sum"
# 외부 파일에서 읽어와서 처리하기
filename="infile"
outfile="outfile"
function redirection_in() {
line=0
while read input;
do
len=`echo $input | wc -w`
#echo "input=$input"
#echo "len=$len"
for (( i=0; i<len; i++))
do
echo -n ${input[$i]}
done
echo
let line++
done
echo "I read $line lines."
} < $filename
redirection_in
function redirection_out() {
declare -a output=("baeldung" "lorem" "ipsum")
for element in ${output[@]}
do
echo $element
done
} > $outfile
redirection_out
function redirection_in_ps() {
read
while read -a input;
do
echo "${input[2]} ${input[8]}"
done
} < <(ls -ll /)
redirection_in_ps
echo "=========="
function redirection_out_ps() {
declare -a output=("baeldung" "lorem" "ipsum" "caracg")
for element in "${output[@]}"
do
echo "$element"
done
} > >(grep "g")
redirection_out_ps
echo "----------"
function fibo() {
num=$1
if [ $num -eq 0 ] || [ $num -eq 1 ]; then
echo $num
else
first=`fibo $((num-1))`
second=`fibo $((num-2))`
echo $(( $first + $second ))
fi
}
#FUNCNEST=10
echo `fibo 5`
echo `fibo 7`
echo `fibo 6`
echo `fibo 12`
function read_and_verify {
read -p "Please enter value for '$1': " tmp1
read -p "Please repeat the value to verify: " tmp2
if [ $tmp1 != $tmp2 ]; then
echo "Values unmatched. Please try again."; return 2
else
declare -n ref="$1" # $1을 참조변수로 선언
ref=$tmp # $1에 tmp1 값을 넣음.
fi
}
number="1234"
read_and_verify number
echo $number
#declare var
#declare -i int
# var="1+1"
# int="1+1"
# echo $var
# echo $int