Unexpected output getopts in AIX
Hi all,
Currently I am using AIX at work. I am no administrator of the system, merely a user. I am trying to create a script that will accept either a file with a list of files as content, or a list of files. At first I build it for myself, but I want it to be available for my colleagues in the longer run. The script is called mailen (Dutch for mailing). Either you start the script as: ./mailen file1 file2 file3 or as: ./mailen -f file_list file1 file2 In the first case, it should e-mail the files (file1, file2 and file3) to me as an attachment. In the second case, it should read the content of the file file_list and e-mail me the files in the content from that list and file1 and file2. So I started with a script that would e-mail me the files I put on the command line. The script works as designed. The next step is to try and see if I can make a script that will get the content of the file in case I use the -f (file) option. The script I have should contain 2 options, namely -v (verbose) and -f (file to read content from). This is what I have so far: Code:
#!/bin/bash $ ./mailen_getopts.sh -f foo foo1 foo2 Rest foo1 foo2 V_FLAG= F_FLAG=1 FILE=foo $ ./mailen_getopts.sh -f foo -v foo1 foo2 Rest foo1 foo2 V_FLAG=1 F_FLAG=1 FILE=foo $ ./mailen_getopts.sh -v foo -f foo1 foo2 Rest foo -f foo1 foo2 V_FLAG=1 F_FLAG= FILE= As you can see, in the first 2 cases output is as expected. However in the last case, -f seems not to be an option anymore... For some reason if you tell getopts that an option should not have an argument, it will not parse the rest of the string if you don't provide a new option right away. Has anyone encountered this before? Does anyone know how to overcome this without loosing too much flexibility? |
That's normal behaviour.
When getopts finds a non-option after -v it treats the rest of the command line as arguments. On GNU/Linux systems the getopt command is available and (I think) behaves as you want. Don't know about AIX. |
Isn't there something to tell getopts that an argument is prohibited for a certain option? Like you can use : to say an argument is mandatory.
I tried some other ways to go around this: 1. Tell getopts that v also needs an argument: v:f: Outcome: if you don't provide an argument it takes -f as the argument... 2. Try v;f: Outcome: seems not to work as a way to tell getopts that an argument is prohibited for the option v. |
The absence of a : after v tells getopts that the v takes no argument.
You can get the behaviour you want with Code:
#!/bin/bash |
On the version of AIX that I am working on, it does not work. It seems not to handle the += and the ( ) on line 16.
|
Try this change
Code:
FILE= |
That did the trick, thank you very much!
Edit: It actually only works in the example given, if I start the script as ./mailen_getopts.sh -f foo foo3 -v foo1 foo2 It gives: $ ./mailen_getopts.sh -v foo foo3 -f foo1 foo2 Rest foo3 -f foo1 foo2 V_FLAG=1 F_FLAG= FILE= So the problem occurs again when I enter an extra argument. The solution seems to be static, not dynamic. Even though with that shift I would think this would be dynamic. |
Better to custom program it to solve that:
Code:
#!/bin/bash Code:
c@CW8:/tmp$ ./try.sh -v foo1 foo2 -f foo3 foo4 -ffoo5 |
I used what you have given me. I finally figured out that indeed getopt or getopts wasn't going to do what I wanted. And I custom crafted the script as I want it to be:
Code:
#!/bin/bash If you have any comments or questions about this script, please let me know. I am marking this thread as solved. Thank you for your time. |
Glad you evolved a solution to meet your requirements and thanks for the update :)
|
All times are GMT -5. The time now is 01:38 AM. |