Let's take a look at the return value of the two functions to get the way:
#!/bin/bash
#函数返回值获取测试.
function fun_test ()
{
#在函数内部是不能有其他的任何一条打印语句.
Echo
}
val=$ (fun_test)
echo $val
Another one:
#!/bin/bash
#函数返回值获取测试.
function fun_test ()
{return
}
fun_test
val=$?
Echo $val
#!/bin/bash
#在当前目录下面创建多个不存在的目录.
function Create_dir ()
{
mkdir./$1 >/dev/null
} while
[$#-gt 0];d o
a= ' ls | grep ' $ '
if [' $a ' = ' $ '];then
echo "has exit!!!"
Exit 0
Else
create_dir the
fi
shift 1
done
#/bin/bash
#递归求解1 + ...
sum=0
function fun_test ()
{
If [$1-gt 0];then set-x let
sum+=$1
set +x
Fun_test $ (($1-1))
else
echo $sum
fi
}
val=$ (fun_test)
echo $val
#!/bin/bash #打印指定目录中的所有目录文件. function Is_dir () {local dir=$1 if [-D ${dir}];then return 0 else return 1 fi} function L
S_dir () {local dir=$1 arr= (' ls ${dir} ') #创建数组.
CD $dir #跳转目录.
for (i=0;i<${#arr [@]};i++)) {#遍历数组, it's important here. If [D-${arr[i]}];then echo "This is a dir:" ${arr[i]} else echo "This is ' t a dir:" ${arr[i
} fi} cd-#回到主进程目录. } proc=$ (basename $) proc= ' basename $ ' function usage () {printf "%s%s\n" "Usage:" "/${proc}dir" #如果没有传进入
parameter, the error message is returned to the user. } If [$#-ne 1];then usage exit 1 fi ls_dir $
#!/bin/bash #打印一个动态的进度条, did not realize the color change. Arr= ("|") "/"-"\ \") function Pro () {local i=0 str= ' while [$i-le] doing let index=i%4 printf ' [%s] [%d%%] [%c]\r "" $str "$i" ${arr[index]} "sleep 0.2 let i++ str=${str}# do} Pro