text:
Hello world!
Hello Amazing world!
script_1:
#! /bin/bash
awk 'BEGIN {
getline < "/dev/tty";
#script stops here, waiting for input:
#Input:
#My Input
print "BEGIN:" $0;
print "BEGIN, NF, NR:",NF,NR;
#Output:
#My Input
#BEGIN:My Input
#BEGIN, NF, NR: 2 0
getline v < "/dev/tty";
#Input:
#My Own Input
#Since getline read input into variable v, instead of $0,
#so, NF is not touched.
print "BEGIN:" v;
print "BEGIN, NF, NR:", NF, NR;
#Output:
#My Own Input
#BEGIN:My Own Input
#BEGIN, NF, NR: 2 0
}
{ print $0; }' text
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
My Input
BEGIN:My Input
BEGIN, NF, NR: 2 0
My Own Input
BEGIN:My Own Input
BEGIN, NF, NR: 2 0
Hello world!
Hello Amazing world!
2. User Controlled Input: while loop
text:
Hello world!
Hello Amazing world!
text2:
Hello Chicago
Hello Los Angeles
Hello Boston
Hello Atlantic
script_1:
#! /bin/bash
awk 'BEGIN {
# getline returning 1 means "success"
# returning 0 means "end of file"
# returning -1 means error
while( getline < "text2" )
{
print "BEGIN:" $0;
}
}
{ print $0; }' text
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
BEGIN:Hello Chicago
BEGIN:Hello Los Angeles
BEGIN:Hello Boston
BEGIN:Hello Atlantic
Hello world!
Hello Amazing world!
3. User Controlled Input: from external command
text2:
Hello Chicago
Hello Los Angeles
Hello Boston
Hello Atlantic
script_1:
#! /bin/bash
awk 'BEGIN {
command="cat text2";
# Sice we do not close the command every time after
# "getline", so for each iteration, getline will
# read the next record compared to the one in the
# last iteration.
while((command | getline v)>0)
print v;
close(command);
}'
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
Hello Chicago
Hello Los Angeles
Hello Boston
Hello Atlantic
No comments:
Post a Comment