镇海住房和建设交通局网站,怎样免费建公司网站,营销策划案例,wordpress安全教程9.Scripts9.1什么是 Shell scriptsshell script 是利用 shell 的功能所写的一个『程序 (program)』#xff0c;这个程序是使用纯文本文件#xff0c;将一些 shell 的语法与指令(含外部指令)写在里面#xff0c;搭配正规表示法、管线命令与数据流重导向等功能#xff0c;以达…9.Scripts9.1什么是 Shell scriptsshell script 是利用 shell 的功能所写的一个『程序 (program)』这个程序是使用纯文本文件将一些 shell 的语法与指令(含外部指令)写在里面搭配正规表示法、管线命令与数据流重导向等功能以达到我们所想要的处理目的。9.1.1干嘛学习 shell scriptsl自动化管理的重要依据l追踪与管理系统的重要工作l简单***检测功能l连续指令单一化l简易的数据处理9.1.2第一支 script 的撰写与执行在 shell script 的撰写中还需要用到底下的注意事项l指令的执行是从上而下、从左而右的分析与执行l指令、选项与参数间的多个空白都会被忽略掉l空白行也将被忽略掉并且 [tab] 按键所推开的空白同样视为空格键l如果读取到一个 Enter 符号 (CR) 就尝试开始执行该行 (或该串) 命令l至于如果一行的内容太多则可以使用『 \[Enter] 』来延伸至下一行l『 # 』可做为批注任何加在 # 后面的资料将全部被视为批注文字而被忽略1.撰写第一支 script[rootlocalhost tmp]# vim hello.sh#编写脚本#!/bin/bash #指定执行脚本的shell# Program:# This program shows Hello World! in your screen.# History:# 2015/07/16 VBird First release # #开头的为注释PATH/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATH #定义PATH属个人习惯echo -e Hello World! \a \n #输出hello worldexit 0 #设置脚本返回值[rootlocalhost tmp]# sh hello.sh#运行脚本Hello World!9.1.3撰写 shell script 的良好习惯建立建议一定要养成良好的 script 撰写习惯在每个 script 的文件头处记录好nscript 的功能nscript 的版本信息nscript 的作者与联络方式nscript 的版权宣告方式nscript 的 History (历史纪录)nscript 内较特殊的指令使用『绝对路径』的方式来下达nscript 运作时需要的环境变量预先宣告与设定。9.2简单的 shell script 练习9.2.1简单范例1.对谈式脚本变量内容由用户决定[rootlocalhost tmp]#vim showname.sh#!/bin/bashread -p Please input your first name: firstname # 提示使用者输入read -p Please input your last name: lastname # 提示使用者输入echo -e \nYour full name is: ${firstname} ${lastname} # 结果由屏幕输出[rootlocalhost tmp]# sh showname.shPlease input your first name:liuPlease input your last name: huaYour full name is: liu hua2.随日期变化利用 date 进行档案的建立[rootlocalhost tmp]# vim 1.sh#!/bin/bashecho -e I will use touch command to create 3 files. # 纯粹显示信息read -p Please input your filename: filename # 提示使用者输入date1$(date --date2 days ago %Y%m%d) # 前两天的日期date2$(date --date1 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}[rootlocalhost tmp]# sh 1.shI will use touch command to create 3 files.Please input your filename: file[rootlocalhost tmp]# ls1.sh file20200425 file20200426 file20200427 hello.sh3.数值运算简单的加减乘除[rootlocalhost tmp]# vim 1.sh#!/bin/bashecho -e You SHOULD input 2 numbers, I will multiplying them! \nread -p first number: firstnuread -p second number: secnutotal$((${firstnu}*${secnu}))echo -e \nThe result of ${firstnu} x ${secnu} is ${total}[rootlocalhost tmp]# sh 1.shYou SHOULD input 2 numbers, I will multiplying them!first number: 2second number: 3The result of 2 x 3 is 69.2.2 script的执行方式差异 (source, sh script, ./script)[rootlocalhost tmp]# cat showname.sh#!/bin/bashread -p Please input your first name: firstnameread -p Please input your last name: lastnameecho -e Your full name is: ${firstname} ${lastname}1.相对路径执行[rootlocalhost tmp]# chmod ax showname.sh #先增加执行权限[rootlocalhost tmp]# ./showname.sh #相对路径执行Please input your first name: liuPlease input your last name: huaYour full name is: liu hua[rootlocalhost tmp]# echo $firstname $lastname#父进程中没有这两个变量2.绝对路径执行(也需要执行权限)[rootlocalhost tmp]# /tmp/showname.shPlease input your first name: liuPlease input your last name: huaYour full name is: liu hua[rootlocalhost tmp]# echo $firstname $lastname#父进程中没有这两个变量#注l相对路径和绝对路径执行需要文件的执行权限l脚本在子进程中执行所以父进程中没有$firstname $lastname两个变量l直接执行showname.sh可能会失败因为当前目录可能不在PATH中3.sh执行[rootlocalhost tmp]# sh showname.shPlease input your first name: liuPlease input your last name: huaYour full name is: liu hua[rootlocalhost tmp]# echo $firstname $lastname#脚本在子进程中执行所以父进程中没有$firstname $lastname两个变量#sh执行不需要文件的执行权限4.source和.执行[rootlocalhost tmp]# . showname.shPlease input your first name: liuPlease input your last name: huaYour full name is: liu hua[rootlocalhost tmp]# echo $firstname $lastnameliu hua[rootlocalhost tmp]# source showname.shPlease input your first name: liuPlease input your last name: huaYour full name is: liu hua[rootlocalhost tmp]# echo $firstname $lastnameliu hua#脚本在父进程中执行所以有$firstname $lastname两个变量#不需要文件的执行权限