Command line options in Shell Script?

I'm trying to enable this

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

in the shell script, but I want to parameterize 3000. However, since $1 already in use, I'm not sure what to do. It was a complete failure:

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

How to pass parameter instead of 3000 in the first script above?

+4
source share
4 answers

Single quotes forbid expansion, therefore:

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

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

 size="$1" du -s *| awk -v size="$size" '$1>size' 
+4
source
 size=$1 du -s *|awk '{ if ($1 > '$size') print }' 
+3
source

You can set awk variables on the command line:

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

Source: https://habr.com/ru/post/1302725/


All Articles