当前位置:网站首页>Basic use of shell script
Basic use of shell script
2022-07-03 00:25:00 【JunesFour】
shell Basic use
List of articles
Shell brief introduction
Shell It's a use. c A program written in a language , It is used by users Linux The bridge . Shell It's a command language , Another programming language .
Shell Script (shell script), It's for Shell Write the script . What the industry says Shell Usually it means Shell Script . Shell And Shell Script is two different concepts .
Linux Medium shell There are many kinds , Several commonly used :
- Bourne Shell (/usr/bin/sh or /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)
Because it's easy to use and free , Be in daily work
Widely used . meanwhile ,Bash And most of them Linux By default Shell.
first Shell Script
Create a first.sh
file ,Shell Script file to .sh ending
, Then start editing the file :
#!/bin/bash
echo "hello world"
Use after writing ll
Command to view the file :
only
Allow reading and writing , Cannot perform , So add executable permissions to it :
chmod +x first.sh
After seeing the file turn green , Use ./firsh.sh
perform :
Common grammar
Defining variables
#!/bin/bash
echo "hello world"
greet="hello program"
echo $greet
- Pay attention to defining variables ,
=
There must be no spaces on either side of the sign . - Later, when using variables , Add
$
Symbol .
if conditional
#!/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 loop
Suppose you want to print /opt/module
All the files under the directory :
#!/bin/bash
for file in $(ls /opt/module/); do
echo $file
done
from 1 Add to 100:
#!/bin/bash
sum=0
for i in {
1..100}; do
let sum+=i;
done
echo $sum
Check the working state of the machine in the LAN :
Suppose the network segment is 192.168.199
, It is required to print out the working status of all machines under the current network segment .
#!/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]
Express ping The number of times .ping -i [time]
Every time said ping Time interval of ./dev/null
It means to filter out some useless information printed .$?
Indicates the execution result of the previous line of instructions .
Use it later and then add .
边栏推荐
- Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
- [shutter] shutter open source project reference
- mm中的GAN模型架构
- Which websites can I search for references when writing a thesis?
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
- TypeError: Cannot read properties of undefined (reading ***)
- Slf4j + Logback日志框架
- Where can I find foreign papers?
- Unique line of "Gelu"
- Multiprocess programming (4): shared memory
猜你喜欢
Markdown使用教程
logback配置文件
Seckill system design
教育学大佬是怎么找外文参考文献的?
pod生命周期详解
TypeError: Cannot read properties of undefined (reading ***)
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
有哪些比较推荐的论文翻译软件?
Digital twin smart factory develops digital twin factory solutions
随机推荐
Multiprocess programming (V): semaphores
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
v8
Pytorch 20 realizes corrosion expansion based on pytorch
Is there a specific format for English papers?
Chapter 4 of getting started with MySQL: data types stored in data tables
CMake基本使用
经济学外文文献在哪查?
[MCU project training] eight way answering machine
Redis21 classic interview questions, extreme pull interviewer
Missing number
论文的英文文献在哪找(除了知网)?
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
[reading notes] phased summary of writing reading notes
LeedCode1480. Dynamic sum of one-dimensional array
Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
Markdown tutorial
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
Confluence的PDF导出中文文档异常显示问题解决
Interface automation coverage statistics - used by Jacobo