[SOLVED] Can we create a new argument to a gawk program command-line?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can we create a new argument to a gawk program command-line?
I have a gawk script called prog that I made self-executable.
usage is:
Code:
prog <input>
I can use 2 kinds of input files: file containing letters or file containing numbers.
If I use letter input, prog will do the task 1
If I use number input, prog will do a different task 2.
Is it possible to add an option to the command-line specifying what kind of input file I am gonna use, which thus tell prog what task to execute?
And also a -h option showing the usage of prog?
#!/usr/bin/awk -f
BEGIN{
if( ARGC != 3 )
usage()
switch(ARGV[1]){
case "-let":
print "we have a letters file"
break
case "-num":
print "we have a numbers file"
break
default:
usage()
}
ARGV[1]=""
}
1
function usage( e)
{
e = "Usage: script_name -[let|num] file"
print e > "/dev/stderr"
exit 1
}
The issue that you have is, if any switch is passed to your script that (g)awk understands, it will be used as a switch to it instead of your program, namely '-h'.
The only way around this that I know of is to always pass '--' prior to any of your options.
Here is the detail around dealing with the arguments yourself.
@pan64: It was just a general question. It was in my plans to create arguments to the command line, but until now I didn't know it was even possible, that's why I asked.
@grail: I didn't expect so much. And I learned a new statement. Thank you very much !
* When the program executed properly, "Done !" displays to the terminal (to do that I used "END{print "Done !"}).
But as we have written in the BEGIN section, if ARGC != 3 the program displays an error massage described in the usage function.
The problem is that it also displays "Done !" like if the program executed normally.
To get rid off the successful message when an error occurred, I tried to change the END section by:
Code:
END{
if(ARGC = 3){
print "Done !"
}
}
but it still keep displaying the END message.
* Is there a way to get rid off the internal awk error message when ARGC != 3.
If ARGC !=3 the program doesn't launch so I get the usage message, but I also get the awk error message (like input_file: No such file or directory).
In such a case, if you don't want the END rule to do its job, set a variable to nonzero before the exit statement and check that variable in the END rule.
I also tried to do the same task for a bash script. A good way seems to use the getopts function, but the tutorials I found on the web are difficult to understand.
Does someone could try to explain me with the above example?
Or any other non-getopts method.
I read carefully both links you posted and I choose to give a try to getopts, but I got some issues.
So I started with something simple. Let's say the usage of myprog is as follow:
Code:
myprog -[l|n] <input> #to launch the program
# OR
myprog -h #to display the usage above
where -l and -n exclude each other, -l or -n and input are compulsory and don't take arguments. myprog contains 3 steps. If -l is mentioned myprog does step 1, 2, 3. If -n is mentioned myprog skip step 1 and does steps 2 and 3 only.
I tried several versions of the code below without success:
Code:
#! bin/bash
input=""
while getopts "l n" opt
do
case $opt in
l)
<do step 1>
;;
n)
next
;;
h)
echo "Usage: myprog -[l|n] input"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit1
;;
done
<do step 2>
<do step 3>
echo "End of the process. See ya !"
I am not really familiar with bash scripting. Is what I wrote really ugly...?
I haven't played much with getopt, mainly as I prefer to roll my own so I have long opts as well, but I think you are supposed to include your 'h' option in the getopts call.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.