Linux ——Shell编程---以后补全

简介:Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。

在这里插入图片描述

1、查看Linux支持的shell
vi /etc/shells

/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
2、第一个shell脚本

创建一个test.sh文件

vim test.sh

编辑文件内容

#!/bin/bash
echo 'hello world'

修改文件可执行权限并执行

# 赋予执行权限
chmod +x ./test.sh

# 执行
./test.sh  # 不能写tesh.sh,写test.sh,linux 系统会去 PATH 里寻找有没有叫 test.sh 的
3、shell变量

注意:
1、变量名和等号之间不能有空格。
2、命名只能使用英文字母
3、中间不能有空格,可以用下划线代替
4、不能使用bash里的关键字

# 起变量
name='lisa'

# 取已定义变量名
echo $name

# 取未定义变量名
echo name

# 只读变量
readonly age='11'
echo $age  --- 11
age='12'  --- 报错

# 删除变量
unset name
# unset 不能删除只读变量
4、变量类型
# 类型一:局部变量
# 类型二:环境变量
# 类型三:shell变量
5、引号
# 单引号:单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;
# 双引号:双引号里可以有变量,双引号里可以出现转义字符。
6、字符串拼接
name='lisa'

# 使用变量${}拼接
who="hello my name is ${name}"
echo $who
# hello my name is lisa
7、获取字符串长度
str='12345'
echo ${#str}
# 输出5
8、提取子字符串
str='123456789'
echo ${str:2:4}
# 输出 3456
9、查找子字符串
str='this is a string test'
echo `expr index "$str" ig`  # 不是单引号

# 以上脚本中 ` 是反引号,而不是单引号 '
10、shell数组

注意:
1、bash支持一维数组(不支持多维数组),并且没有限定数组的大小。

2、在shell中,用括号来表示数组,数组元素用“空格”符号分割开。

# 定义数组
array_name=(1 2 3 4)

# 数组下标取值
echo ${array_name[array_index]}

# 获取数组中全部的值
echo ${array_name[@]}

# 获取数组长度
echo ${#array_name[@]}
11、shell传递参数

必须使用双引号,单引号无效。

#!/bin/bash

echo "Shell 传递参数实例!";
echo "执行的文件名:$0"; # 系统会自动传入
echo "第一个参数为:$1"; # 真实的第一个参数
echo "第二个参数为:$2";
echo "第三个参数为:$3";
# 修改文件执行权限
chmod +x test.sh

# 传入参数执行文件
./test.sh 第一个参数为1 第二个参数 第三个参数

# 执行结果
:<<'
 执行的文件名:./test.sh
 第一个参数为:第一个参数为1
 第二个参数为:第二个参数为2
 第三个参数为:第三个参数为3
'

在这里插入图片描述

12、shell基本运算符

原生的bash不支持简单的数学运算,但是可以通过其他命令来实现,如awk和expr,expr最常用。

算数运算符:+ - * / % = == !=
#! /bin/bash
var=`expr 2+2`
echo "两数之和为:$val"

注意:乘号前面必须加反斜杠()才能实现运算

关系运算符:-eq -ne -gt -lt -ge -le
# vim tesh.sh
#! /bin/bash
a=10
b=20
if [$a -eq $b]
then
	echo "$a -eq $b":"a等于b"
else
	echo "$a -eq $b":"a不等于b"
fi
chmod +x tesh.sh
./tesh.sh
布尔运算符:!(非) -o(或) -a(与)
# vim tesh.sh
#! /bin/bash
a=10
b=20
if [$a -lt $b -a $b=20]
then
	echo "a小于b切b=20"
else
	echo '[$a -lt $b -a $b=20]条件不成立'
fi
逻辑运算符:&&(and) ||(OR)
#!/bin/bash

a=10
b=20

if [[ $a -lt 100 && $b -gt 100 ]]
then
   echo "返回 true"
else
   echo "返回 false"
fi
字符串运算符:= != -Z -n $
#!/bin/bash

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a 等于 b"
else
   echo "$a = $b: a 不等于 b"
fi
13、shell的输出命令:echo 和 printf

echo:自动huanh
printf:不会自动换行

14、test命令

shell中的test命令用于检查某个条件是否成立,可以进行数值,字符和文件三个方面的测试

数值测试:

在这里插入图片描述

#! /bin/bash
num1=100
num2=100
if test $[num1] -eq $[num2]
then
    echo '两个数相等!'
else
    echo '两个数不相等!'
fi
字符测试:

在这里插入图片描述

#! /bin/bash
num1="ru1noob"
num2="runoob"
if test $num1 = $num2
then
    echo '两个字符串相等!'
else
    echo '两个字符串不相等!'
fi
文件测试:

在这里插入图片描述

cd /bin
if test -e ./bash
then
    echo '文件已存在!'
else
    echo '文件不存在!'
fi
15、流程控制
if
if condition
then
    command1 
    command2
    ...
    commandN 
fi
if else
if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi
if else-if else
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi
for循环
for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done
while循环
#!/bin/bash
int=1
while(( $int<=5 ))
do
    echo $int
    let "int++"
done
until循环

until 循环执行一系列命令直至条件为 true 时停止。

until 循环与 while 循环在处理方式上刚好相反。

#!/bin/bash

a=0

until [ ! $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done
case:多选择语句
#! /bin/bash
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
    1)  echo '你选择了 1'
    ;;
    2)  echo '你选择了 2'
    ;;
    3)  echo '你选择了 3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac
16、shell函数
>基本定义:
#!/bin/bash

demoFun(){
    echo "这是我的第一个 shell 函数!"
}
echo "-----函数开始执行-----"
demoFun
echo "-----函数执行完毕-----"
>函数参数:
#!/bin/bash

funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

注意:当n>=10时,需要使用${n}来获取参数。

发布了128 篇原创文章 · 获赞 0 · 访问量 2511

猜你喜欢

转载自blog.csdn.net/qq_41134008/article/details/105243238