Luka
Luka

Reputation: 37

Parameter expansion bash

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.

Upvotes: 0

Views: 128

Answers (2)

that other guy
that other guy

Reputation: 123640

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.

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185640

If you really want to disable globing (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

Upvotes: 2

Related Questions