简体   繁体   中英

Parameter expansion bash

I'm recursively counting lines in files whose format is given in command line as a parameter, eg *.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.

If you really want to disable glob ing (this is odd), you can set :

set -o noglob

To set this option back to off :

set +o noglob

But I don't know what you are really trying to do, but I feel you do it the wrong way. You should consider giving more explanations on the context

There is no way to do this just for your particular script. Don't try to make ./script.sh foo *.txt work the way you want. It's not how shells works, and no other utilities do it.

Instead, you should do it the same way find and grep does it: simply require the user to quote:

./script.sh foo '*.txt'

When you do this, you can in script.sh use $2 quoted to keep it as a pattern (like in your find /$1 -name "$2" | wc -l , or unquoted to expand it into multiple filenames, as in your echo $2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM