小六学习-脚本语言-Shell
hello world
echo "hello world"
运行shell
- 可执行程序
chmod +x ./helloworld.sh
- 解释语言
/bin/sh helloworld.sh
基础语法
变量
设置变量
test ="hello";
取出变量
$test
${test}
删除变量
unset test
字符串
设置字符串
str ="hello"
拼接字符串
str1 ="world"
str2 ="hello ${str1}"
字符串长度
str1 ="world"
${#str1}
数组
定义数组
array=(str1,str2,str3,str4)
数组赋值
array[0]="hello"
读取数组
${array[0]}
数组长度
${#array[*]}
输出
echo
echo "hello"
printf
printf("world")
> 定向输出
"hello world" > a.txt
>> 追加 定向输出
"hello world" >> a.txt
IF ELSE FOR WHILE
IF
a=10
b=20
if [ $a == $b ]
then
printf("hello")
fi
ELSE
if [ $a == $b ]
then
printf("hello")
elif
then
echo("world")
fi
FOR
for entry in 1 2 3 4 5
do
echo "The value is: $entry"
done
WHILE
index =1
while(( $index<=100 ))
do
echo $index
let "index++"
done
函数
shell_fun(){
echo "hello"
}
shell_fun(){
return "hello"
}
脚本参数传递
./hello_world hello world 1 45
$0 hello_world
$1 hello
$2 world
$3 1
$4 45
echo $0 ------> echo hello_world