LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-11-2012, 08:27 PM   #1
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Rep: Reputation: Disabled
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?

e.g. if input file contains letters:
Code:
prog <input> -let
or if input file comtain numbers:
Code:
prog <input> -num
and to show usage:
Code:
prog -h
 
Old 10-12-2012, 06:19 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
do you have the source code?
 
Old 10-12-2012, 10:34 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
This should get you going:
Code:
#!/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.
 
1 members found this post helpful.
Old 10-13-2012, 04:02 AM   #4
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
@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 !
 
Old 10-15-2012, 01:05 AM   #5
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
2 Little problems I didn't think about though.

* 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).

Hope someone could help me !

Last edited by Trd300; 10-15-2012 at 01:06 AM.
 
Old 10-15-2012, 01:58 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
i would suggest you to set a variable in the BEGIN block (instead of using ARGC). And you can check that variable in the END block
 
Old 10-15-2012, 01:58 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Possibly a simpler way may be something like this:

Code:
awk '( let ) { <do letter tasks> } ; ( num ) { <do number tasks> }' let=1 num=0 letterfile.txt let=0 num=1 numberfile.txt
awk allows you to pass variables as arguments before a filename.

I haven't actually tested it, but it should work in concept.
 
Old 10-15-2012, 11:06 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is what the manual says:
Quote:
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.
See here for further details.
 
Old 10-17-2012, 06:47 AM   #9
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
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.

Thanks !
 
Old 10-17-2012, 08:32 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
here is a step-by-step tutorial: http://wiki.bash-hackers.org/howto/getopts_tutorial
but this getopts cannot handle long options like -let or -num
 
1 members found this post helpful.
Old 10-17-2012, 11:39 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe this is what you are looking for:

http://www.linuxquestions.org/questi...andling-34675/
 
1 members found this post helpful.
Old 10-17-2012, 11:23 PM   #12
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
Very helpful, thanks !
 
Old 10-18-2012, 06:37 AM   #13
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
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...?

Last edited by Trd300; 10-18-2012 at 06:39 AM.
 
Old 10-18-2012, 06:44 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
not really ugly, but not really nice: you should write something like:
Code:
case $opt in
   h) function_h;; # usage
   l) function_l;;
   n) function_n;;
   *) function_error;;
esac

do_common_steps
and implement four functions to handle all the cases.
 
Old 10-18-2012, 10:54 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gawk works from command line but not from cron fantasygoat Linux - Server 3 10-25-2011 01:18 PM
howto create GUI-graphical wrapper for command line program SaintDanBert Linux - Desktop 4 01-30-2011 08:24 PM
Program on Command Line Argument and Pointers thelink123 Programming 2 09-01-2009 09:05 AM
[SOLVED] specifying fields for printing in gawk from command line David the H. Programming 8 08-04-2009 03:32 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration