当前位置:网站首页>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/nullIt means to filter out some useless information printed .$?Indicates the execution result of the previous line of instructions .
Use it later and then add .
边栏推荐
- [shutter] shutter open source project reference
- 開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
- Redis21 classic interview questions, extreme pull interviewer
- Multi process programming (III): message queue
- Interface automation coverage statistics - used by Jacobo
- Maya fishing house modeling
- Digital twin visualization solution digital twin visualization 3D platform
- Custom throttling function six steps to deal with complex requirements
- 秒杀系统设计
- Markdown tutorial
猜你喜欢

Where can I find the English literature of the thesis (except HowNet)?

Interface difference test - diffy tool

logback配置文件

What website can you find English literature on?

Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect

Seckill system design

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

布隆过滤器

MySQL 23道经典面试吊打面试官

CMake基本使用
随机推荐
What are the projects of metauniverse and what are the companies of metauniverse
kubernetes编写yml简单入门
Markdown tutorial
FRP reverse proxy +msf get shell
collections. What is the purpose of chainmap- What is the purpose of collections. ChainMap?
redis21道经典面试题,极限拉扯面试官
Program analysis and Optimization - 9 appendix XLA buffer assignment
DotNet圈里一个优秀的ORM——FreeSql
数组常用操作方法整理(包含es6)及详细使用
How do educators find foreign language references?
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
NC17059 队列Q
字符设备注册常用的两种方法和步骤
NC24840 [USACO 2009 Mar S]Look Up
Multiprocess programming (4): shared memory
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
LeedCode1480.一维数组的动态和
国外的论文在那找?
Using tensorflow to realize voiceprint recognition
多进程编程(一):基本概念