I explained the reasons why the awk
code you supplied won't work. You can choose to ignore my comments and continue to wonder why your code doesn't work. Trying to use shell variables in awk
and assume that shell variable expansions will work in awk
will never work the way you have used them because awk
is not bash
. After defining a variable in bash
to be the name of a file with:
read input
and then using:
awk -v input="$input" 'BEGIN { print "input contains: \"" input "\""}'
shows you how you can turn a shell variable into an awk
variable that can be used inside an awk
script. Note that in shell:
echo $VAR
prints the value of the string stored in the variable named VAR
(assuming that the string assigned to VAR
does not contain any <backslash> characters, does not start with a <hyphen> possibly following leading <space> and/or <tab> characters). (If those constraints are broken, the output produced by echo
varies from shell to shell and operating system to operating system.)
You get roughly the same output inside awk
with the awk
statement:
print VAR
if and only if VAR
is also an awk
variable containing the same string as the shell variable VAR
.
Using $1
in a shell script refers to the contents of the 1st command line argument passed to your shell script. Using $1
in an awk
script refers to the contents of the 1st field on the current record you are processing from the current file you are reading with awk
.
If the shell variable input
contains the string 5
and the awk
variable input
input contains the string 5
then in shell code $input
expands to the string 5
, but in awk
code $input
expands to the string that is the contents of field number 5 in the current input line in the current input file.
In a directory where I have hundreds of files and the first file in the directory (sorted alphanumerically) is named 1999_08-09.sum
, the shell command
echo$(ls)
produces the output:
bash: echo1999_08-09.sum: command not found
because the shell didn't find a utility named echo1999_08-09.sum
after concatenating the output produced by the command substitution $(ls)
with the string echo
.
The same code in awk
(when there is no variable in awk
named echo
and no variable in awk
named ls
with your version of awk
expands to $(0)
which awk
treats as the contents of the current input line from the current input file. This obviously has absolutely nothing to do with the name of a file stored in a shell variable.
You can't mix random shell statements and random awk
statements and assume that that mix will magically be interpreted the way you want it to be interpreted.
The shell command language and the awk
command language are not the same no matter how much you want that to be true. If you want to use shell
variables in an awk
script you have to create an awk
variable that contains the contents of that shell variable. If you want to run shell commands inside an awk
script, you have to learn the awk
commands that can be used to do that (and the awk
you have shown us doesn't make any attempt to do so).
BSD, Linux, and UNIX systems provide you with hundreds of tools you can use to do all sorts of wondrous things. But you have to first learn that those tools only fit together in certain ways. And, there are certain tools that are extremely good at doing one thing and extremely poor at doing other things. And, using an awk
if
statement to determine if a string assigned to a shell variable names an existing file is an extreme case of using the wrong tool to try to do the job.
I wish you luck in your adventures, but I sincerely hope you'll take a closer look at the (entirely) bash
script I gave you that seems to do what I think you were trying to do. Trying to use awk
to determine whether or not there is a file of a certain name in the current working directory is enormously more difficult than learning to use the test
utility that is available in all shells based on Bourne shell syntax (e.g., bash
, dash
, ksh
, sh
, and zsh
).
You could also use [[ expression ]]
instead of test expression
or [ expression ]
, in some versions of bash
and some versions of ksh
, but I strongly suggest that you learn the basics before trying to use shell specific features that work well in some cases but not in others.