shell script 实例------Linux

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木头科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值