shell脚本基本命令_Shell脚本初学者指南3:更多基本命令和链

shell脚本基本命令

shell脚本基本命令

banner-01

You’ve learned how to create scripts, use arguments, and build for loops. Now, let’s take a look at some more basic commands, text file manipulation, and redirecting input and output to files and other commands.

您已经了解了如何创建脚本,使用参数以及构建for循环。 现在,让我们看一些更基本的命令,文本文件操作以及将输入和输出重定向到文件和其他命令。

一些基本的有用命令 (Some Basic Useful Commands)

We’ve already given you a basic breakdown of shell scripts and an example-based outline of for loops, but be sure to check out those articles if you’ve missed our shell scripting guide thus far.

我们已经为您提供了Shell脚本基本分类for循环的基于示例的概述 ,但是如果您到目前为止尚未错过我们的Shell脚本指南,请务必查看这些文章。

The command-line is wonderful for many reasons, and redirection is one of the most prominent. If you had to make note of and reproduce the output of every command in order to take further action or use that for something else, then we’d all have gone crazy long ago. Redirection lets us use that output and save it or immediately use it as an input for another command. We can also use files as inputs for other commands.

命令行之所以出色,有很多原因,重定向是最突出的原因之一。 如果您必须记下并重现每个命令的输出以采取进一步的措施或将其用于其他用途,那么很久以前我们都会疯了。 重定向使我们可以使用该输出并保存它,或者立即将其用作另一个命令的输入。 我们还可以将文件用作其他命令的输入。

Before we go on, let’s cover some basic commands which can be of use in many different places.

在继续之前,让我们介绍一些可以在许多不同地方使用的基本命令。

echo – This command simply prints (displays) the entirety of its argument on the command-line as output

echo –此命令仅在命令行上打印(显示)整个参数作为输出

echo argument with spaces

带空格的echo参数

echo 1

As you can see, special characters need to be “escaped” so they are treated normally. This is done by using a backslash (\) in front of the character. It’s a better idea to use quotes. The echo command also works with variables.

如您所见,特殊字符需要“转义”,以便正常处理。 这是通过在字符前面使用反斜杠(\)来完成的。 使用引号是一个更好的主意。 echo命令也可用于变量。

echo 2

As you can see, single and double quotes behave differently. For more information, check out What’s the Difference Between Single and Double Quotes in the Bash Shell?

如您所见,单引号和双引号的行为不同。 有关更多信息,请查看Bash Shell中的单引号和双引号有什么区别?

cat – This command displays the contents of text files as output.

cat –此命令将文本文件的内容显示为输出。

cat file_to_be_read

猫file_to_be_read

Let’s say we create this text file in nano:

假设我们在nano中创建了此文本文件:

nano list

When we use the cat command on the file, we can see it’s output.

在文件上使用cat命令时,我们可以看到它的输出。

cat list

grep – This is one of the most powerful and useful commands available to you in Linux. It stands for Global/Regular Expression Print. It looks through a file and prints any line that matches a particular pattern. Because this pattern is based on “regular expression,” a concise line can yield a multitude of patterns to be matched. For not, though, you can enter a tern for searching.

grep –这是Linux中可用的最强大和有用的命令之一。 它代表全局/正则表达式打印。 它浏览文件并打印与特定模式匹配的任何行。 因为此模式基于“正则表达式”,所以简洁的行可以产生大量要匹配的模式。 但是,您可以输入一个“ tern”来进行搜索。

grep pattern file

grep模式文件

grep

I assure you, grep can do more, but for now let’s stick to the easier stuff.

我向您保证,grep可以做更多的事情,但是现在让我们坚持使用更简单的方法。

重定向输出 (Redirecting Outputs)

To redirect the output of a command to a file, we make use of a special character, the greater-than symbol (>).

要将命令的输出重定向到文件,我们使用特殊字符,即大于号(>)。

Let’s change our list up, shall we? Enter the following command:

让我们改变一下清单,好吗? 输入以下命令:

echo pepperoni > list

回声意大利辣味香肠>列表

echo gt list

You can see that echo doesn’t display the line anymore, and when we look at the contents of the “list” file, we see what we echoed in there.

您可以看到echo不再显示该行,并且当我们查看“列表”文件的内容时,我们将看到在其中回显的内容。

Also take note that the previous contents of “list” were removed. Try it again:

另请注意,“列表”的先前内容已删除。 再试一遍:

echo gt list 2

This can be useful when you want to reuse a file, but often we just want to add to an existing file. For this, we use two consecutive greater-than symbols:

当您想重用文件时,这很有用,但是通常我们只想添加到现有文件中。 为此,我们使用两个连续的大于号:

echo yellow peppers >> list

回声黄椒>>列表

echo gtgt list

Easy! Let’s use this command to create a larger list, shall we?

简单! 让我们使用此命令创建一个更大的列表,对吧?

echo gtgt list 2

There we go. I think you can see why so many geeks use the command-line to make to-do lists and the like, but it gets even better.

好了 我想您可以理解为什么这么多怪胎使用命令行制作待办事项列表之类的方法,但是情况变得更好。

Let’s take the output of a command and put it into a file:

让我们获取命令的输出并将其放入文件中:

ls –al / > ~/rootlist

ls –al />〜/ rootlist

rootlist

Making lists of files, editing them down, and then running commands on the ones you want has never been simpler. And, while we’re doing these basic functions in the command-line, these work well in scripts, too.

创建文件列表,进行编辑,然后在所需文件上运行命令从未如此简单。 而且,尽管我们在命令行中执行了这些基本功能,但它们在脚本中也能很好地工作。

管道或链接 (Piping, or Chaining)

Piping is so named because it uses the pipe, (| ; shared with the \ key on most keyboards). Essentially, it takes the output of one command and directly feeds it to another. You can create long chains of commands to get a very specific desired output this way, and it’s very convenient for commands like grep.

如此命名管道是因为它使用管道(|;在大多数键盘上与\键共享)。 本质上,它接受一个命令的输出,然后直接将其馈送到另一个命令。 您可以通过这种方式创建较长的命令链,以获得非常特定的所需输出,这对于诸如grep这样的命令非常方便。

pipe grep

It acts a lot like “>” except it can be chained multiple times and its effect is more general in that it doesn’t need to go through a text file.

它的作用与“>”非常相似,不同之处在于它可以被多次链接,并且其效果更为普遍,因为它不需要浏览文本文件。

As you can see, grep is case-sensitive. You can use the “-i” flag to make it ignore case.

如您所见,grep区分大小写。 您可以使用“ -i”标志使其忽略大小写。

grep non-case-sensitive

重定向输入 (Redirecting Inputs)

You can also take inputs from files for commands by using the less-than symbol (<).

您还可以使用小于号(<)从文件中获取命令输入。

cat < list

猫<清单

cat lt list

“That’s not any different from using an argument!” you might say. Well, you’d be correct in this case. Where redirection of input really comes in handy is in chaining commands together.

“这与使用参数没有什么不同!” 你可能会说。 好吧,在这种情况下你是正确的。 输入重定向的真正方便之处在于将命令链接在一起。

Let’s say we want to filter any word that has “pep” in it from our current “list” file into a new file called “revisions”.

假设我们要从当前“列表”文件中过滤出任何包含“ pep”的单词,并将其过滤到一个名为“修订”的新文件中。

grep pep < list > revisions

grep pep <列表>修订

input-output 1

Let’s redo this command, and add some sorting.

让我们重做此命令,并添加一些排序。

grep pep < list | sort > revisions

grep pep <列表| 排序>修订

input-output 2

This will use “pep” as the search term from the input file “list”, sort it in alphabetical order (all upper-case terms followed by all lower-case terms), then output it into the “revisions” file.

这将使用“ pep”作为输入文件“列表”中的搜索词,并按字母顺序排序(所有大写词,然后是所有小写词),然后将其输出到“修订”文件中。

To illustrate the sort command, let’s look at the following example:

为了说明sort命令,让我们看下面的示例:

sort -f

As you can see, adding the “-f” flag to the sort command allows you to ignore case. This makes it easy for us to alphabetize lines in text files and ignore capitalization when it doesn’t matter.

如您所见,将“ -f”标志添加到sort命令可以忽略大小写。 这使我们可以轻松地按字母顺序排列文本文件中的行,并在无关紧要时忽略大写。

一个简单的脚本 (A Simple Script)

Let’s create a script that has the following form:

让我们创建一个具有以下形式的脚本:

script searchterm listfile

脚本搜索词列表文件

script1

It will take the term and use grep to search through a list file, sort the results, and then output them to another file.

将使用术语并使用grep搜索列表文件,对结果进行排序,然后将其输出到另一个文件。

Here’s the directory that we’ll be testing the script in:

这是我们将在其中测试脚本的目录:

script3

And, we can create a list of what in here, then run the script.

并且,我们可以在此处创建列表,然后运行脚本。

script2

There you go! The more you learn the rules of regular expressions, the more accurately you can put together a search command. And, anything that is valid in quotes can be substituted for your first argument!

你去! 您对正则表达式的规则了解得越多,则可以更准确地组合搜索命令。 而且,任何用引号引起来的内容都可以替代您的第一个参数!

As far as sorting goes, you can do more than just sort alphabetically. Take a look at the man page for some of the following commands:

就排序而言,您不仅可以按字母顺序进行排序,还可以做更多的事情。 查看手册页,了解以下一些命令:

  • tsort – a more advanced topological sorting function

    tsort –更高级的拓扑排序功能
  • tr – lets you map specific characters to other characters, and transcribe between them.

    tr –使您可以将特定字符映射到其他字符,并在它们之间转录。
  • uniq – removes any non-unique (read: duplicate)

    uniq –删除所有非唯一(读取:重复)
  • awk – a really advanced text processing language/function that can be used to separate fields in filenames

    awk –一种非常高级的文本处理语言/功能,可用于分隔文件名中的字段
  • cut, paste/join – commands useful for isolating fields from text files and adding new data into columns

    剪切,粘贴/联接–用于从文本文件中隔离字段并将新数据添加到列中的命令
  • look – searches like grep does, but uses a dictionary file (that can be user-specified) for the lookup

    look –像grep一样进行搜索,但是使用字典文件(可以由用户指定)进行查找
  • wc – lets you get word count, line count, character count, and more

    wc –让您获得字数,行数,字符数等


We took a look at some more basics today that can be as useful on the command-line as in scripts. Text-based data is often at the heart of things we use daily, so being able to work with it, search it, and manipulate it is key.

今天,我们看了一些其他基础知识,这些基础知识在命令行和脚本中一样有用。 基于文本的数据通常是我们日常使用的核心内容,因此能够使用它,进行搜索和操作是关键。

What are some of your favorite scripts? Have any special-use scripts for text-based files? Share what you know in the comments!

您最喜欢什么脚本? 是否有用于基于文本的文件的特殊用途脚本? 在评论中分享您所知道的!

翻译自: https://www.howtogeek.com/69091/the-beginners-guide-to-shell-scripting-3-more-basic-commands-chains/

shell脚本基本命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值