shell脚本编辑

shell:
1,归档:将多个文件打包在一起tar(归档,解档)
tar -cvf ***.tar 文件列表
tar -xvf ***.tar 文件列表

2,gzip是GNU组织开发的一个压缩程序,后缀:gz
tar -czf ***.tar.gz 文件列表
tar -xzf ***.tar.gz 文件列表
3,zip:Linux提供了zip和unzip程序来压缩和解压程序
zip ***.zip 文件列表
unzip ***.zip (后面不用跟文件列表)


shell脚本:为了完成某一个任务,将相关的shell命令存放在文件中,并自动执行
1,脚本解释器:/bin/bash
2,shell源文件的后缀名:.sh
3,编写第一个脚本:# !/bin/bash
4,输出:echo “信息” #输出行 printf "格式"输出内容
5,输入:read 变量名
read -p “提示文本” 变量名
6,执行程序:路径/文件名
7,执行的顺序:从上而下
8,程序=算法+数据
9,shell是一门弱语言:shell是没有数据类型的,只有一个字符串类型


定义变量:变量名=值

引用变量:应用变量的内容

获取变量内容:${变量名}

获取变量长度:${#变量名}

获取字符串: ${变量名:n:m}

字符串表示法(三种):“hello” ‘hello’ hello
‘$var hello’ 都是字符串

“$var hello” 引用变量的内容+hello

转义字符:n \n t \t


数值测试:由于shell的数值均为字符串,需要借助test命令来判断大小
test 比较文件类型或者数值字符串的大小
test num -eq num2
test num -gt num2
test num -ge num2
test num -lt num2
test num -le num2
test num -ne num2


10,流程控制:选择控制 循环控制

选择控制:当某条件成立时才执行,否则不执行

if condition
then 
	command...
else
	command...
fi

多分支:

if condition1
then
	command1...
elif  condition2 
then
	command2...
elif  condition3 
then
	command3...
elif  condition4 
then
	command4...	
else
	command...
fi	

嵌套if

if  condition
then
	if condition1
	then
		command
	fi
else	
	command...
fi		

练习:
3、请完成如下要求的脚本:输入一个文件名,并创建该文件 mkfile.sh
在这里插入图片描述
在这里插入图片描述

练习:
4、比较两个输入的数字的大小,并比较出输入最小值
在这里插入图片描述

在这里插入图片描述

练习:
5、求num1,num2,num3的最大值,求次大值

在这里插入图片描述

测试发现有一个错误!

在这里插入图片描述

改正后:

在这里插入图片描述
改正后结果测试正确

在这里插入图片描述

开心!

猜你喜欢

转载自blog.csdn.net/weixin_43807294/article/details/84668583