当前位置:网站首页>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~
边栏推荐
猜你喜欢

UE4 模型OnClick事件不生效的两种原因

(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)

基于STM32设计的UNO卡牌游戏(双人、多人对战)

微软 Win10 照片磁贴后的又一“刺客”,谷歌 Chrome 浏览器将在新标签页展示用户照片

程序员代码面试指南 CD15 生成窗口最大值数组

2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m

typescript27 - what about enumeration types

(Codeforce 757) E. Bash Plays with Functions

时时刻刻保持敬畏之心

开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
随机推荐
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
程序员代码面试指南 CD15 生成窗口最大值数组
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
Visual Studio提供的 Command Prompt 到底有啥用
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
罗技鼠标体验记录
25. 这三道常见的面试题,你有被问过吗?
(Codeforce 757)E. Bash Plays with Functions(积性函数)
LeetCode 1189. “气球” 的最大数量
Immutable
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
雪糕和轮胎
Optional parameters typescript19 - object
Message queue MySQL table for storing message data
FFmpeg 搭建本地屏幕录制环境
项目风险管理必备内容总结
PMP子过程定义总结
The Principle Of Percona Toolkit Nibble Algorithm
Excuse me, only primary key columns can be queried using sql in table storage. Does ots sql not support non-primary keys?