目录
2.for((expression1:expression2:expression3))
运行环境
在线运行Bash,网址:在线运行Bash (bejson.com),版本(使用echo $BASH_VERSION打印结果):5.0.0(1)-release
一.hello world
代码:
#!/bin/bash
# bash script
echo "hello world"
运行结果:
1.#!/bin/bash
主要作用:指明命令解析器所在位置。如果代码中没有该行,会使用默认的shell解释该程序,就可能不是bash。
#!被称为shebang,用于指定解释程序;/bin/bash是命令解析器的路径,用于指定bash shell在操作系统中的位置。
命令解析器:接受用户命令,然后调用相应的应用程序或功能。linux 通过以下代码查看bash shell(bash 是shell 的一种,bash shell 指基于bash规则的命令解析器)所在位置。
which bash
2.#
#:用于单行注释,bash script指当前语言(简称bash),script 指脚本语言,
多行注释:以 : ' 开始 , ' 结束,注意 : 和 ' 之间的空格不能省略
eg:
#!/bin/bash
: ' bash script
bash script
bash script
bash script
'
echo "hello world"
运行结果与前一个完全一致
3.echo
echo 打印空格之后的内容后换行,注意 echo 和 " 中间存在空格,不能省略。
单引号和双引号的使用:单引号不能用于变量,只能用于纯文本;双引号两者皆可,当文本内容与变量名相同时,优先指代变量。
代码:
#!/bin/bash
#定义变量,string、= 和'test'三者之间不能有空格
string='test'
#输出变量
echo $string
# $表示后面是变量,如果变量不存在,从$到其后第一个空格或换行之前的字符全部不输出
echo "$string"
#输出''内内容
echo '$string'
运行结果:
二.选择
1.if-then
if [ condition ] ; #注意 [ 后、 ] 前的空格
then
......
fi #结束
eg. 代码:
#!/bin/bash
a=1
b=2
if [ $a -eq 1 ] && [ $b -eq 2 ]; # -eq 判断数字是否相等,== 判断字符串是否相等,注意空格
then
echo "a=$a,b=$b"
fi
echo "end"
执行结果:
2.if-else-then
if [ condition ];
then
...
else
...
fi
eg 代码:
#!/bin/bash
if [ "test1" == "test2" ];
then
echo "test1"
else
echo "test2"
fi
echo "end"
执行结果:
3.if-then- else if-then
# 先判判断是否符合条件1,符合执行commond1,然后跳到fi后,
# 不符合判断是否符合条件2,符合条件2执行commond2,然后跳到fi后
# 不符合条件2执行commond3,然后跳到fi后
if [ condition1 ];
then
... #command1
elif [ condition2 ];
then
... #command2
else
... #command3
fi
eg 代码:
#!/bin/bash
a=3
if [ $a -lt 1 ]; #若a小于1
then
echo "less than 1"
elif [ $a -lt 2 ] #若 a小于2
then
echo "less than 2"
elif [ $a -gt 2 ] #若a大于2
then
echo "greater than 2"
else
echo "eqauls 2"
fi
echo "end"
执行结果:
4.case
#判断变量是否满足value1,满足执行commond1,然后结束
# 不满足判断是否满足value2,满足执行commond2,然后结束
# 以上都不满足,执行commond3,然后结束
case $variable in
value1)
... #commond1
;; #表示commond1结束
value2)
... #commond2
;;
*) #不符合以上两种情况
... #commond3
;;
esac
eg 代码:
#!/bin/bash
read sex #读取一行赋给name
case $sex in
Male | male) # Male 或 male
echo "Male"
;; #语句块结束标志
Female | female)
echo "Female"
;;
*)
echo "wrong input"
;;
esac
echo "end"
执行结果:
三、循环
1.for-in
#当变量在list范围内时,执行commond
for variable in list
do
... #commond
done
eg1. 代码:
#以空格为界,分割字符串(产生数组?),多个连续空格分割时不产生空格,(分割结果作为变量数组?)
#!/bin/bash
test="this is a test "
i=1
for variable in $test # 此处若是 for variable in "$test" 则以换行为分割符分割字符串,结果只有 1 this is a test
do
echo "$i $variable"
i=$(($i+1)) #i=i+1
done
执行结果:
eg2. 代码:
#输出列表中的元素
#!/bin/bash
for i in {1..3..1} # {start..end..increment} 如果没有..increment,默认自增1
do
echo $i
done
执行结果:
2.for((expression1:expression2:expression3))
#定义变量,当变量符合条件时,先执行command1,然后执行command2,重复执行command1和command2直至不满足条件condition
for((define variable : condition :command2))
do
... #command1
done
eg 代码:
#!/bin/bash
for ((i=0;i<3;i++))
do
echo $i
done
执行结果:
3.while循环
#当满足条件expression时,重复执行command
while [expression];
do
... #command
done
eg 代码:
#!/bin/bash
i=0
while [ $i -lt 3 ];
do
echo $i
i=$(($i+1))
done
执行结果:
4.until循环
#执行command直到条件成立
until [ condition ];
do
... #commad
done
eg 代码:
#!/bin/bash
i=5
until [ $i -lt 3 ];
do
echo $i
i=$(($i-1)) #也可写成((i--))
done
echo "end"
执行结果: