This is my shell script that receives a string as an input from the user from the stdin.
#!bin/sh
printf "Enter your query\n"
read query
cmd=`echo $query | cut -f 1 -d " "`
input_file=`echo $query | cut -f 2 -d " "`
printf $input_file
if [ $input_file = "" ]
then
printf "No input file specified\n"
exit
fi
if [ $cmd = "summary" ]
then
awk -f summary.awk $input_file $query
fi
Lets say he enters
summary a.txt /o foo.txt
Now cmd variable will take the value summary and input_file will take a.txt.
Isn’t that right?
I want summary.awk to work on $input_file, based on what is present in $query.
My understanding is as follows :
-
The 1st command line argument passed is treated as input file.
e.g. :awk 'code' file arg1 arg2 arg3
onlyfileis treated as input file -
If the input file is piped, it doesn’t see any of the arguments as input files.
e.g. :cat file | awk 'code' arg1 arg2 arg3
arg1is NOT treated as input file.
Am I right?
The problem is
I get awk: cannot open summary (No such file or directory)
Why is it trying to open summary?
It is the next word after $input_file.
How do I fix this issue?
If there’s no
-foption, the first non-option argument is treated as the script, and the remaining arguments are input files. If there’s a-foption, the script comes from that file, and the rest are input files.If and only if there are no input file arguments, it reads the input from stdin. This means if you pipe to awk and also give it filename arguments, it will ignore the pipe.
This is the same as most Unix file-processing commands.
Try this:
This will set the awk variable
queryto the contents of query.