当前位置:网站首页>Shell脚本基本使用
Shell脚本基本使用
2022-07-02 22:59:00 【JunesFour】
shell基本使用
Shell简介
Shell是一个用c语言编写的程序,它是用户使用Linux的桥梁. Shell 既是命令语言,又是一种程序设计语言.
Shell脚本(shell script),是一种为Shell编写的脚本程序。业界所说的Shell通常指的是Shell脚本. Shell 与Shell脚本是两个不同的概念.
Linux中的shell有很多种类,常用的几种:
- Bourne Shell (/usr/bin/sh 或/bin/sh)
- Bourne Again Shell (/bin/bash)
- C Shell (/usrbin/csh)
- K Shell (/usr/bin/ksh)
- Shell for Root (/sbin/sh)
Bash(Bourne Again Shell)由于易用和免费,在日常工作中被
广泛使用。同时,Bash也是大多数Linux系统默认的Shell.
第一个Shell脚本
创建一个first.sh文件,Shell脚本文件以.sh结尾,然后开始编辑文件:
#!/bin/bash
echo "hello world"
编写完之后使用ll命令查看文件的权限:
只
允许读和写,不能执行,所以要为它加上可执行权限:
chmod +x first.sh
看到文件变成绿色以后,使用./firsh.sh执行:
常用语法
定义变量
#!/bin/bash
echo "hello world"
greet="hello program"
echo $greet

- 注意定义变量的时候,
=号的两边不能有空格. - 后面在使用变量的时候,变量的前面要加上
$符号.
if条件判断
#!/bin/bash
num=1
if [$num -eq 0]; then
echo "num is 0"
elif [$num -eq 1]; then
echo "num is 1"
else
echo "num is not 0 and 1"
fi
for循环
假设要打印/opt/module目录下面的所有文件:
#!/bin/bash
for file in $(ls /opt/module/); do
echo $file
done

从1加到100:
#!/bin/bash
sum=0
for i in {
1..100}; do
let sum+=i;
done
echo $sum

检测局域网内机器的工作状态:
假设现在处于的网段是192.168.199,要求打印出当前网段下所有机器的工作状态.
#!/bin/bash
for i in {
1..254}; do
ping -c 2 -i 0.5 192.168.199.$i &>/dev/null
if [$? -eq 0]; then
echo "192.168.199.$i is up"
else
echo "192.168.199.$i is down"
fi
done
ping -c [count]表示ping的次数.ping -i [time]表示每次ping的时间间隔./dev/null表示把打印的一些无用信息过滤掉.$?表示上一行指令的执行结果.
后面使用到再进行追加.
边栏推荐
- 返回二叉树中最大的二叉搜索子树的大小
- 论文的英文文献在哪找(除了知网)?
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
- [reading notes] phased summary of writing reading notes
- Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
- Which software can translate an English paper in its entirety?
- S12. Verify multi host SSH mutual access script based on key
- RTP 接发ps流工具改进(二)
- Slf4j + logback logging framework
- 附加:token;(没写完,别看…)
猜你喜欢
![Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation](/img/4a/ab732c41ea8a939fa0983fec475622.png)
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation

Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)

Digital twin smart factory develops digital twin factory solutions

Bloom filter

Cmake basic use

QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)

Should you study kubernetes?

Custom throttling function six steps to deal with complex requirements

TypeError: Cannot read properties of undefined (reading ***)

Digital collection trading website domestic digital collection trading platform
随机推荐
67 page overall planning and construction plan for a new smart city (download attached)
What website can you find English literature on?
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
JDBC tutorial
How to write the design scheme of the thesis?
【OJ】两个数组的交集(set、哈希映射 ...)
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
Several methods of the minimum value in the maximum value of group query
英文论文有具体的格式吗?
Chapter 4 of getting started with MySQL: data types stored in data tables
Realization of mask recognition based on OpenCV
S12. Verify multi host SSH mutual access script based on key
Digital twin visualization solution digital twin visualization 3D platform
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
写论文可以去哪些网站搜索参考文献?
实用系列丨免费可商用视频素材库
How do educators find foreign language references?
cocospods 的使用
35 pages dangerous chemicals safety management platform solution 2022 Edition
MySQL advanced learning notes (4)