linux 编写脚本示范,Linux-scripts-简单脚本和脚本的执行

9.Scripts

9.1什么是 Shell scripts

shell script 是利用 shell 的功能所写的一个『程序 (program)』,这个程序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面,搭配正规表示法、管线命令与数据流重导向等功能,以达到我们所想要的处理目的。

9.1.1干嘛学习 shell scripts

l自动化管理的重要依据

l追踪与管理系统的重要工作

l简单***检测功能

l连续指令单一化

l简易的数据处理

9.1.2第一支 script 的撰写与执行

在 shell script 的撰写中还需要用到底下的注意事项:

l指令的执行是从上而下、从左而右的分析与执行;

l指令、选项与参数间的多个空白都会被忽略掉;

l空白行也将被忽略掉,并且 [tab] 按键所推开的空白同样视为空格键;

l如果读取到一个 Enter 符号 (CR) ,就尝试开始执行该行 (或该串) 命令;

l至于如果一行的内容太多,则可以使用『 \[Enter] 』来延伸至下一行;

l『 # 』可做为批注!任何加在 # 后面的资料将全部被视为批注文字而被忽略!

1.撰写第一支 script

[root@localhost 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:~/bin

export PATH          #定义PATH,属个人习惯

echo -e "Hello World! \a \n"       #输出hello world!

exit 0         #设置脚本返回值

[root@localhost 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.对谈式脚本:变量内容由用户决定

[root@localhost tmp]#vim showname.sh

#!/bin/bash

read -p "Please input your first name: " firstname      # 提示使用者输入

read -p "Please input your last name:  " lastname       # 提示使用者输入

echo -e "\nYour full name is: ${firstname} ${lastname}" # 结果由屏幕输出

[root@localhost tmp]# sh  showname.sh

Please input your first name:liu

Please input your last name:  hua

Your full name is: liu hua

2.随日期变化:利用 date 进行档案的建立

[root@localhost tmp]#  vim 1.sh

#!/bin/bash

echo -e "I will use 'touch' command to create 3 files." # 纯粹显示信息

read -p "Please input your filename: " 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}"

[root@localhost tmp]# sh 1.sh

I will use 'touch' command to create 3 files.

Please input your filename: file

[root@localhost tmp]# ls

1.sh  file20200425  file20200426  file20200427  hello.sh

3.数值运算:简单的加减乘除

[root@localhost tmp]# vim 1.sh

#!/bin/bash

echo -e "You SHOULD input 2 numbers, I will multiplying them! \n"

read -p "first number:  " firstnu

read -p "second number: " secnu

total=$((${firstnu}*${secnu}))

echo -e "\nThe result of ${firstnu} x ${secnu} is ==> ${total}"

[root@localhost tmp]# sh 1.sh

You SHOULD input 2 numbers, I will multiplying them!

first number:  2

second number: 3

The result of 2 x 3 is ==> 6

9.2.2 script的执行方式差异 (source, sh script, ./script)

[root@localhost tmp]# cat showname.sh

#!/bin/bash

read -p "Please input your first name: " firstname

read -p "Please input your last name:  " lastname

echo -e "Your full name is: ${firstname} ${lastname}"

1.相对路径执行

[root@localhost tmp]# chmod a+x showname.sh  #先增加执行权限

[root@localhost tmp]# ./showname.sh    #相对路径执行

Please input your first name: liu

Please input your last name:  hua

Your full name is: liu hua

[root@localhost tmp]# echo $firstname $lastname

#父进程中没有这两个变量

2.绝对路径执行(也需要执行权限)

[root@localhost tmp]# /tmp/showname.sh

Please input your first name: liu

Please input your last name:  hua

Your full name is: liu hua

[root@localhost tmp]# echo $firstname $lastname

#父进程中没有这两个变量

#注:

l相对路径和绝对路径执行需要文件的执行权限

l脚本在子进程中执行,所以,父进程中没有$firstname $lastname两个变量

l直接执行showname.sh,可能会失败,因为当前目录可能不在PATH中

3.sh执行

[root@localhost tmp]# sh showname.sh

Please input your first name: liu

Please input your last name:  hua

Your full name is: liu hua

[root@localhost tmp]# echo $firstname $lastname

#脚本在子进程中执行,所以,父进程中没有$firstname $lastname两个变量

#sh执行不需要文件的执行权限

b4e2f7894c7dc4eb8cf9facced717e53.png4.source和.执行

[root@localhost tmp]# . showname.sh

Please input your first name: liu

Please input your last name:  hua

Your full name is: liu hua

[root@localhost tmp]# echo $firstname $lastname

liu hua

[root@localhost tmp]# source showname.sh

Please input your first name: liu

Please input your last name:  hua

Your full name is: liu hua

[root@localhost tmp]# echo $firstname $lastname

liu hua

#脚本在父进程中执行,所以,有$firstname $lastname两个变量

#不需要文件的执行权限

4c5b8734d47d9bc3ac509cdf81eb8490.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值