这个 bash 脚本代码行是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10411312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what does this bash script line of code means
提问by KItis
i am new to shell scripting and i found following line of code in a given script.
我是 shell 脚本的新手,我在给定的脚本中发现了以下代码行。
could someone explain me what does the following lines of codes means
有人可以解释一下以下几行代码是什么意思吗
_filecount=echo ${_filelist} | wc -w | awk '{ print $1 }'printk "archiveWLS(): ${_filecount} file(s)"
_filecount=echo ${_filelist} | wc -w | awk '{ print $1 }'printk "archiveWLS(): ${_filecount} file(s)"
Thanks in advance for any help
在此先感谢您的帮助
回答by user unknown
_filecount=echo ${_filelist} | wc -w | awk '{ print }'
wc -w: wc is word-count, wordcount counts words, lines, characters and so on, -w only outputs the word count.
wc -w:wc是word-count,wordcount统计单词、行、字符等,-w只输出单词计数。
The output is piped to awk, to print only the first word of output. If you use
输出通过管道传送到 awk,以仅打印输出的第一个单词。如果你使用
wc -w FILE
the filename would be printed, and it would make sense to strip it away with awk. Doing it with a piped output, where there is no filename, is superflous.
文件名将被打印出来,用 awk 将其剥离是有意义的。用没有文件名的管道输出来做它是多余的。
As assignment, the line should instead be:
作为分配,该行应该是:
_filecount=$(echo ${_filelist} | wc -w )
Without $(...) or backticks, it doesn't work, and filecount is just assigned the word "echo". Seems like a poorly written script.
没有 $(...) 或反引号,它不起作用,并且文件计数只是分配了“echo”这个词。看起来像一个写得很糟糕的脚本。
printk isn't defined in bash, nor is it a well established gnu programm from the coreutils.
printk 不是在 bash 中定义的,也不是来自 coreutils 的完善的 gnu 程序。
Maybe it is defined in the script itself.
也许它是在脚本本身中定义的。
回答by axiom
It stores the number of words in _filelist variable into _filecount. Elaboration :
它将_filelist 变量中的单词数存储到_filecount 中。细化:
_filecount=`echo ${_filelist} | wc -w | awk '{ print $1 }'`
you have to wrap it under `s,for this is the rule.
_filecount=` echo ${_filelist} | wc -w | awk '{ print $1 }'`
你必须将它包裹在 `s 下,因为这是规则。
echogives out what's stored in _filelistto wc.
echo将_filelist 中存储的内容提供给wc。
wc (word counter )receives it (this passing of intermediate result is called piping),and because it's invoked with a -woption, it already gives out only the number of words to awk,and keeps the count of lines and characters to itself.
wc(字计数器)接收它(这种中间结果的传递称为管道),并且因为它是用-w选项调用的,它已经只向awk提供字数,并将行数和字符数保留给自己.
awkin turn prints out first column of what's supplied to it ( awk '{ print $1 }'`).
awk依次打印出提供给它的内容的第一列( awk '{ print $1 }'`)。
wconly supplies 1 column here,i.e. the word count,so calling awkis redundant actually.
wc这里只提供 1 列,即字数,所以调用awk实际上是多余的。
printf "archiveWLS(): ${_filecount} file(s)"
printf "archiveWLS(): ${_filecount} file(s)"
substitutes the value of _filecount into the string and gives it out to the standard output stream,which happens to be your shell output window. That's it.
将 _filecount 的值替换为字符串并将其输出到标准输出流,这恰好是您的 shell 输出窗口。就是这样。
回答by sorpigal
What this script does is, essentially, nothing.
这个脚本所做的基本上什么都没有。
_filecount=echo ${_filelist} | wc -w | awk '{ print }'
This line is syntactically valid but not likely to be correct.
这一行在语法上是有效的,但不太可能是正确的。
Because echoappears where it does it is being interpreted as a temporary variable assignment that exists only for the duration of the following command, Thus, the environment variable _filecountwill have a value of echoduring the run of the command stored in the variable _filelist; since, I imagine, _filelistdoesn't contain a command name and, instead, contains the name of one or more non-executable files, you will get a command not founderror.
因为echo出现的地方它被解释为一个临时变量赋值,它只存在于以下命令的持续时间内,因此,环境变量在命令运行期间_filecount将具有echo存储在变量中的值_filelist;因为,我想,_filelist它不包含命令名称,而是包含一个或多个不可执行文件的名称,所以您会收到command not found错误消息。
The rest of the line is irrelevant. Since the ${_filelist}command output nothing to stdout, nothing was redirected to wc -w. wcwill report that 0 words werre counted and this value will be passed to awk, which will strip the leading whitespace.
该行的其余部分无关紧要。由于${_filelist}命令没有向 stdout 输出任何内容,因此没有任何内容被重定向到wc -w. wc将报告计算了 0 个单词,并且该值将传递给awk,这将去除前导空格。
What you probably meant to say was more like
你可能想说的更像是
_filecount=$(echo ${_filelist} | wc -w | awk '{ print }')
Which will execute the echopipeline and store the result in the _filecountvariable.
这将执行echo管道并将结果存储在_filecount变量中。
It isn't really a good idea to store a file list in a space-delimited string like this (for one thing, files can have spaces in their names), instead use a basharray, like this:
将文件列表存储在这样的以空格分隔的字符串中并不是一个好主意(一方面,文件的名称中可以有空格),而是使用bash数组,如下所示:
_filelist=("file1.txt" "file2.txt")
You can expand this to the list of files with "${_filelist[@]}", all properly quoted, and you can now obtain the count of files this way:
您可以使用 将其扩展到文件列表"${_filelist[@]}",全部正确引用,现在您可以通过以下方式获取文件数:
_filecount=${#_filelist[@]}
Which is much faster (executes in-process instead of calling external utilities) and more reliable (doesn't rely on "one file is one word" semantics).
哪个更快(在进程内执行而不是调用外部实用程序)并且更可靠(不依赖于“一个文件就是一个词”语义)。
As for this line:
至于这一行:
printk "archiveWLS(): ${_filecount} file(s)"
Since there is no bash builtin called printkand there is no standard utility called printk, the result is likely to be:
由于没有调用 bash 内置printk函数,也没有调用标准实用程序printk,结果很可能是:
-bash: printk: command not found
You could have used printffor this:
你可以用printf这个:
printf "archiveWLS(): ${_filecount} file(s)"
And the output would be
输出将是
archiveWLS(): 0 file(s)
Where 0is the value of _filecount.
0的值在哪里_filecount。
Although, it would be nicer to say:
虽然,最好说:
printf 'archiveWLS(): %s file(s)\n' "${_filecount}"
Which will output the same as the above, also include a newline at the end and be easier to maintain.
这将输出与上面相同的内容,并在末尾包含一个换行符,并且更易于维护。

