mirror of
https://github.com/LCTT/TranslateProject.git
synced 2026-08-23 04:03:29 +08:00
translated
This commit is contained in:
@@ -1,188 +0,0 @@
|
||||
[#]: subject: "Bash Basics Series #1: Create and Run Your First Bash Shell Script"
|
||||
[#]: via: "https://itsfoss.com/create-bash-script/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Bash Basics Series #1: Create and Run Your First Bash Shell Script
|
||||
======
|
||||
|
||||
This is the beginning of a new tutorial series on It's FOSS. In this one, you'll get familiar with bash scripting.
|
||||
|
||||
The series assumes that you are somewhat familiar with the Linux terminal. You don't have to be a master, but knowing the basics would be good. I advise going through Terminal Basics Series.
|
||||
|
||||
**Who is this series for?**
|
||||
|
||||
Anyone who wants to start learning bash shell scripting.
|
||||
|
||||
If you are a student with shell scripting as part of your course curriculum, this series is for you.
|
||||
|
||||
If you are a regular desktop Linux user, this series will help you understand most shell scripts you come across while exploring various software and fixes. You could also use it to automate some common, repetitive tasks.
|
||||
|
||||
By the end of this Bash Basics series, you should be able to write simple to moderate bash scripts.
|
||||
|
||||
All the chapters in the series have sample exercises so that you can learn it by doing it.
|
||||
|
||||
> 🚧 You'll learn bash shell scripting here. While there are other shells with mostly the same syntax, their behavior still differs at a few points. Bash is the most common and universal shell and hence start learning shell scripting with bash.
|
||||
|
||||
### Your first shell script: Hello World!
|
||||
|
||||
Open a terminal. Now [create a new directory][1] to save all the scripts you'll be creating in this series:
|
||||
|
||||
```
|
||||
mkdir bash_scripts
|
||||
```
|
||||
|
||||
Now [switch to this newly created directory][2]:
|
||||
|
||||
```
|
||||
cd bash_scripts
|
||||
```
|
||||
|
||||
Let's [create a new file][3] here:
|
||||
|
||||
```
|
||||
touch hello_world.sh
|
||||
```
|
||||
|
||||
Now, [edit the file][4] and add `echo Hello World` line to it. You can do this with the append mode of the cat command (using >):
|
||||
|
||||
```
|
||||
[email protected]:~/bash_scripts$ cat > hello_world.sh
|
||||
echo Hello World
|
||||
^C
|
||||
```
|
||||
|
||||
I prefer adding new lines while using the cat command for adding text.
|
||||
|
||||
Press Ctrl+C or Ctrl+D keys to come out of the append mode of the cat command. Now if you check the contents of the script `hellow_world.sh`, you should see just a single line.
|
||||
|
||||
![Creating first shell script][5]
|
||||
|
||||
The moment of truth has arrived. You have created your first shell script. It's time to [run the shell script][6].
|
||||
|
||||
Do like this:
|
||||
|
||||
```
|
||||
bash hello_world.sh
|
||||
```
|
||||
|
||||
The echo command simply displays whatever was provided to it. In this case, the shell script should output Hello World on the screen.
|
||||
|
||||
![Run first shell script][7]
|
||||
|
||||
Congratulations! You just successfully ran your first shell script. How cool is that!
|
||||
|
||||
Here's a replay of all the above commands for your reference.
|
||||
|
||||
![][8]
|
||||
|
||||
#### Another way to run your shell scripts
|
||||
|
||||
Most of the time, you'll be running the shell scripts in this manner:
|
||||
|
||||
```
|
||||
./hello_world.sh
|
||||
```
|
||||
|
||||
Which will result in an error because the file for you as the script doesn't have execute permission yet.
|
||||
|
||||
```
|
||||
bash: ./hello_world.sh: Permission denied
|
||||
```
|
||||
|
||||
Add execute permission for yourself to the script:
|
||||
|
||||
```
|
||||
chmod u+x hello-world.sh
|
||||
```
|
||||
|
||||
And now, you can run it like this:
|
||||
|
||||
```
|
||||
./hello_world.sh
|
||||
```
|
||||
|
||||
![Run shell scripts][9]
|
||||
|
||||
So, you learned two ways to run a shell script. It's time to focus on bash.
|
||||
|
||||
### Turn your shell script into a bash script
|
||||
|
||||
Confused? Actually, there are several shells available in Linux. Bash, ksh, csh, zsh and many more. Out of all these, bash is the most popular one and almost all distributions have it installed by default.
|
||||
|
||||
The shell is an interpreter. It accepts and runs Linux commands. While the syntax for most shell remains the same, their behavior may differ at certain points. For example, the handling of brackets in conditional logic.
|
||||
|
||||
This is why it is important to tell the system which shell to use to interpret the script.
|
||||
|
||||
When you used `bash hello_world.sh`, you explicitly used the bash interpreter.
|
||||
|
||||
But when you run the shell scripts in this manner:
|
||||
|
||||
```
|
||||
./hello_world.sh
|
||||
```
|
||||
|
||||
The system will use whichever shell you are currently using to run the script.
|
||||
|
||||
To avoid unwanted surprises due to different syntax handling, you should explicitly tell the system which shell script it is.
|
||||
|
||||
How to do that? Use the shebang (#!). Normally, # is used for comments in shell scripts. However, if #! is used as the first line of the program, it has the special purpose of telling the system which shell to use.
|
||||
|
||||
So, change the content of the hello_world.sh so that it looks like this:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
echo Hello World
|
||||
```
|
||||
|
||||
And now, you can run the shell script as usual knowing that the system will use bash shell to run the script.
|
||||
|
||||
![Run bash shell script][10]
|
||||
|
||||
> 💡 If you feel uncomfortable editing script files in the terminal, as a desktop Linux user, you can use Gedit or other GUI text editors for writing scripts and run them in the terminal.
|
||||
|
||||
### 🏋️ Exercise time
|
||||
|
||||
It is time to practice what you learned. Here are some basic practice exercises for this level:
|
||||
|
||||
- Write a bash script that prints "Hello Everyone"
|
||||
- Write a bash script that displays your current working directory (hint: use pwd command)
|
||||
- Write a shell script that prints your user name in the following manner: My name is XYZ (hint: use $USER)
|
||||
|
||||
The answers can be discussed in [this dedicated thread][11] in the Community forum.
|
||||
|
||||
The last practice exercise uses `$USER`. That's a special variable that prints the user name.
|
||||
|
||||
And that brings me to the topic of the next chapter in the Bash Basics Series: Variables.
|
||||
|
||||
Stay tuned for that next week.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/create-bash-script/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/make-directories/
|
||||
[2]: https://itsfoss.com/change-directories/
|
||||
[3]: https://itsfoss.com/create-files/
|
||||
[4]: https://itsfoss.com/edit-files-linux/
|
||||
[5]: https://itsfoss.com/content/images/2023/06/create-first-shell-script.png
|
||||
[6]: https://itsfoss.com/run-shell-script-linux/
|
||||
[7]: https://itsfoss.com/content/images/2023/06/run-first-shell-script.png
|
||||
[8]: https://itsfoss.com/content/images/2023/06/create-run-first-shell-script.svg
|
||||
[9]: https://itsfoss.com/content/images/2023/06/running-shell-scripts.png
|
||||
[10]: https://itsfoss.com/content/images/2023/06/run-bash-shell-script.png
|
||||
[11]: https://itsfoss.community:443/t/practice-exercise-in-bash-basics-series-1-create-and-run-your-first-bash-shell-script/10682
|
||||
@@ -0,0 +1,188 @@
|
||||
[#]: subject: "Bash Basics Series #1: Create and Run Your First Bash Shell Script"
|
||||
[#]: via: "https://itsfoss.com/create-bash-script/"
|
||||
[#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/"
|
||||
[#]: collector: "lkxed"
|
||||
[#]: translator: "geekpi"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
|
||||
Bash 基础知识系列 #1:创建并运行你的第一个 Bash Shell 脚本
|
||||
======
|
||||
|
||||
这是关于 It's FOSS 的新教程系列的开始。在这一篇中,你将熟悉 bash 脚本。
|
||||
|
||||
本系列假定你对 Linux 终端比较熟悉。你不必是大师,但了解基础知识会很好。我建议阅读终端基础知识系列。
|
||||
|
||||
**这个系列适合谁?**
|
||||
|
||||
任何想开始学习 bash shell 脚本的人。
|
||||
|
||||
如果你是一名将 shell 脚本作为课程的一部分的学生,那么本系列适合你。
|
||||
|
||||
如果你是普通的桌面 Linux 用户,本系列将帮助你了解在探索各种软件和修复程序时遇到的大多数 shell 脚本。你还可以使用它来自动执行一些常见的重复性任务。
|
||||
|
||||
到本 Bash 基础系列结束时,你应该能够编写简单到中等的 bash 脚本。
|
||||
|
||||
该系列的所有章节都有示例练习,你可以边做边学。
|
||||
|
||||
> 🚧 你将在这里学习 bash shell 脚本。虽然还有其他 shell 的语法基本相同,但它们的行为在某些方面仍然存在差异。Bash 是最常见和通用的 shell,因此开始使用 bash 学习 shell 脚本。
|
||||
|
||||
### 你的第一个 shell 脚本:Hello World!
|
||||
|
||||
打开一个终端。现在[创建一个新目录][1]来保存你将在本系列中创建的所有脚本:
|
||||
|
||||
```
|
||||
mkdir bash_scripts
|
||||
```
|
||||
|
||||
现在[切换到这个新创建的目录][2]:
|
||||
|
||||
```
|
||||
cd bash_scripts
|
||||
```
|
||||
|
||||
让我们在这里[创建一个新文件][3]:
|
||||
|
||||
```
|
||||
touch hello_world.sh
|
||||
```
|
||||
|
||||
现在,[编辑文件][4]并向其中添加 “echo Hello World” 行。你可以使用 cat 命令的追加模式(使用 >)执行此操作:
|
||||
|
||||
```
|
||||
[abhishek@itsfoss]:~/bash_scripts$ cat > hello_world.sh
|
||||
echo Hello World
|
||||
^C
|
||||
```
|
||||
|
||||
我更喜欢在使用 cat 命令添加文本时添加新行。
|
||||
|
||||
按 Ctrl+C 或 Ctrl+D 键退出 cat 命令的追加模式。现在,如果你查看脚本 `hellow_world.sh` 的内容,你应该只看到一行。
|
||||
|
||||
![Creating first shell script][5]
|
||||
|
||||
关键时刻来了。你已经创建了第一个 shell 脚本。是时候[运行 shell 脚本][6] 了。
|
||||
|
||||
这样做:
|
||||
|
||||
```
|
||||
bash hello_world.sh
|
||||
```
|
||||
|
||||
echo 命令只是显示提供给它的任何内容。在这种情况下,shell 脚本应该在屏幕上输出 Hello World。
|
||||
|
||||
![Run first shell script][7]
|
||||
|
||||
恭喜! 你刚刚成功运行了第一个 shell 脚本。多么酷啊!
|
||||
|
||||
以下是上述所有命令的重放,供你参考。
|
||||
|
||||
![][8]
|
||||
|
||||
#### 另一种运行 shell 脚本的方法
|
||||
|
||||
大多数时候,你将以这种方式运行 shell 脚本:
|
||||
|
||||
```
|
||||
./hello_world.sh
|
||||
```
|
||||
|
||||
这将产生错误,因为作为脚本的文件还没有执行权限。
|
||||
|
||||
```
|
||||
bash: ./hello_world.sh: Permission denied
|
||||
```
|
||||
|
||||
给脚本添加执行权限:
|
||||
|
||||
```
|
||||
chmod u+x hello-world.sh
|
||||
```
|
||||
|
||||
现在,你可以像这样运行它:
|
||||
|
||||
```
|
||||
./hello_world.sh
|
||||
```
|
||||
|
||||
![Run shell scripts][9]
|
||||
|
||||
因此,你学习了两种运行 shell 脚本的方法。是时候关注 bash 了。
|
||||
|
||||
### 把你的 shell 脚本变成 bash 脚本
|
||||
|
||||
感到困惑? 实际上,Linux 中有几种可用的 shell。Bash、ksh、csh、zsh 等等。其中,bash 是最受欢迎的,几乎所有发行版都默认安装了它。
|
||||
|
||||
shell 是一个解释器。它接受并运行 Linux 命令。虽然大多数 shell 的语法保持不变,但它们的行为在某些点上可能有所不同。例如,条件逻辑中括号的处理。
|
||||
|
||||
这就是为什么告诉系统使用哪个 shell 来解释脚本很重要。
|
||||
|
||||
当你使用 `bash hello_world.sh` 时,你明确地使用了 bash 解释器。
|
||||
|
||||
但是当你以这种方式运行 shell 脚本时:
|
||||
|
||||
```
|
||||
./hello_world.sh
|
||||
```
|
||||
|
||||
系统将使用你当前使用的任何 shell 来运行脚本。
|
||||
|
||||
为避免由于不同的语法处理而导致不必要的意外,你应该明确告诉系统它是哪个 shell 脚本。
|
||||
|
||||
怎么做? 使用 shebang (#!)。通常,# 用于 shell 脚本中的注释。但是,如果 #! 用作程序的第一行,它的特殊用途是告诉系统使用哪个 shell。
|
||||
|
||||
因此,更改 hello_world.sh 的内容,使其看起来像这样:
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
echo Hello World
|
||||
```
|
||||
|
||||
现在,你可以像往常一样运行 shell 脚本,因为你知道系统将使用 bash shell 来运行脚本。
|
||||
|
||||
![Run bash shell script][10]
|
||||
|
||||
> 💡 如果你觉得在终端中编辑脚本文件不方便,作为桌面 Linux 用户,你可以使用 Gedit 或其他 GUI 文本编辑器编写脚本并在终端中运行。
|
||||
|
||||
### 🏋️ 练习时间
|
||||
|
||||
是时候练习你学到的东西了。以下是该级别的一些基本练习:
|
||||
|
||||
- 编写一个打印 “Hello Everyone” 的 bash 脚本
|
||||
- 编写一个显示当前工作目录的 bash 脚本(提示:使用 pwd 命令)
|
||||
- 编写一个 shell 脚本,使用以下列方式打印你的用户名:My name is XYZ(提示:使用 $USER)
|
||||
|
||||
答案可以在社区论坛的[这个专门的帖子][11]中讨论。
|
||||
|
||||
最后一个练习使用 `$USER`。这是一个打印用户名的特殊变量。
|
||||
|
||||
这就引出了 Bash 基础系列下一章的主题:变量。
|
||||
|
||||
请继续关注下周的内容。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/create-bash-script/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lkxed][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lkxed/
|
||||
[1]: https://itsfoss.com/make-directories/
|
||||
[2]: https://itsfoss.com/change-directories/
|
||||
[3]: https://itsfoss.com/create-files/
|
||||
[4]: https://itsfoss.com/edit-files-linux/
|
||||
[5]: https://itsfoss.com/content/images/2023/06/create-first-shell-script.png
|
||||
[6]: https://itsfoss.com/run-shell-script-linux/
|
||||
[7]: https://itsfoss.com/content/images/2023/06/run-first-shell-script.png
|
||||
[8]: https://itsfoss.com/content/images/2023/06/create-run-first-shell-script.svg
|
||||
[9]: https://itsfoss.com/content/images/2023/06/running-shell-scripts.png
|
||||
[10]: https://itsfoss.com/content/images/2023/06/run-bash-shell-script.png
|
||||
[11]: https://itsfoss.community:443/t/practice-exercise-in-bash-basics-series-1-create-and-run-your-first-bash-shell-script/10682
|
||||
Reference in New Issue
Block a user