shell脚本学习指南_Shell脚本初学者指南:基础知识

Shell脚本是Linux用户提升效率的关键工具,它允许您使用已知的命令进行编程,实现批处理操作,如文件管理、自动化备份等。本文介绍了Shell脚本的基础知识,包括什么是Shell脚本、编写脚本前的准备、脚本权限和命名、编写准则,以及编写您的第一个脚本。通过学习和实践,您可以更深入地掌握命令行并提高生产力。
摘要由CSDN通过智能技术生成
shell脚本学习指南

shell脚本学习指南

banner-01

The term “shell scripting” gets mentioned often in Linux forums, but many users aren’t familiar with it. Learning this easy and powerful programming method can help you save time, learn the command-line better, and banish tedious file management tasks.

“ shell脚本”一词在Linux论坛中经常被提及,但是许多用户对此并不熟悉。 学习这种简单而强大的编程方法可以帮助您节省时间,更好地学习命令行并消除繁琐的文件管理任务。

什么是Shell脚本? (What Is Shell Scripting?)

Being a Linux user means you play around with the command-line. Like it or not, there are just some things that are done much more easily via this interface than by pointing and clicking. The more you use and learn the command-line, the more you see its potential. Well, the command-line itself is a program: the shell. Most Linux distros today use Bash, and this is what you’re really entering commands into.

成为Linux用户意味着您需要使用命令行。 不管喜欢与否,通过此界面完成的某些事情要比指向和单击要容易得多。 您使用和学习命令行的次数越多,您看到的命令行的潜力就越大。 好吧,命令行本身就是一个程序:shell。 如今,大多数Linux发行版都使用Bash,而这正是您真正在其中输入命令的地方。

Now, some of you who used Windows before using Linux may remember batch files. These were little text files that you could fill with commands to execute and Windows would run them in turn. It was a clever and neat way to get some things done, like run games in your high school computer lab when you couldn’t open system folders or create shortcuts. Batch files in Windows, while useful, are a cheap imitation of shell scripts.

现在,在使用Linux之前使用Windows的某些人可能会记住批处理文件。 这些都是小的文本文件,您可以在其中填充要执行的命令,Windows会依次运行它们。 这是完成某些事情的一种巧妙而巧妙的方法,例如当您无法打开系统文件夹或创建快捷方式时,在高中计算机实验室中运行游戏。 Windows中的批处理文件虽然有用,但可以廉价地模仿Shell脚本。

cbr script

Shell scripts allow us to program commands in chains and have the system execute them as a scripted event, just like batch files. They also allow for far more useful functions, such as command substitution. You can invoke a command, like date, and use it’s output as part of a file-naming scheme. You can automate backups and each copied file can have the current date appended to the end of its name. Scripts aren’t just invocations of commands, either. They’re programs in their own right. Scripting allows you to use programming functions – such as ‘for’ loops, if/then/else statements, and so forth – directly within your operating system’s interface. And, you don’t have to learn another language because you’re using what you already know: the command-line.

Shell脚本使我们可以对链中的命令进行编程,并使系统将其作为脚本事件执行,就像批处理文件一样。 它们还允许使用更多有用的功能,例如命令替换。 您可以调用日期之类的命令,并将其输出用作文件命名方案的一部分。 您可以自动执行备份,每个复制的文件都可以在其名称的末尾附加当前日期。 脚本也不只是命令的调用。 它们本身就是程序。 脚本允许您直接在操作系统界面内使用编程功能,例如“ for”循环,if / then / else语句等。 而且,您不必学习其他语言,因为您使用的是已知的命令行。

That’s really the power of scripting, I think. You get to program with commands you already know, while learning staples of most major programming languages. Need to do something repetitive and tedious? Script it! Need a shortcut for a really convoluted command? Script it! Want to build a really easy to use command-line interface for something? Script it!

我认为,这确实是脚本编写的力量。 在学习大多数主要编程语言的基础知识的同时,您可以使用已知的命令进行编程。 需要做一些重复且乏味的事情吗? 编写脚本! 需要真正复杂的命令的快捷方式? 编写脚本! 是否想为某个东西构建一个真正易于使用的命令行界面? 编写脚本!

在你开始之前 (Before You Begin)

Before we begin our scripting series, let’s cover some basic information. We’ll be using the bash shell, which most Linux distributions use natively. Bash is available for Mac OS users and Cygwin on Windows, too. Since it’s so universal, you should be able to script regardless of your platform. In addition, so long as all of the commands that are referenced exist, scripts can work on multiple platforms with little to no tweaking required.

在开始编写脚本系列之前,让我们介绍一些基本信息。 我们将使用bash shell,大多数Linux发行版都使用bash shell。 Bash适用于Mac OS用户和Windows上的Cygwin。 由于它是如此通用,因此无论平台如何,您都应该能够编写脚本。 此外,只要所引用的所有命令都存在,脚本就可以在多个平台上运行,而几乎不需要调整。

Scripting can easily make use of “ad

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值