0

I'm recursively counting lines in files whose format is given in command line as a parameter, e.g. *.txt... In this example I'm searching for all .txt files and counting their lines. Additionally, I have to echo input parameters. My problem is with echo, when I try "$1", it expands and echoes all the .txt files, also with '$1' it echoes $1... What I want is not to expand and just echo raw input, in this case *.txt.

EDIT:

I'm calling the script with 2 parameters, first is the starting directory of recursion, and the second one is the format of desired file type.

./script.sh test *.txt

Then, I need to echo both of the parameters and recursively count the lines of the files whose format is given with 2nd parameter

echo $1
echo $2

find /$1 -name "$2" | wc -l

This code isn't working currently, but I'm trying to fix parameter echo first.

4

2 回答 2

2

如果你真的想禁用globing (这很奇怪),你可以设置:

set -o noglob

要将此选项设置回 off :

set +o noglob

但我不知道你真正想做什么,但我觉得你做错了。您应该考虑对上下文进行更多解释

于 2013-03-25T23:09:54.540 回答
1

没有办法仅针对您的特定脚本执行此操作。不要试图./script.sh foo *.txt按照你想要的方式工作。这不是 shell 的工作方式,也没有其他实用程序可以这样做。

相反,您应该以相同的方式find执行grep此操作:只需要求用户引用:

./script.sh foo '*.txt'

执行此操作时,您可以在 script.sh 中使用$2带引号将其保留为模式(如在您的find /$1 -name "$2" | wc -l, 或不带引号将其扩展为多个文件名,如在您的echo $2.

于 2013-03-25T23:23:18.237 回答