当前位置:网站首页>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表示把打印的一些无用信息过滤掉.$?表示上一行指令的执行结果.
后面使用到再进行追加.
边栏推荐
- Analyze ad654: Marketing Analytics
- Leetcode relaxation question - day of the week
- Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
- JVM foundation review
- Which software can translate an English paper in its entirety?
- Optimization of streaming media technology
- 判断二叉树是否为满二叉树
- [target detection] r-cnn, fast r-cnn, fast r-cnn learning
- How to apply for company email when registering in company email format?
- The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
猜你喜欢

监控容器运行时工具Falco

MySQL advanced learning notes (III)

35页危化品安全管理平台解决方案2022版
![MATLAB signal processing [Q & a notes-1]](/img/53/ae081820fe81ce28e1f04914678a6f.png)
MATLAB signal processing [Q & a notes-1]

Digital collection trading website domestic digital collection trading platform

Architecture: database architecture design
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

Many to one, one to many processing

Xcode real machine debugging

流媒体技术优化
随机推荐
Question e: merged fruit -noip2004tgt2
Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
Chapter 4 of getting started with MySQL: data types stored in data tables
ArrayList analysis 2: pits in ITR, listiterator, and sublist
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
67页新型智慧城市整体规划建设方案(附下载)
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
leetcode 650. 2 keys keyboard with only two keys (medium)
大学生课堂作业2000~3000字的小论文,标准格式是什么?
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Define MySQL function to realize multi module call
返回二叉树两个节点间的最大距离
AcWing_ 188. Warrior cattle_ bfs
附加:token;(没写完,别看…)
Leetcode DP three step problem
Interface difference test - diffy tool
RTP 接发ps流工具改进(二)
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)