shell
1.shell命令使用户用来操作linux硬件的语言,命令通过解析器解析操作linux内核,进而操作linux硬件,类似于windows的dos命令
2.shell解析器
[root@VM-0-2-centos /]# cat /etc/shells
/bin/sh 最早使用的
/bin/bash 默认的,交互性强,推荐使用
/usr/bin/sh
/usr/bin/bash
/bin/tcsh csh的扩展版
/bin/csh C语言风格的shell
3.查看使用的解析器类型
[root@VM-0-2-centos /]# echo $SHELL
/bin/bash
echo用于输出的,$SHELL属于共享环境变量,所有shell程序都可以共享读取
4.shell脚本文件是一个文本文件,使用.sh作为后缀名,首行需要写解析器类型
#!/bin/bash
5.注释格式
(1)单行注释
# 注释内容
(2)多行注释
:<<!
注释内容
案例 输出hello,world!
[root@VM-0-2-centos sun]# touch helloworld.sh
[root@VM-0-2-centos sun]# vim helloworld.sh
[root@VM-0-2-centos sun]# sh helloworld.sh
hello,world!
[root@VM-0-2-centos ~]# sh /home/sun/helloworld.sh
hello,world!
[root@VM-0-2-centos ~]# bash /home/sun/helloworld.sh
hello,world!
[root@VM-0-2-centos sun]# ./helloworld.sh
-bash: ./helloworld.sh: Permission denied
[root@VM-0-2-centos sun]# chmod 777 helloworld.sh
[root@VM-0-2-centos sun]# ./helloworld.sh
hello,world!
sh和bash执行脚本文件方式是直接使用shell解析器运行脚本文件,不需要执行权限
仅路径方式是执行脚本文件自己,需要可执行权限
shell脚本多命令处理
案例:在已知目录下,执行batch.sh脚本,实现在指定目录下创建一个one.txt,在one.txt文件中增加内容“HelloShell”
[root@VM-0-2-centos ~]# cd /home
[root@VM-0-2-centos home]# ls
sun
[root@VM-0-2-centos home]# cd sun
[root@VM-0-2-centos sun]# touch batch.sh
[root@VM-0-2-centos sun]# vim batch.sh
[root@VM-0-2-centos sun]# cat batch.sh
#!/bin/bash
touch /home/sun/one.txt
echo "Hello Shell!" >> /home/sun/one.txt
[root@VM-0-2-centos sun]# sh batch.sh
[root@VM-0-2-centos sun]# ls
batch.sh helloworld.sh one.txt
[root@VM-0-2-centos sun]# cat one.txt
Hello Shell!
[root@VM-0-2-centos sun]#
shell变量环境变量
1.系统的环境变量
系统提供的共享变量,是linux系统加载Shell的配置文件中定义的变量共享给所有的Shell程序使用。
系统级环境变量:Shell环境加载全局配置文件中定义的变量,供所有用户使用
用户级环境变量:Shell环境加载个人配置文件中定义的变量,供当前用户使用
查看系统环境变量
[root@VM-0-2-centos sun]# env
查看所有的系统环境变量包括函数
[root@VM-0-2-centos sun]# set
查看最近执行的命令
[root@VM-0-2-centos sun]# cat /root/.bash_history
查看系统字符码表
[root@VM-0-2-centos sun]# echo $LANG
Shell变量:自定义变量
1.自定义局部变量
介绍
就是定义在一个脚本文件中的变量,只能在这个脚本中使用的变量
定义语法:(等号两边不能有空格)
var_name=value
定义:
[root@VM-0-2-centos ~]# name=sun
[root@VM-0-2-centos ~]# age=21
查询:
[root@VM-0-2-centos ~]# echo $name
sun
[root@VM-0-2-centos ~]# echo $age
21
[root@VM-0-2-centos ~]# echo ${name}
sun
[root@VM-0-2-centos ~]# echo my name is ${name} 123
my name is sun 123
删除:
unset var_name
[root@VM-0-2-centos ~]# unset age
2.自定义常量
介绍
就是变量设置值后不可以修改的变量叫常量,也叫做只读变量
语法:
readonly var_name
[root@VM-0-2-centos ~]# echo $sex
123
[root@VM-0-2-centos ~]# readonly sex
[root@VM-0-2-centos ~]# echo $sex
123
[root@VM-0-2-centos ~]# sex=male
-bash: sex: readonly variable
3.自定义全局变量
介绍
就是在当前脚本文件中定义了全局变量,这个全局变量可以在当前Shell 环境与子Shell环境中都可以使用
语法:export var_name1 var_name2
父子shell环境介绍
例:两个shell脚本A.sh和B.sh
如果在A.sh中执行了B.sh,那么A.sh就是父Shell环境,B.sh就是子Shell环境
案例:创建两个脚本文件demo1.sh和demo2.sh
编辑demo1.sh,定义全局变量var4,执行demo2.sh脚本文件,输出var4全局变量
[root@VM-0-2-centos sun]# touch demo1.sh
[root@VM-0-2-centos sun]# touch demo2.sh
[root@VM-0-2-centos sun]# vim demo1.sh
[root@VM-0-2-centos sun]# vim demo1.sh
[root@VM-0-2-centos sun]# cat demo1.sh
#!/bin/bash
VAR4="sun"
sh demo2.sh
[root@VM-0-2-centos sun]# vim demo2.sh
[root@VM-0-2-centos sun]# cat demo2.sh
#!/bin/bash
echo "demo3文件中输出变量VAR4=${VAR4}"
[root@VM-0-2-centos sun]# sh demo1.sh
demo3文件中输出变量VAR4=
[root@VM-0-2-centos sun]# cat demo1.sh
#!/bin/bash
VAR4="sun"
export VAR4
sh demo2.sh
[root@VM-0-2-centos sun]# sh demo1.sh
demo3文件中输出变量VAR4=sun
Shell变量:特殊变量
含义
用于接收脚本文件执行时传入的参数
$0用来获取当前脚本文件的名称的
$1~
9
代
表
获
取
第
一
输
入
参
数
到
第
九
输
入
参
数
第
十
参
数
以
上
:
9代表获取第一输入参数到第九输入参数 第十参数以上:
9代表获取第一输入参数到第九输入参数第十参数以上:{数字} ,${#}获取输入参数
语法:
sh 脚本文件 输入参数1 输入参数2 …
案例
创建脚本文件demo3.sh并在脚本文件内部执行打印脚本文件的名字,第一个输入参数,第二个输入参数;
[root@VM-0-2-centos sun]# touch demo3.sh
[root@VM-0-2-centos sun]# vim demo3.sh
[root@VM-0-2-centos sun]# cat demo3.sh
#!/bin/bash
echo "$0"
echo "$1"
echo "$2"
echo "${10}"
echo "${#}"
[root@VM-0-2-centos sun]# sh demo3.sh 1 2 3 4 5 6 7 8 9 0
demo3.sh
1
2
0
10
特殊变量
$* 和 $@
在demo4.sh中循环打印输入的所有输入参数,体会 $* 和 $@的区别
[root@VM-0-2-centos sun]# vim demo4.sh
[root@VM-0-2-centos sun]# cat demo4.sh
#!/bin/bash
echo '使用$*直接输出':$*
echo '使用$@直接输出':$@
echo '使用$*循环输出所有参数'
for item in "$*"
do
echo $item
done
echo '使用$@循环输出所有参数'
for item in "$@"
do
echo $item
done
[root@VM-0-2-centos sun]# sh demo4.sh 1 2 3 4 5 6 7 8 9
使用$*直接输出:1 2 3 4 5 6 7 8 9
使用$@直接输出:1 2 3 4 5 6 7 8 9
使用$*循环输出所有参数
1 2 3 4 5 6 7 8 9
使用$@循环输出所有参数
1
2
3
4
5
6
7
8
9
在vim中怎么复制?
在你要复制的区域第一行esc模式下,按下你要复制几行,然后yy,最后光标移到你要复制的目的地按下p。例如我现在要复制5行我就5yy + p
特殊变量
$? 用于获取上一个Shell命令的退出状态码
每一个shell命令都有一个执行状态码,一般0代表执行成功,非0代表执行失败
$$
获取当前shell环境进程的ID号
[root@VM-0-2-centos sun]# echo $$
24791
全局配置文件/etc/profile应用场景
案例
/etc/profile定义存储自定义系统级环境变量数据
vim /etc/profile
到达文件底端: G
顶端:gg
配置环境变量(建议大写)
export VAR1=VAR1
这样之后还要重载环境变量配置文件profile
[root@VM-0-2-centos etc]# vim profile
[root@VM-0-2-centos etc]# echo $VAR1
[root@VM-0-2-centos etc]# vim profile
[root@VM-0-2-centos etc]# source profile
[root@VM-0-2-centos etc]# echo $VAR1
VAR1
Shell环境变量深入:加载流程原理
Shell工作介绍环境
用户进入linux系统就会初始化Shell环境,这个环境会加载全局配置文件或个人配置文件,每个文件都有自己的Shell环境
Shell工作环境分类
交互式Shell
与用户进行交互,效果就是用户输入一个命令,Shell立刻做出反应
非交互式
不需要用户参与,就会执行多个命令,例如脚本,直接执行
*Shell登录环境
需要用户名密码登录进入的Shell环境
加载过程
echo $0判断是否是登录环境,bash代表非登陆环境,-bash代表登录环境。bash命令切换为非登陆环境。
[root@VM-0-2-centos ~]# echo $0
-bash
[root@VM-0-2-centos ~]# bash
[root@VM-0-2-centos ~]# echo $0
bash
Shell字符串变量
格式
1.单引号无法解析,不建议使用
2.推荐使用双引号
3.获取长度${#字符串名字}
[root@VM-0-2-centos ~]# k1='v1'
[root@VM-0-2-centos ~]# k2="v2"
[root@VM-0-2-centos ~]# k3=v3
[root@VM-0-2-centos ~]# echo '${k1} hello world!'
${k1} hello world!
[root@VM-0-2-centos ~]# echo "${k2} abc"
v2 abc
[root@VM-0-2-centos ~]# echo $var1
${k1} hello world!
[root@VM-0-2-centos ~]# echo ${#var1}
18
字符串拼接
1.无符号拼接
[root@VM-0-2-centos ~]# var1=abc
[root@VM-0-2-centos ~]# var2="hello world"
[root@VM-0-2-centos ~]# var3=${var1}${var2}
[root@VM-0-2-centos ~]# echo $var3
abchello world
2.双引号拼接
[root@VM-0-2-centos ~]# var3=“${var1}${var2}”
[root@VM-0-2-centos ~]# echo $var3
abchello world
3.混合拼接
[root@VM-0-2-centos ~]# var3="${var1}'&'${var2}"
[root@VM-0-2-centos ~]# echo $var3
abc&hello world
字符串截取
var1从第0个开始,截取长度为2的字符串
[root@VM-0-2-centos ~]# var1=welcomitheima
[root@VM-0-2-centos ~]# echo $var1
welcomitheima
[root@VM-0-2-centos ~]# echo ${var1:0:2}
we
var1从0开始一直截取到最后
[root@VM-0-2-centos ~]# echo ${var1:5}
mitheima
var1从右侧第五个开始,向右截取2个
[root@VM-0-2-centos ~]# echo ${var1:0-5:2}
he
从左侧第1次出现截取到最后
[root@VM-0-2-centos ~]# echo ${var1#*e}
lcomitheima
从左侧最后1次出现截取到最后
[root@VM-0-2-centos ~]# echo ${var1##*e}
ima
右侧是%,%%
Shell索引数组
注意:只有一维数组
定义
[root@VM-0-2-centos ~]# nums=(1 2 3 4 5 6)
[root@VM-0-2-centos ~]# nums=(1 2 3 "sadsad")
[root@VM-0-2-centos ~]# arr[6]=100
[root@VM-0-2-centos ~]# arr2=([0]=1 [3]="sadlasj" [10]=12)
获取
[root@VM-0-2-centos ~]# echo ${nums[0]}
1
[root@VM-0-2-centos ~]# item=${nums[2]}
[root@VM-0-2-centos ~]# echo $item
3
[root@VM-0-2-centos ~]# echo ${nums[*]}
1 2 3 sadsad
[root@VM-0-2-centos ~]# echo ${nums[@]}
1 2 3 sadsad
[root@VM-0-2-centos ~]# echo ${#nums[@]}
4
[root@VM-0-2-centos ~]# echo ${#nums[*]}
4
[root@VM-0-2-centos ~]# echo ${#nums[0]}
1
[root@VM-0-2-centos ~]# echo ${#nums[3]}
6
数组拼接
[root@VM-0-2-centos ~]# arr3=(${nums[@]} ${arr2[@]})
[root@VM-0-2-centos ~]# echo ${arr3[*]}
1 2 3 sadsad 1 sadlasj 12
删除数组
[root@VM-0-2-centos ~]# unset arr3
[root@VM-0-2-centos ~]# echo ${arr3[*]}
[root@VM-0-2-centos ~]# echo ${nums[*]}
1 2 3 sadsad
[root@VM-0-2-centos ~]# unset nums[1]
[root@VM-0-2-centos ~]# echo ${nums[*]}
1 3 sadsad
Shell内置命令
内置命令就是系统自带的,不是用脚本文件执行的,外部命令就是用脚本执行的命令,因为每次执行脚本都会新开一个进程,那么就会出现上下文的切换,就会消耗CPU的资源,效率会很低。
[root@VM-0-2-centos ~]# type cd
cd is a shell builtin
[root@VM-0-2-centos ~]# type ifconfig
ifconfig is /usr/sbin/ifconfig
给ps -aux设置别名
[root@VM-0-2-centos ~]# alias psList="ps -aux"
[root@VM-0-2-centos ~]# psList
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 43572 3952 ? Ss Dec13 0:20 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S Dec13 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< Dec13 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S Dec13 0:13 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S Dec13 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Dec13 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R Dec13 0:47 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< Dec13 0:00 [lru-add-drain]
root 11 0.0 0.0 0 0 ? S Dec13 0:01 [watchdog/0]
root 13 0.0 0.0 0 0 ? S Dec13 0:00 [kdevtmpfs]
root 14 0.0 0.0 0 0 ? S< Dec13 0:00 [netns]
root 15 0.0 0.0 0 0 ? S Dec13 0:00 [khungtaskd]
[root@VM-0-2-centos ~]# unalias psList
[root@VM-0-2-centos ~]# psList
bash: psList: command not found
[root@VM-0-2-centos ~]#
输出换行
echo -n 输出内容
-e解析转义字符
echo -e “hello \nworld”
[root@VM-0-2-centos ~]# echo -e "hello \nworld"
hello
world
read命令读取控制台
[root@VM-0-2-centos sun]# vim read1.sh
[root@VM-0-2-centos sun]# cat read1.sh
#!/bin/bash
read -p "请输入姓名,年龄,爱好:" name age hobby
echo "姓名:${name}"
echo "年龄:${age}"
echo "爱好:${hobby}"
[root@VM-0-2-centos sun]# sh read1.sh
请输入姓名,年龄,爱好:张三 20 Shell
姓名:张三
年龄:20
爱好:Shell
[root@VM-0-2-centos sun]#
read读取一个字符
[root@VM-0-2-centos sun]# vim read2.sh
[root@VM-0-2-centos sun]# cat read2.sh
#!/bin/bash
read -n 1 -p "你确定要删除吗?(y/n)" char
printf "\n"
echo "你输入的字符是:${char}"
[root@VM-0-2-centos sun]# sh read2.sh
你确定要删除吗?(y/n)y
你输入的字符是:y
[root@VM-0-2-centos sun]#
read限制输入时间
[root@VM-0-2-centos sun]# vim read3.sh
[root@VM-0-2-centos sun]# cat read3.sh
#!/bin/bash
# 输入密码 -s代表静默模式,输入时不显示
read -t 20 -sp "请输入密码(20秒内):" pwd1
# 回车
echo
read -t 20 -sp "请再次输入密码(20秒内):" pwd2
# 回车
printf "\n"
# 判断
if [ $pwd1 == $pwd2 ]
then
echo "密码验证正确!"
else
echo "密码验证失败!"
fi
[root@VM-0-2-centos sun]# sh read3.sh
请输入密码(20秒内):
请再次输入密码(20秒内):
密码验证正确!
[root@VM-0-2-centos sun]# sh read3.sh
请输入密码(20秒内):
请再次输入密码(20秒内):
密码验证失败!
[root@VM-0-2-centos sun]#
declare命令
-i 代表整形,后续赋值字符串,将不会起作用,变为0
+i 代表取消整形
-r 代表制度
[root@VM-0-2-centos sun]# declare -i age=20
[root@VM-0-2-centos sun]# age=abc
[root@VM-0-2-centos sun]# echo $age
0
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# age=20
[root@VM-0-2-centos sun]# echo $age
20
[root@VM-0-2-centos sun]# declare +i age
[root@VM-0-2-centos sun]# age=abc
[root@VM-0-2-centos sun]# echo $age
abc
[root@VM-0-2-centos sun]# declare -r age
[root@VM-0-2-centos sun]# age=bcd
bash: age: readonly variable
[root@VM-0-2-centos sun]#
declare实现关联数组
[root@VM-0-2-centos sun]# vim declare1.sh
[root@VM-0-2-centos sun]# cat declare1.sh
#!/bin/bash
# 设置索引数组
echo "declare 设置索引数组:"
declare -a array=(abc 123 "dajskd")
# 获取索引数组数据
echo "索引为2的:${array[2]}"
echo "所有元素:${array[@]}"
echo
# 设置关联数组
echo "declare 设置关联数组:"
declare -A array=(["one"]=abc ["num"]=123 ["la"]= "dajskd")
# 获取索引数组数据
echo "获取key为num的元素:${array[num]}"
echo "所有元素:${array[@]}"
echo
[root@VM-0-2-centos sun]# sh declare1.sh
declare 设置索引数组:
索引为2的:dajskd
所有元素:abc 123 dajskd
declare 设置关联数组:
declare1.sh: line 15: array: cannot convert indexed to associative array
declare1.sh: line 15: array: cannot convert indexed to associative array
获取key为num的元素:abc
所有元素:abc 123 dajskd
[root@VM-0-2-centos sun]#
Shell运算符
expr命令
[root@VM-0-2-centos ~]# expr 1 + 2
3
[root@VM-0-2-centos ~]# expr 1+2
1+2
[root@VM-0-2-centos ~]# result=`1 + 2`
-bash: 1: command not found
[root@VM-0-2-centos ~]# result=`expr 1 + 2`
[root@VM-0-2-centos ~]# echo $result
3
[root@VM-0-2-centos ~]# expr 1 + 1.1
expr: non-integer argument
[root@VM-0-2-centos sun]# cat expr1.sh
#!/bin/bash
read -p "输入两个数字:" oper1
read -p "输入两个数字:" oper2
echo "oper1 + oper2 = `expr ${oper1} + ${oper2}`"
echo "oper1 - oper2 = `expr ${oper1} - ${oper2}`"
echo "oper1 * oper2 = `expr ${oper1} \* ${oper2}`"
echo "oper1 / oper2 = `expr ${oper1} / ${oper2}`"
echo "oper1 % oper2 = `expr ${oper1} % ${oper2}`"
[root@VM-0-2-centos sun]# sh expr1.sh
[root@VM-0-2-centos sun]# sh expr1.sh
输入两个数字:1
输入两个数字:2
oper1 + oper2 = 3
oper1 - oper2 = -1
oper1 * oper2 = 2
oper1 / oper2 = 0
oper1 % oper2 = 1
比较运算符
[root@VM-0-2-centos sun]# [ 1 -eq 2 ]
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# [ 1 -ne 2 ]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# [ 1 -ge 2 ]
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# [ 1 -lt 2 ]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]# [ 1 -le 2 ]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# [ 1 -le 2.0 ]
-bash: [: 2.0: integer expression expected
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# ((1 == 2))
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# ((1 != 2))
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]# ((1 > 2))
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# ((1 < 2))
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]# ((1 <= 2))
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]# ((1 >= 2))
[root@VM-0-2-centos sun]# echo $?
1
字符串比较运算符
[] :如果字符串内由空格,会进行分割,再比较
[[]] :推荐使用
[root@VM-0-2-centos sun]# [[ 1.1 == 2 ]]
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# [[ 1.1 == 1.1 ]]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]# [[ abc == cde ]]
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# [[ abc == abc ]]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]# [ a == b ]
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# [ a == a ]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# [ -n "" ]
[root@VM-0-2-centos sun]# echo $?
1
[root@VM-0-2-centos sun]# [ -n "ac" ]
[root@VM-0-2-centos sun]# echo $?
0
[root@VM-0-2-centos sun]#
文件测试运算符
文件类型介绍
-:普通文件
d:目录文件
l:连接文件
b:块设备文件
c:设备文件
p:管道文件
文件测试运算符
let命令
[root@VM-0-2-centos /]# let a=1
[root@VM-0-2-centos /]# let b=1
[root@VM-0-2-centos /]# echo $((a+b))
2
[root@VM-0-2-centos /]#
[root@VM-0-2-centos /]# let a=(10*10)/2+5
[root@VM-0-2-centos /]# echo $((a))
55
[root@VM-0-2-centos /]#
bc命令
[root@VM-0-2-centos /]# vim task.txt
[root@VM-0-2-centos /]# bc -q task.txt
617
28
[root@VM-0-2-centos /]# bc -q
1+2
3
3/4
0
scale=2;3/4
.75
ibase=2;7
7
ibase=2;111
7
obase=2;7
111
quit
[root@VM-0-2-centos /]#
[root@VM-0-2-centos /]# bc -q -l
e(2)
7.38905609893065022723
s(12)
-.53657291800043497166
l(e)
-99999999999999999999.00000000000000000000
bc非互动管道运算
[root@VM-0-2-centos /]# echo "1+2" | bc
3
[root@VM-0-2-centos /]# echo "scale=2;1/3" | bc
.33
[root@VM-0-2-centos /]# echo "e(2)" | bc -l
7.38905609893065022723
[root@VM-0-2-centos /]# echo "obase=2;7" | bc
111
[root@VM-0-2-centos /]# a=2
[root@VM-0-2-centos /]# echo "b=$a+2;b"
b=2+2;b
[root@VM-0-2-centos /]# echo "b=$a+2;b" | bc
4
[root@VM-0-2-centos /]# c=`echo "1+1" | bc`
[root@VM-0-2-centos /]# echo $c
2
[root@VM-0-2-centos /]#
bc非互动输入重定向运算
[root@VM-0-2-centos /]# b=`bc -l << EOF
> 1+1
> 2+2
> 3+3
> EOF
> `
[root@VM-0-2-centos /]# echo $b
2 4 6
[root@VM-0-2-centos /]#
流程控制语句
if - else
[root@VM-0-2-centos sun]# vim test.sh
[root@VM-0-2-centos sun]# sh test.sh
请输入你的分数:101
成绩输入有误!
[root@VM-0-2-centos sun]# sh test.sh
请输入你的分数:90
成绩非常优秀!
[root@VM-0-2-centos sun]# sh test.sh
请输入你的分数:20
成绩不合格!
[root@VM-0-2-centos sun]# cat test.sh
#!/bin/bash
read -p "请输入你的分数:" score
if ((score< 60 && score >= 0))
then
echo "成绩不合格!"
elif ((score<70 && score >=60))
then
echo "成绩差!"
elif ((score<80 && score >=70))
then
echo "成绩良好!"
elif ((score<90 && score >=80))
then
echo "成绩优秀!"
elif ((score>=90 && score<=100))
then
echo "成绩非常优秀!"
else
echo "成绩输入有误!"
fi
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# if((1==1)); then echo "1111"; else echo "2222";fi;
1111
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# touch test3.txt
[root@VM-0-2-centos sun]# cat test2.sh
#!/bin/bash
read -p "设置文件的全路径名:" filename
read -p "设置输入数据:" data
if [ -w $filename -a -n data ]
then
echo $data > $filename
else
echo "文件创建失败!"
fi
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# sh test2.sh
设置文件的全路径名:/home/sun/test3.txt
设置输入数据:hello woe^He^Hr
[root@VM-0-2-centos sun]# cat test3.txt
hello wor
[root@VM-0-2-centos sun]#
test命令
[root@VM-0-2-centos sun]# test 1 -eq 2;echo $?
1
[root@VM-0-2-centos sun]# test 1 -eq 1;echo $?
0
[root@VM-0-2-centos sun]# test a == b;echo $?
1
[root@VM-0-2-centos sun]# test a == a;echo $?
0
[root@VM-0-2-centos sun]# test -z "a";echo $?
1
[root@VM-0-2-centos sun]# test -z "";echo $?
0
[root@VM-0-2-centos sun]# test -e test2.sh;echo $?
0
[root@VM-0-2-centos sun]# test -w test2.sh;echo $?
0
[root@VM-0-2-centos sun]# test -r test2.sh;echo $?
0
[root@VM-0-2-centos sun]# test -x test2.sh;echo $?
1
[root@VM-0-2-centos sun]# test 1 -eq 1 -a 3 \> 2;echo $?
0
[root@VM-0-2-centos sun]#
case命令
[root@VM-0-2-centos sun]# vim week.sh
[root@VM-0-2-centos sun]# sh week.sh
输入一个数字:1
星期一
[root@VM-0-2-centos sun]# sh week.sh
输入一个数字:0
星期天
[root@VM-0-2-centos sun]# sh week.sh
输入一个数字:8
输入数字无效!
[root@VM-0-2-centos sun]# cat week.sh
#!/bin/bash
read -p "输入一个数字:" number
case $number in
1)
echo "星期一"
;;
2)
echo "星期二"
;;
3)
echo "星期三"
;;
4)
echo "星期四"
;;
5)
echo "星期五"
;;
6)
echo "星期六"
;;
7|0)
echo "星期天"
;;
*)
echo "输入数字无效!"
esac
[root@VM-0-2-centos sun]#
while循环
[root@VM-0-2-centos sun]# cat test5.sh
#!/bin/bash
read -p "请输入一个循环数字:" number
i=1
while ((i<=number))
do
echo "hello${i}"
let i++
done
[root@VM-0-2-centos sun]# sh test5.sh
请输入一个循环数字:5
hello1
hello2
hello3
hello4
hello5
[root@VM-0-2-centos sun]#
for循环
[root@VM-0-2-centos sun]# vim test5.sh
[root@VM-0-2-centos sun]# cat test5
cat: test5: No such file or directory
[root@VM-0-2-centos sun]# cat test5.sh
#!/bin/bash
read -p "请输入一个循环数字:" number
i=1
for i in 1 3 5 7 9
do
echo "hello${i}"
done
[root@VM-0-2-centos sun]# sh test5.sh
请输入一个循环数字:5
hello1
hello3
hello5
hello7
hello9
[root@VM-0-2-centos sun]#
[root@VM-0-2-centos sun]# vim test5.sh
[root@VM-0-2-centos sun]# sh test5.sh
请输入一个循环数字:10
hello1
hello2
hello3
hello4
hello5
hello6
hello7
hello8
hello9
hello10
[root@VM-0-2-centos sun]# cat test5
cat: test5: No such file or directory
[root@VM-0-2-centos sun]# cat test5.sh
#!/bin/bash
read -p "请输入一个循环数字:" number
i=1
for i in {1..10}
do
echo "hello${i}"
done
[root@VM-0-2-centos ~]# for((i=1;i<=10;i++));do echo "hello${i}";done;
hello1
hello2
hello3
hello4
hello5
hello6
hello7
hello8
hello9
hello10
[root@VM-0-2-centos ~]#
系统函数
获取文件名 basename
[root@VM-0-2-centos sun]# basename /sun/home/sun/test1.sh
test1.sh
[root@VM-0-2-centos sun]# basename /sun/home/sun/test1.sh .sh
test1
[root@VM-0-2-centos sun]#
提取目录 dirname
[root@VM-0-2-centos sun]# dirname /sun/home/sun/test1.sh
/sun/home/sun
[root@VM-0-2-centos sun]#
Shell自定义函数
无参无返回值
[root@VM-0-2-centos sun]# cat fun1.sh
#!/bin/bash
function demo(){
echo "执行了函数!"
}
# 调用函数
demo
[root@VM-0-2-centos sun]# sh fun1.sh
执行了函数!
[root@VM-0-2-centos sun]#
无参有返回值
[root@VM-0-2-centos sun]# vim fun2.sh
[root@VM-0-2-centos sun]# cat fun2.sh
#!/bin/bash
sum(){
echo "求两个数的和"
read -p "请输入第一个数字:" n1
read -p "请输入第二个数字:" n2
echo ”两个数字分别为 $n1 和 $n2“
return $(($n1+$n2))
}
sum
echo "两个数的和为:$?"
[root@VM-0-2-centos sun]# sh fun2.sh
求两个数的和
请输入第一个数字:1
请输入第二个数字:3
”两个数字分别为 1 和 3“
两个数的和为:4
[root@VM-0-2-centos sun]#
Shell重定向输入与输出
0输入数据
1输出正确数据
2输出错误数据
将结果输入到文件中,覆盖方式 > 文件名
将结果输入到文件中,追加方式 >> 文件名
将错误信息也输出到文件中 >>文件名 2&>1
[root@VM-0-2-centos sun]# echo "hello3" >> log.txt
[root@VM-0-2-centos sun]# cat log.txt
hello2
hello3
[root@VM-0-2-centos sun]# ll adddddd
ls: cannot access adddddd: No such file or directory
[root@VM-0-2-centos sun]# ll adddddd >> log.txt
ls: cannot access adddddd: No such file or directory
[root@VM-0-2-centos sun]# ll adddddd 2>> log.txt
[root@VM-0-2-centos sun]# cat log.txt
hello2
hello3
ls: cannot access adddddd: No such file or directory
[root@VM-0-2-centos sun]# ll adddddd >> log.txt 2>&1
[root@VM-0-2-centos sun]# cat log.txt
hello2
hello3
ls: cannot access adddddd: No such file or directory
ls: cannot access adddddd: No such file or directory
[root@VM-0-2-centos sun]# ll add >> log.txt 2>&1
[root@VM-0-2-centos sun]# cat log.txt
hello2
hello3
ls: cannot access adddddd: No such file or directory
ls: cannot access adddddd: No such file or directory
ls: cannot access add: No such file or directory
[root@VM-0-2-centos sun]# ll >> log.txt 2>&1
[root@VM-0-2-centos sun]# cat log.txt
hello2
hello3
ls: cannot access adddddd: No such file or directory
ls: cannot access adddddd: No such file or directory
ls: cannot access add: No such file or directory
total 80
-rw-r--r-- 1 root root 0 Dec 17 13:10 2
-rw-r--r-- 1 root root 77 Dec 16 18:34 batch.sh
-rw-r--r-- 1 root root 435 Dec 17 01:20 declare1.sh
-rw-r--r-- 1 root root 47 Dec 16 20:02 demo1.sh
-rw-r--r-- 1 root root 58 Dec 16 20:00 demo2.sh
-rw-r--r-- 1 root root 55 Dec 16 20:26 demo3.sh
-rw-r--r-- 1 root root 231 Dec 16 20:32 demo4.sh
-rw-r--r-- 1 root root 337 Dec 17 11:17 expr1.sh
-rw-r--r-- 1 root root 77 Dec 17 16:37 fun1.sh
-rw-r--r-- 1 root root 228 Dec 17 16:42 fun2.sh
-rwxrwxrwx 1 root root 32 Dec 16 18:15 helloworld.sh
-rw-r--r-- 1 root root 169 Dec 17 16:59 log.txt
-rw-r--r-- 1 root root 13 Dec 16 18:35 one.txt
-rw-r--r-- 1 root root 138 Dec 17 00:34 read1.sh
-rw-r--r-- 1 root root 109 Dec 17 00:41 read2.sh
-rw-r--r-- 1 root root 323 Dec 17 00:55 read3.sh
-rw-r--r-- 1 root root 194 Dec 17 13:02 test2.sh
-rw-r--r-- 1 root root 14 Dec 17 13:04 test3.txt
-rw-r--r-- 1 root root 105 Dec 17 13:56 test5.sh
-rw-r--r-- 1 root root 392 Dec 17 12:50 test.sh
-rw-r--r-- 1 root root 282 Dec 17 13:18 week.sh
[root@VM-0-2-centos sun]#