Dan Rosenstark
Dan Rosenstark

Reputation: 69747

Command-line parameters in Shell Script?

I am trying to include this

du -s *|awk '{ if ($1 > 3000) print }'

in a shell script, but I want to parameterize the 3000. However, since the $1 is already being used, I'm not sure what to do. This was a total failure:

size=$1
du -s *|awk '{ if ($1 > $size) print }'

How can I pass a parameter in place of 3000 in the first script above?

Upvotes: 4

Views: 850

Answers (4)

mouviciel
mouviciel

Reputation: 67831

You can set awk variables on its command line:

du -s * | awk '{ if ($1 > threshold) print }' threshold=$1

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342273

when passing shell variables to awk, try to use the -v option of awk as much as possible. This will be "cleaner" than having quotes all around

size="$1"
du -s *| awk -v size="$size" '$1>size'

Upvotes: 4

a'r
a'r

Reputation: 36989

size=$1
du -s *|awk '{ if ($1 > '$size') print }'

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

Single quotes inhibit expansion, so:

du -s *|awk '{ if ($1 > '"$1"') print }'

Upvotes: 3

Related Questions