shell构建基本脚本

news/2025/2/22 3:55:59/

构建基本脚本

使用多个命令

一次使用多个命令,把它们放在一行,使用’;'隔开

[root@myserver ~]# date ; who; ls
Sun May 14 23:39:34 CST 2023
root     pts/0        2023-05-14 23:31 (192.168.10.1)
anaconda-ks.cfg  initial-setup-ks.cfg

创建shell脚本

创建shell脚本,必须在文件第一行指定要使用的shell:

#!/bin/bash

编写一个简单的脚本,第一行#!告诉shell使用哪里的shell来运行脚本,后面的#都是起注释作用

#创建一个临时目录用于测试
[root@myserver ~]# mkdir tmp
[root@myserver ~]# cd tmp
[root@myserver tmp]# vi tmp.sh
[root@myserver tmp]# cat tmp.sh
#!/bin/bash
date
who
ls

现在运行一下

[root@myserver tmp]# tmp.sh
bash: tmp.sh: command not found...

shell是通过PATH环境变量来查找命令,查看PATH环境变量

[root@myserver tmp]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

PATH变量被设置成只在一组目录中查找命令。要让shell找到tmp脚本,两种做法:

  • shell脚本所在目录添加到PATH环境变量中

  • 在提示符中使用绝对或相对文件路径引用shell脚本

使用单点操作符

[root@myserver tmp]# ./tmp.sh
-bash: ./tmp.sh: Permission denied

访问被拒绝,赋予文件执行权限

[root@myserver tmp]# ll
total 4
-rw-r--r--. 1 root root 24 May 14 23:47 tmp.sh
[root@myserver tmp]# chmod u+x tmp.
[root@myserver tmp]# ./tmp.sh
Mon May 15 00:06:10 CST 2023
root     pts/0        2023-05-14 23:31 (192.168.10.1)
tmp.sh
sh

显示消息

#一般输出字符不需要加引号,但如果出现单引号就需要双引号进行界定
[root@myserver tmp]# echo hello shell
hello shell
[root@myserver tmp]# echo let's see if this'll work
lets see if thisll work
[root@myserver tmp]# echo "let's see if this'll work"
let's see if this'll work

可以将echo语句添加到shell脚本中去,显示额外信息:

[root@myserver tmp]# cat tmp.sh
#!/bin/bash
echo The time and date are:
date
echo Who logged into the system:
who
echo Files in the current folder:
ls
[root@myserver tmp]# ./tmp.sh
The time and date are:
Mon May 15 00:18:26 CST 2023
Who logged into the system:
root     pts/0        2023-05-14 23:31 (192.168.10.1)
Files in the current folder:
tmp.sh

如果想把文本字符串和命令输出在同一行,使用echo

[root@myserver tmp]# cat tmp.sh
#!/bin/bash
echo -n  The time and date are:
date
echo Who logged into the system:
who
echo Files in the current folder:
ls
[root@myserver tmp]# ./tmp.sh
The time and date are:Mon May 15 00:26:57 CST 2023
Who logged into the system:
root     pts/0        2023-05-14 23:31 (192.168.10.1)
Files in the current folder:
tmp.sh

变量

环境变量

shell维护一组环境变量,用于记录特定系统信息。

set显示完整环境变量:

set

在脚本中可以使用’$'来使用这些环境变量

[root@myserver tmp]# cat test
#!/bin/bash
echo user info : $USER
echo uid : $UID
echo home : $HOME
[root@myserver tmp]# ./test
user info : root
uid : 0
home : /root

显示美元符号

[root@myserver tmp]# echo $15
5
[root@myserver tmp]# echo \$15
$15

用户变量

用户变量由用户定义,长度不超过20个,区分大小写

[root@myserver tmp]# cat test
#!/bin/bash
a=10
echo a val : $a
b=15
echo b val : $b
[root@myserver tmp]# ./test
a val : 10
b val : 15

退出脚本

Linux提供了专门的变量$?来保存上一个已执行命令的退出状态码

[root@myserver ~]# echo $?
0
[root@myserver ~]# abcd
bash: abcd: command not found...
[root@myserver ~]# echo $?
127

默认情况下,shell脚本会以脚本中最后一个命令的推出状态码推出


[root@myserver tmp]# cat test
#!/bin/bash
echo $(date)
[root@myserver tmp]# ./test
Mon May 15 13:27:30 CST 2023
[root@myserver tmp]# echo $?
0

状态码:
在这里插入图片描述

你可以改变这种默认,返回你自己的退出状态码

[root@myserver tmp]# cat test
#!/bin/bash
a=10
b=20
c=$[$a+$b]
exit $c
[root@myserver tmp]# ./test
[root@myserver tmp]# echo $?
30

当你设置状态码大于255时,shell就会模256余数


[root@myserver tmp]# cat test
#!/bin/bash
a=100
b=200
c=$[$a+$b]
exit $c
[root@myserver tmp]# ./test
[root@myserver tmp]# echo $?
44

http://www.ppmy.cn/news/70498.html

相关文章

校园安全,一键报警主机助力保障

校园安全,一键报警主机助力保障 随着社会发展和科技进步,校园安全问题日益受到重视。如何保障师生们的安全成为了学校一项重要任务。而校园可视一键报警主机就是一种非常有效的安保设备。 这种报警主机集合了视频监控、安全防范、数据处理等多个功能&a…

【微电网】含风、光、储联合发电的微电网优化调度研究(Matlab代码实现)

💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…

乔哈里窗模型

乔哈里窗由心理学家乔瑟夫和哈里在20世纪50年代提出的,也常被称之为"自我意识的发现/反馈模型”,或“信息交流过程管理工具”。 模型介绍 该模型把人的内心信息分成四个区域,即: 第一个区域,我知道,你…

EW代理工具的使用说明

一、EW介绍 Earthworm(EW) 是一套便携式的网络穿透工具,具有 SOCKS v5服务架设和端口转发两大核心功能,可在复杂网络环境下完成网络穿透。 该工具能够以“正向”、“反向”、“多级级联”等方式打通一条网络隧道,直达…

RVMedia VCL 8.0 for Delphi Crack

RVMedia VCL 8.0 for Delphi Crack FGX Native Network Frame是制造跨平台和现代移动设备的强大工具。FG开发团队的主要目标是简化移动应用程序的开发,使大多数人都能以各种技能开发应用程序。此外,这种形式的网络最重要的功能可以在100%的用户界面中设计…

变量与常量(基础)

1.0变量与常量 变量的概念:变量用于存储编程所使用的数据和方法。 声明一般变量关键字:var, let, const。其中let和const是es6的语法。 声明其他特殊变量关键字:function,class,import等 声…

什么是 Java 中的多线程(Multithreading)?

多线程(Multithreading)是指在一个程序中同时运行多个线程,每个线程都是独立运行的,可以执行不同的任务。Java 中的多线程机制是一种并发编程的实现方式,它允许程序在同一时间执行多个任务,从而提高程序的运…

云计算(Cloud Computing)

一、从技术概念理解云计算 早期的云计算就是虚拟化主机上的分布式计算,现阶段的云计算,已经不单单是一种分布式计算,而是分布式计算、效用计算、负载均衡、并行计算、网络存储、热备份冗杂和虚拟化等计算机技术混合演进并跃升的结果。云计算…