.sh
,名称就叫hello
echo 'hello world'
./hello.sh
而后回车name='tom'; echo $name echo ${name}
#
count=100; readonly count count=101 #count: readonly variable
count=100; unset count echo $count # 不报错,可是打印为空
字符串拼接前端
str1="hello" str2="world" str3="${str1} ${str2}" echo $str3 # hello world
获取字符串长度python
str="hello world" echo ${#str} # 输出11 空格也算
截取字符串linux
str="hello" echo ${str:1:3} # 数字指的是索引,输出ell
查找字符串git
str="hello" echo `expr index "${str}" h` # 输出 1 从1 计算index
定义数组web
arr=(1 2 3 4 5) # 注意 是空格区分 arr1=( # 能够换行 1 2 3 4 5 )
访问数组–索引访问shell
arr=(1 2 3 4 5 6) echo ${arr[1]} # 输出2
访问数组–所有数组
arr=(1 2 3 4 5 6) echo ${arr[*]} # 输出1 2 3 4 5 6 echo ${arr[@]} # 输出1 2 3 4 5 6
获取数组长度bash
arr=(1 2 3 4 5 6) echo ${#arr[@]} # 输出6 echo ${#arr[*]} # 输出6
算数运算符微信
1+2
要写成1 + 2
n=1 m=2 echo `expr ${n} + ${m}` # 输出3
a=1 b=2 if [ $a -eq $b ] then echo "$a -eq $b : a 等于 b" else echo "$a -eq $b: a 不等于 b" fi if [ $a -ne $b ] then echo "$a -ne $b: a 不等于 b" else echo "$a -ne $b : a 等于 b" fi if [ $a -gt $b ] then echo "$a -gt $b: a 大于 b" else echo "$a -gt $b: a 不大于 b" fi if [ $a -lt $b ] then echo "$a -lt $b: a 小于 b" else echo "$a -lt $b: a 不小于 b" fi if [ $a -ge $b ] then echo "$a -ge $b: a 大于或等于 b" else echo "$a -ge $b: a 小于 b" fi if [ $a -le $b ] then echo "$a -le $b: a 小于或等于 b" else echo "$a -le $b: a 大于 b" fi
a="hello" b="world" if [ $a = $b ] then echo "$a = $b : a 等于 b" else echo "$a = $b: a 不等于 b" fi if [ $a != $b ] then echo "$a != $b : a 不等于 b" else echo "$a != $b: a 等于 b" fi if [ -z $a ] then echo "-z $a : 字符串长度为 0" else echo "-z $a : 字符串长度不为 0" fi if [ -n "$a" ] then echo "-n $a : 字符串长度不为 0" else echo "-n $a : 字符串长度为 0" fi if [ $a ] then echo "$a : 字符串不为空" else echo "$a : 字符串为空" fi
echo "hello" > test.txt # 在当前目录下的test.txt 写入hello
function iteratorFolder(){ for file in `ls $1` do folder=$1"/"$file if [ -d $folder ] then iteratorFolder $folder else echo $folder fi done } folder="/etc" iteratorFolder $folder