读入bash脚本并将参数传递给脚本

时间:2020-11-28 23:20:12

I have a question. Unfortunately, I didn't find any answer. How can I pass arguments to script which are result of another command. For example:

我有个问题。不幸的是,我没有找到任何答案。如何将参数传递给脚本,这是另一个命令的结果。例如:

 ls | ./myscript.sh

I want to pass the result of ls to myscript. And if I execute the above command and the script is :

我想将ls的结果传递给myscript。如果我执行上面的命令,脚本是:

#!/bin/bash
read some
for arg in $@
do 
     grep $some $arg
done

then it didn't wait to read the some variable and the variable some gets its value from the result passed from ls command? How this actually works ? I want the following: To put to the script names of files (with command ls), after that the user to enter some string and I want to print every line which contain the entered word. I didn't know why the code above didn't work. Thanks in advance.

然后它没有等待读取某个变量,变量有些从ls命令传递的结果中得到它的值?这实际上有用吗?我想要以下内容:将脚本名称放入文件(使用命令ls),之后用户输入一些字符串,我想打印包含输入单词的每一行。我不知道为什么上面的代码不起作用。提前致谢。

2 个解决方案

#1


xargs is the utility to use for converting stdin input to command-line arguments.

xargs是用于将stdin输入转换为命令行参数的实用程序。

Naively, you could do the following:

天真地,你可以做到以下几点:

ls | xargs ./myscript.sh

However, that will not work as expected with filenames that have embedded spaces, as they will be split into multiple arguments.
Note that ./myscript.sh $(ls) has the same problem.

但是,对于具有嵌入空格的文件名,这将无法正常工作,因为它们将被拆分为多个参数。请注意./myscript.sh $(ls)具有相同的问题。

If your xargs implementation supports the nonstandard -0 option for parsing NUL-separated input, you can fix this as follows:

如果您的xargs实现支持非标准-0选项来解析NUL分隔的输入,您可以按如下方式解决此问题:

printf '%s\0' * | xargs -0 ./myscript.sh

Otherwise, use the following, which, however, will only work if no filenames have embedded " characters (or embedded newlines):

否则,请使用以下命令,但是,只有在没有文件名嵌入“字符(或嵌入的换行符)”时才会起作用:

printf '"%s" ' * | xargs ./myscript.sh

Since your stdin input comes from filenames in this case, you can use find, which essentially has xargs functionality built in:

由于在这种情况下你的stdin输入来自文件名,你可以使用find,它本质上内置了xargs功能:

find . -type f -maxdepth 1 -exec ./myscript.sh {} +
  • Note that find . -type f -maxdepth 1 in essence does the same as ls, but there are subtle differences, notably that each matching filename will be prefixed with ./ and that the filenames may not be sorted.
  • 注意找到。 -type f -maxdepth 1本质上与ls相同,但存在细微差别,特别是每个匹配的文件名将以./为前缀,并且文件名可能不会被排序。

  • -exec ./myscript.sh {} + invokes your script with as many filenames ({}) as can fit on a single command line (+) - just like xargs does - which are typically all of them.
  • -exec ./myscript.sh {} +使用尽可能多的文件名({})调用您的脚本,就像在单个命令行(+)上一样 - 就像xargs一样 - 通常都是它们的全部。


Note that both xargs and find ... -exec ... + could result in multiple invocations of the specified command, if not all arguments fit on a single command line.
However, given how long command lines are allowed to be on modern platforms, this will rarely happen - it only happens if you have a huge number of files in your directory and/or their names are really long.

请注意,如果不是所有参数都适合单个命令行,则xargs和find ... -exec ... +都可能导致多次调用指定的命令。但是,考虑到允许在现代平台上使用命令行多长时间,这种情况很少发生 - 只有当您的目录中有大量文件和/或它们的名称非常长时才会发生这种情况。

#2


You can run a command like this:

您可以运行如下命令:

./myscript.sh $(ls)

This will prompt the user for the some variable, and grep through the results of ls for files that contain the value entered by the user.

这将提示用户输入某个变量,并通过ls的结果grep查看包含用户输入值的文件。

However, you are likely to run into problems with this approach, since spaces and other characters can cause your for loop to receive invalid input.

但是,您可能会遇到此方法的问题,因为空格和其他字符可能导致for循环接收无效输入。

With that in mind, you might also want to look into find and xargs to achieve something similar, depending on how complex your script is. For example:

考虑到这一点,您可能还需要查看find和xargs以实现类似的功能,具体取决于脚本的复杂程度。例如:

find . -type f -print0 |xargs -0 grep "test"

找 。 -type f -print0 | xargs -0 grep“test”

#1


xargs is the utility to use for converting stdin input to command-line arguments.

xargs是用于将stdin输入转换为命令行参数的实用程序。

Naively, you could do the following:

天真地,你可以做到以下几点:

ls | xargs ./myscript.sh

However, that will not work as expected with filenames that have embedded spaces, as they will be split into multiple arguments.
Note that ./myscript.sh $(ls) has the same problem.

但是,对于具有嵌入空格的文件名,这将无法正常工作,因为它们将被拆分为多个参数。请注意./myscript.sh $(ls)具有相同的问题。

If your xargs implementation supports the nonstandard -0 option for parsing NUL-separated input, you can fix this as follows:

如果您的xargs实现支持非标准-0选项来解析NUL分隔的输入,您可以按如下方式解决此问题:

printf '%s\0' * | xargs -0 ./myscript.sh

Otherwise, use the following, which, however, will only work if no filenames have embedded " characters (or embedded newlines):

否则,请使用以下命令,但是,只有在没有文件名嵌入“字符(或嵌入的换行符)”时才会起作用:

printf '"%s" ' * | xargs ./myscript.sh

Since your stdin input comes from filenames in this case, you can use find, which essentially has xargs functionality built in:

由于在这种情况下你的stdin输入来自文件名,你可以使用find,它本质上内置了xargs功能:

find . -type f -maxdepth 1 -exec ./myscript.sh {} +
  • Note that find . -type f -maxdepth 1 in essence does the same as ls, but there are subtle differences, notably that each matching filename will be prefixed with ./ and that the filenames may not be sorted.
  • 注意找到。 -type f -maxdepth 1本质上与ls相同,但存在细微差别,特别是每个匹配的文件名将以./为前缀,并且文件名可能不会被排序。

  • -exec ./myscript.sh {} + invokes your script with as many filenames ({}) as can fit on a single command line (+) - just like xargs does - which are typically all of them.
  • -exec ./myscript.sh {} +使用尽可能多的文件名({})调用您的脚本,就像在单个命令行(+)上一样 - 就像xargs一样 - 通常都是它们的全部。


Note that both xargs and find ... -exec ... + could result in multiple invocations of the specified command, if not all arguments fit on a single command line.
However, given how long command lines are allowed to be on modern platforms, this will rarely happen - it only happens if you have a huge number of files in your directory and/or their names are really long.

请注意,如果不是所有参数都适合单个命令行,则xargs和find ... -exec ... +都可能导致多次调用指定的命令。但是,考虑到允许在现代平台上使用命令行多长时间,这种情况很少发生 - 只有当您的目录中有大量文件和/或它们的名称非常长时才会发生这种情况。

#2


You can run a command like this:

您可以运行如下命令:

./myscript.sh $(ls)

This will prompt the user for the some variable, and grep through the results of ls for files that contain the value entered by the user.

这将提示用户输入某个变量,并通过ls的结果grep查看包含用户输入值的文件。

However, you are likely to run into problems with this approach, since spaces and other characters can cause your for loop to receive invalid input.

但是,您可能会遇到此方法的问题,因为空格和其他字符可能导致for循环接收无效输入。

With that in mind, you might also want to look into find and xargs to achieve something similar, depending on how complex your script is. For example:

考虑到这一点,您可能还需要查看find和xargs以实现类似的功能,具体取决于脚本的复杂程度。例如:

find . -type f -print0 |xargs -0 grep "test"

找 。 -type f -print0 | xargs -0 grep“test”