shell script(程序化脚本)
迷~+V:w891123884
一、实训目的及要求
1.掌握Linux shell script的简单编程
2.掌握shell script中的加减乘除 运算符
3.熟悉linux操作环境
二、实训设备、软件
个人计算机一台 虚拟机VMware软件 CentOS7操作系统
三、实训原理及内容
1.编写一个脚本,用户输入first name和second name后,输出fullname
[root@localhost scripts]# cat sh01.sh
#!bin/bash
PATH=bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PASH
read -p "Please input your firstname:" firstname
read -p "Please input your second name:" secondname
echo -e "\nYou full name is: $firstname $secondname"
[root@localhost scripts]# sh sh01.sh
Please input your firstname:22
Please input your second name:33
You full name is: 22 33
2.编写script,以前天昨天今天的日期来创建三个文件
[root@localhost scripts]# cat sh02.sh
#!bin/bash
PATH=bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PASH
echo -e "I will use 'touch' command to create 3 files."
read -p "Please input your filename:" fileuser
filename=${
fileuser:-"filename"}
date1=$(date --date='2 days ago' +%Y%m%d)
date2=$(date --date='1 days ago' +%Y%m%d)
date3=$(date +%Y%m%d)
file1=${
filename}${
date1}
file2=${
filename}${
date2}
file3=${
filename}${
date3}
touch "$file1"
touch "$file2"
touch "$file3"
3、任意输入两个数,并输出二者的加减乘除以及取余结果
[root@localhost scripts]# cat sh03.sh
#!bin/bash
PATH=bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PASH
echo -e "You SHOULD input 2 numbers, I will cross them! \n"
read -p "the first number:" a
read -p "the second number:" b
c=`expr $a + $b`
echo "a+b:$c"
c=`expr $a - $b`
echo "a-b:$c"
c=`expr $a \* $b`
echo "a*b:$c"
c=`expr $a / $b`
echo "a\b:$c"
[root@localhost scripts]# sh sh03.sh
You SHOULD input 2 numbers, I will cross them!
the first number:89
the second number:55
a+b:144
a-b:34
a*b:4895
a\b:1
四、项目导入
如果想要管理好主机,一定要学好shell script(程序化脚本)。shell script 有点像是早期的批处理,即将一些命令汇总起来一次运行。但是shell script 拥有很强大的功能,那就是它可以进行类似程序(program)的撰写,并且不需要编译(compile)就能够运行,非常的方便。在整个linux的环境中,一些服务(service)的启动都是通过shell script 来运行的。
需要其他脚本+V:w891123884