awkCommand:
BEGIN {
print ARGC;
for (k=0; k < ARGC; k++)
{
print "ARGV[" k "]=" ARGV[k];
}
}
{
print $0;
}
awkCommand2:
BEGIN{
print ARGC;
for(k=0; k<ARGC; k++)
{
print "ARGV[" k "]=" ARGV[k];
}
}
text:
Hello world!!
Amazing world!
./script_1:
#! /bin/bash
#Following is error statement, it will take the program
#as input file, and complain no such a file
echo "" | awk -v var1=1 -v var2=2 Hello world! var3=3 '{
print ARGC;
for (k=0; k < ARGC; k++)
{
print "ARGV[" k "]=" ARGV[k];
}
}'
#Following is also error statement, it will take Hello as the
#input file, and complain no such a file.
#Actually awk can take input from 2 sources, either standard
#input(pipe) or input files. If providing the input files, awk
#will ignore the input from pipe/starndard input.
#In this case, "Hello" "world!" and "var3=3" are taken as
#3 input files.
echo "" | awk -v var1=1 -v var2=2 '{
print ARGC;
for (k=0; k < ARGC; k++)
{
print "ARGV[" k "]=" ARGV[k];
}
}' Hello world! var3=3
#awk ignore the input from pipe, and choose the input
#from text file.
echo "Amazing New York" | awk '{
print $0;
}' text
#output:
#Hello world!!
#Amazing world!
#Following statement successfully read into the 3 options, but
#still take "Hello" as the input file name and complain no such
#a file.
echo "" | awk -v var1=1 -v var2=2 -f awkCommand Hello world! var3=3
#output:
#4
#ARGV[0]=awk
#ARGV[1]=Hello
#ARGV[2]=world!
#ARGV[3]=var3=3
#awk: cannot open Hello (No such file or directory)
#Following statement successfully read into the option, and
#successfully get the text's information
awk -v var1=1 -v var2=2 -f awkCommand text
#output:
#2
#ARGV[0]=awk
#ARGV[1]=text
#Hello world!!
#Amazing world!
#since awkCommand2 only has "BEGIN" action, and not
#having any "pattern/action" pair. In this case,
#"Hello", "world" will be taken as pure arguments,
#instead of input source file
awk -f awkCommand2 Hello world
#output:
#3
#ARGV[0]=awk
#ARGV[1]=Hello
#ARGV[2]=world
2. Environment Variables
When launching the awk script, awk will inherit the environment variable.
#! /bin/bash
awk 'BEGIN {
print ENVIRON["HOME"];
print ENVIRON["USER"];
#output:
#/home/aubinxia
#aubinxia
}'
No comments:
Post a Comment