当前位置:网站首页>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
表示把打印的一些无用信息过滤掉.$?
表示上一行指令的执行结果.
后面使用到再进行追加.
边栏推荐
- Angled detection frame | calibrated depth feature for target detection (with implementation source code)
- 返回二叉树中最大的二叉搜索子树的根节点
- Blue decides red - burst CS teamserver password
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
- Slf4j + Logback日志框架
- AcWing_188. 武士风度的牛_bfs
- 35页危化品安全管理平台解决方案2022版
- Xcode real machine debugging
- 经济学外文文献在哪查?
猜你喜欢
35 pages dangerous chemicals safety management platform solution 2022 Edition
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
Realization of mask recognition based on OpenCV
Pytorch里面多任务Loss是加起来还是分别backward?
Maybe you read a fake Tianlong eight
How much do you know about synchronized?
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
TypeError: Cannot read properties of undefined (reading ***)
[shutter] shutter open source project reference
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
随机推荐
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
1380. Lucky numbers in the matrix
附加:token;(没写完,别看…)
Is there a specific format for English papers?
Is the multitasking loss in pytoch added up or backward separately?
Pytorch 20 realizes corrosion expansion based on pytorch
How to set automatic reply for mailbox and enterprise mailbox?
How QT exports data to PDF files (qpdfwriter User Guide)
有哪些比较推荐的论文翻译软件?
yolov5detect. Py comment
Mutual exclusion and synchronization of threads
35页危化品安全管理平台解决方案2022版
Slf4j + logback logging framework
秒杀系统设计
JS interviewer wants to know how much you understand call, apply, bind no regrets series
Slf4j + Logback日志框架
教育学大佬是怎么找外文参考文献的?
MFC file operation
程序分析与优化 - 9 附录 XLA的缓冲区指派
Maybe you read a fake Tianlong eight