当前位置:网站首页>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 .
边栏推荐
- 在线预览Word文档
- Is there a specific format for English papers?
- Array de duplication
- Andorid gets the system title bar height
- pageoffice-之bug修改之旅
- Hit the industry directly! The propeller launched the industry's first model selection tool
- SQL query statement parameters are written successfully
- redis21道经典面试题,极限拉扯面试官
- Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
- MySQL advanced learning notes (4)
猜你喜欢

字符设备注册常用的两种方法和步骤

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

哪些软件可以整篇翻译英文论文?

Should you study kubernetes?

Cmake basic use

mm中的GAN模型架构

监控容器运行时工具Falco

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

Confluence的PDF导出中文文档异常显示问题解决
随机推荐
Sysdig analysis container system call
Chapter 4 of getting started with MySQL: data types stored in data tables
redis21道经典面试题,极限拉扯面试官
【单片机项目实训】八路抢答器
Pytorch里面多任务Loss是加起来还是分别backward?
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
zhvoice
Multiprocess programming (4): shared memory
Seckill system design
MySQL 23 classic interview hanging interviewer
Digital twin visualization solution digital twin visualization 3D platform
SQL query statement parameters are written successfully
Which websites can I search for references when writing a thesis?
数组常用操作方法整理(包含es6)及详细使用
监控容器运行时工具Falco
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
Xcode real machine debugging
Hit the industry directly! The propeller launched the industry's first model selection tool
程序分析与优化 - 9 附录 XLA的缓冲区指派
NC20806 区区区间间间