#!/bin/bash
your_name="黑胡椒"
echo $your_name
your_name="低调点"
echo "${your_name}"
unset your_name
echo ${your_name}
site="www.site.com"
echo "${site}"
readonly site
site="www.666.com"
echo site
str1='test'
str2="demo"
echo $str1 $str2
echo ${#str1}
echo ${str1:1:3}
arr=('a' '1' '啊')
echo "${arr[@]}"
echo "${arr[*]}"
echo "${#arr[@]}"
for item in "${arr[@]}"; doecho "${item}"
done
echo "${arr[1]}"
:<<&#96;!&#96;
多行注释
&#96;!&#96;
:<<&#96;&#96;
多行注释
&#96;&#96;
if [ 2 -gt 1 ]; thenecho &#39;2 > 1&#39;
elif [ 1 -gt 0 ]; thenecho &#39;1 > 0&#39;
elseecho &#39;default&#39;
fi
&#96;expr &#43; - * / % &#61;&#61; !&#61;&#96;
a&#61;1
b&#61;2
echo &#96;expr 2 &#43; 2&#96;
echo $[a&#43;b]
echo $((a&#43;b)) &#96;expr express&#96;和$[express]
echo &#39;&#61;&#61;&#61;&#61;&#61;&#39;
expr $a / $b&#96; ]
thenecho &#39;能够整除&#39;
fi
if [ &#96;expr $a \* $b&#96; ]; thenecho &#96;expr $a \* $b&#96;
fi
if [ 2 -gt 1 -o 0 -lt 1 ]; thenecho &#39;ok&#39;
fi
if [ &#96;expr $a &#61;&#61; 1&#96; -a &#96;expr $a % $b&#96; -ge 0 ]
thenecho &#39;okk&#39;
fi
a&#61;1
b&#61;2
if [[ ! ${a} -ne 1 && ${b} -gt 1 ]]
thenecho &#39;取反&#39;
fi
str&#61;&#39;&#39;
if [ -z "${str}" ]
thenecho &#39;为空&#39;
fi
str&#61;&#39;test&#39;
if [ -n "${str}" ]
thenecho &#39;不为空&#39;
fi
:<<&#96;!&#96;-b file 块设备文件-c file 字符设备文件-d file 目录-f file 普通-p file 管道-r file 可读-w file 可写-x file 可执行-s file 文件是否不为空&#xff0c;大小为0-e file 文件或目录是否存在-S file 是否socket-L file 是否存在并且是一个符号链接
&#96;!&#96;
file&#61;&#39;./base.sh&#39;
if [ -w $file ]
thenecho &#39;可读&#39;
fi
if [ -r $file ]
then echo &#39;可写&#39;
fi
if [ -x $file ]
then echo &#39;可执行&#39;
fi
if [ -L $file ]
thenecho &#39;存在并且是链接&#39;
fi
if [ -f $file ]
thenecho &#39;是文件&#39;
fi
dir&#61;&#39;../demo-shell&#39;
if [ -d $dir ]
then
echo &#39;是目录&#39;
fi
if [ ! -p $dir ]
then
echo &#39;不是管道&#39;
fi
if [ -s $file ]
thenecho &#39;文件不为空&#39;
fi
if [ -e $file ]
thenecho &#39;文件存在&#39;
fi
if [ -e $file -a -s $file -a -f $file ]
thenecho &#39;文件存在并且是内容不为空的普通文件&#39;
fi
echo 111 > ./test.txt
echo &#96;date&#96;
for((i&#61;0;i<3;i&#43;&#43;));
doecho $i
done
for item in {a..c}; doecho "${item}"
done
a&#61;3
while(($a >&#61; 1))
doecho $alet &#39;a--&#39;if [ $a -eq 0 ]then echo &#39;boom&#39;fi
done
:<<&#96;&#96;
while :
doecho "输入1到4之间的数字:"read aNumecho "你输入的数字为&#xff1a;${aNum}"case $aNum in1) echo &#39;1&#39;;;2) echo &#39;2&#39;;;3) echo &#39;3&#39;;;4) echo &#39;4&#39;;;6|7|8) echo &#39;6/7/8&#39;;;9) echo &#39;你猜中了&#39;break ;;*) echo &#39;其他数字&#39;;;esac
done
&#96;&#96;
for((i&#61;0;i<5;i&#43;&#43;)); doif [ &#96;expr $i % 2&#96; -eq 0 ]thenecho "${i}"elsebreakfi
done
echo &#39;hello&#39; > test.txt
echo &#39;world&#39; >> test.txt
>&#96;为输出到...。
&#96;then&#96;别忘了。
&#96;do... done&#96;代替&#96;{}&#96;&#96;,比如while() do...done, for() do...done
&#96;;;&#96;替代&#96;break&#96;&#xff0c;&#96;)&#96;替代&#96;:&#96;
&#96;[[xx && xx]]&#96; &#96;[xx -a xx -o xx]&#96; &#96;[xx] && [xx]&#96;
&#96;ls /etc&#96; &#xff1a; 反引号 &#xff08;所有的unix系统都支持&#xff09;
fun.sh文件
#!/bin/bash
function add () {echo "请输入两个值来进行数字相加..."echo "输入第一个数字&#xff1a;"read aecho "输入第二个数字&#xff1a;"read breturn $(($a &#43; $b))
}
echo "$?拿不到了" function fnParam() {echo "第1个参数$1"echo "第2个参数$2"echo "第10个参数 $10 " echo "第10个参数${10}"echo "第11个参数${11}"echo "共有参数个数为&#xff1a;$#"echo "所有参数字符串输出&#xff1a;$*"echo "当前进程ID $$"echo "后台运行的最后一个进程ID $!"a&#61;$&#64;echo "所有参数: ${a}"echo "退出命令的状态$?"
}
fnParam 0 1 2 3 4 5 6 7 8 22 99
b&#61;&#39;外部变量&#39;
function fnVal () {local b&#61;&#39;本地变量&#39;echo "${b}"
}
echo "\$b&#61;${b}"
fnVal