当前位置:网站首页>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/shells
The 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.sh
Enter inside to edit
#!/bin/bash
echo "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.sh
Next 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.sh
In 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.sh
The 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.sh
Then pwd, you will find that the path has not changed
What about the source
source test2.sh
At 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~
边栏推荐
猜你喜欢
随机推荐
【云原生之kubernetes实战】kubernetes集群的检测工具——popeye
PAT乙级 1001 害死人不偿命的(3n+1)猜想
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
API Design Notes: The pimpl trick
【目标检测】YOLOv7理论简介+实践测试
剑指 Offer 68 - II. 二叉树的最近公共祖先
MySQL-DML语言-数据库操作语言-insert-update-delete-truncate
UE4 从鼠标位置射出射线检测
RSA主要攻击方法
最新 955 不加班的公司名单
typescript27 - what about enumeration types
Message queue MySQL table for storing message data
7 行代码搞崩溃 B 站,原因令人唏嘘!
基于Arduino制作非接触式测温仪
Optional parameters typescript19 - object
【堆】小红的数组
UE4 模型OnClick事件不生效的两种原因
MySQL实践总结-
Article summary: the basic model of VPN and business types
High Numbers | 【Re-integration】Line Area Score 880 Examples