参数扩展
${param
${param
${param%pattern}
${param%%pattern}
${param/pattern/string}
${param//pattern/string}
${param:3:2}
${param:-word}
${param:=word}
${
${param^^}
${param,,}
获取文件名
filename=${f%.*}
获取文件后缀
fileext=${f##*.}
if
if [[ "$1" != "" ]]; then
echo $1
else
echo "none"
fi
for
for f in $(ls)
do
echo $f
done
while
loop=true
# CTRL+C
trap 'onCtrlC' SIGINT
function onCtrlC () {
loop=false
echo 'Exit'
}
while $loop
do
echo "Hello world"
sleep 1
done
case
# 等待输入
read -p " [yes|no]: " opcode
case $opcode in
"yes")
echo "yes"
;;
"no")
echo "no"
;;
*)
echo "none"
;;
esac
字符串
# 判断字符串为空
if [[ -z "$str" ]]; then
echo string none
fi
# 判断字符串非空
if [[ -n "$str" ]]; then
echo string not none
fi
# 字符串比较
if [[ "$str" == "hello" ]]; then
echo string equal
fi
# 字符串匹配
if [[ "$str" =~ "hello" ]]; then
echo string pattern
fi
文件
# 文件存在
if [[ -e $file ]]; then
echo $file exist
fi
# 文件存在且是常规文件
if [[ -f $file ]]; then
echo $file exist and is a regular file
fi
# 文件可执行
if [[ -x $file ]]; then
echo $file executable
fi
# 文件夹存在
if [[ -d $file ]]; then
echo $file directory
fi
评论区