变量
# 默认是全局变量,作用域名是当前进程,不管在何处定义
a
# 本地变量,作用域是当前大括号内{}
local a
# 环境变量,作用域是当前进程及其子进程
export a
IO
echo ""
echo -e ""
printf "%d" 0x10
字符串处理
# 原生字符串
'bc$a'
# 格式化字符串
"bc$a"
# 切片
b=${a:0:-1}
# 正则
[[ "123" =~ .*"1" ]]
# 数学
a=1
b=$((a++))
递归遍历目录
经过测试,1000次ls调用耗时2s,10000次系统调用耗时23秒,所以,别用bash处理数据。
#!/bin/bash
recursion_dirs(){
if [ "$#" == "0" ];then echo "[Error]"; return;fi
local opt="$1"
if [ ! -d "$opt" ];then
echo "$opt"; return ;fi
files="$(ls $opt)"
for file in $files;do
file="$opt/$file"
recursion_dirs $file
done
}
print_msg(){
oldIFS=$IFS
IFS=$'\n'
path="$HOME/.wine/drive_c"
recursion_dirs "$path"
IFS=$oldIFS
}
main(){
print_msg
}
main @