#!/usr/bin/env bash
name="pan" //定义变量
echo "aa{$name}aa" //使用变量
for p in a,b,c,d;do
echo $p
done //for循环
readonly pass="password" //定义变量为只读
greeting="hello my name is ${name}"
echo ${greeting}
echo ${#greeting} //获取字符串的长度
echo ${greeting:2:6} #get length of string //截取子符串
echo `expr index "${greeting}" e` //获取子字符在字符串中的位置
array_name=('2' '1' '3' '4') //定义数组
echo ${array_name[0]} //获取数组元素
echo ${array_name[@]} //获取数组所有元素
echo "the lenth of the array is "${#array_name[@]} //获取数组长度
echo $$ //输出该进程的进程号
echo $? //输出shell执行最终结果 0表示正确
sum=`expr 2 + 2` 算术表达式 +
echo ${sum}
if [ 10 != 10 ] 关系运算
then
echo true
else
echo false
fi
aa=10
bb="b"
if [ $aa == $bb ];then echo "aa==bb";fi
if [ $aa == $bb ];then echo "aa==bb"
elif [ ${aa} != ${bb} ];then echo "aa!=bb"
fi
printf "%d %s\n" 10 "pan"
for ppp in 2 3 //for循环
do
echo ${ppp}
done
i=0
while (($i < 3 )) //while循环
do
echo $i
let i++;
done
f1(){
for ((; ;)) //死循环
do
echo "aa"
done
}
//函数
demoFun(){
echo "这是我的第一个 shell 函数!"
}
name="pan" //定义变量
echo "aa{$name}aa" //使用变量
for p in a,b,c,d;do
echo $p
done //for循环
readonly pass="password" //定义变量为只读
greeting="hello my name is ${name}"
echo ${greeting}
echo ${#greeting} //获取字符串的长度
echo ${greeting:2:6} #get length of string //截取子符串
echo `expr index "${greeting}" e` //获取子字符在字符串中的位置
array_name=('2' '1' '3' '4') //定义数组
echo ${array_name[0]} //获取数组元素
echo ${array_name[@]} //获取数组所有元素
echo "the lenth of the array is "${#array_name[@]} //获取数组长度
echo $$ //输出该进程的进程号
echo $? //输出shell执行最终结果 0表示正确
sum=`expr 2 + 2` 算术表达式 +
echo ${sum}
if [ 10 != 10 ] 关系运算
then
echo true
else
echo false
fi
aa=10
bb="b"
if [ $aa == $bb ];then echo "aa==bb";fi
if [ $aa == $bb ];then echo "aa==bb"
elif [ ${aa} != ${bb} ];then echo "aa!=bb"
fi
printf "%d %s\n" 10 "pan"
for ppp in 2 3 //for循环
do
echo ${ppp}
done
i=0
while (($i < 3 )) //while循环
do
echo $i
let i++;
done
f1(){
for ((; ;)) //死循环
do
echo "aa"
done
}
//函数
demoFun(){
echo "这是我的第一个 shell 函数!"
}