当前位置:网站首页>I met a shell script
I met a shell script
2022-08-01 04:51:00 【Looking for the BUG master】
Foreword: Now more and more individuals and enterprises are using the linux operating system, so some basic commands are understood.So if you want to reduce manual input, can you?The answer is yes.What method to use? The answer is to write a script. The most convenient is the shell script that comes with Linux.Next, follow me, recognize, learn and use the shell to achieve basic operations, let's get started!
Since it is a script, there must be an interpreter.There are many shell interpreters, we can view it with the following command
cat /etc/shellsThe result is shown below

We can use one of them. How to use it will be mentioned later
Begin editing the script below and specify the interpreter
Using commands
vi test1.shEnter inside to edit
#!/bin/bashecho "hello world"Where #!/bin/bash is the role of the specified interpreter
You can execute it, but you must give this file an execute permission before executing it
chmod +x test1.shNext execution
./test1.sh
This is the simplest shell script, just output hello world
In the future, you may come into contact more with $, so I will list some common combinations and meanings of it here, and I will use them when I encounter them in the future
$0: The shell's command itself
$1 to 9: the first parameter of Shell
$? : show the execution of the last command
$#: the number of arguments passed to the script
$$: The current process ID number of the script running
$*: Displays all parameters passed to the script as a single string
$!: ID number of the last process running in the background
$-: Displays the current options used by the shell
You can try to tap these commands to output, I will show you here first
echo $0
Some questions and answers
Q: Can I not specify an interpreter
Answer: Yes, if you don't specify it, then the default interpreter will be executed, that is, /bin/bash
Q: What is the default interpreter
Answer: You can use the following command
echo $SHEL
Q: Is there any other implementation method
Answer: Yes, there are two other ways, everyone can follow their own habits
The first is to execute the interpreter, and the file name is used as a parameter
/bin/bash test1.shIn this way, there is no need to specify the interpreter in the file, because the interpreter is mainly executed
The second is the source command
source test1.shThe above two execution methods do not need to assign an execution permission to the script, because the execution is not the file itself
Q: Is there any difference between these execution methods?
Answer: There is a difference. The source word can be understood as a sequential execution command. Now the final result is displayed. At the same time, the directory is the directory where the final execution is located. The other two words can be understood in this way. A new process is started.The current and execution environment is independent and has no effect on the current. You can take an example to see it
Create a new test2.sh file, and then the content is
cd /cd ~
Direct interpreter execution
sh test2.shThen pwd, you will find that the path has not changed
What about the source
source test2.shAt this time, you will find that the path has changed, and the difference is already very clear
Q: Does my suffix have to be .sh?
Answer: No, in linux, all files do not have the concept of suffix name, they can have any name, the reason why we do this is to facilitate viewing and understanding
The above is about the basic content of shell scripting, and it will be updated continuously~
边栏推荐
- 在互联网时代,有诸多「互联网+」模式的诞生
- 【愚公系列】2022年07月 Go教学课程 023-Go容器之列表
- [target detection] YOLOv7 theoretical introduction + practical test
- Mysql基础篇(约束)
- 请问表格储存中用sql只能查询到主键列,ots sql非主键不支持吗?
- PMP 80个输入输出总结
- (2022牛客多校四)H-Wall Builder II(思维)
- 56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
- (Codeforce 757)E. Bash Plays with Functions(积性函数)
- PMP工具与技术总结
猜你喜欢

UE4 rays flashed from mouse position detection

y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)

typescript28 - value of enumeration type and data enumeration

时时刻刻保持敬畏之心

博客系统(完整版)

Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink

typescript24-类型推论

Excel record of integer programming optimization model to solve the problem

mysql中解决存储过程表名通过变量传递的方法

基于STM32设计的UNO卡牌游戏(双人、多人对战)
随机推荐
数组问题之《两数之和》以及《三数之和 》
解决ffmpeg使用screen-capture-recorder录屏,有屏幕缩放的情况下录不全的问题
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
LeetCode 27. 移除元素
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
EntityFramework saves to SQLServer decimal precision is lost
PAT乙级 1002 写出这个数
请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
typescript22-接口继承
MySQL-数据定义语言-DDLdatebase define language
UE4 制作遇到的问题
时时刻刻保持敬畏之心
The kernel's handling of the device tree
动态规划 01背包
mysql中解决存储过程表名通过变量传递的方法
状态压缩dp
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data