add options to bash script
Hello forum friends, I have the following bash script (which works perfectly), the code is as follows:
Code:
#!/bin/bash My idea is to add the following - book (create book) - booklet (create booklet) - screen (create screen) - legal (make legal) - all (all formats) try to get married, but did not get what he wanted (I should clarify that I am not a programmer, and I believe it from google, this and other forums Linux users) if you help me with some example, or tell me I should improve on this script, would appreciate it. regards Pablo |
If I may, I'd like to first make a formatting suggestion.
It looks like a well-written script, much cleaner than many I've seen. But it's not very readable. You really need to use a lot more whitespace. Indent all your sub-commands evenly, and separate logical sections with empty lines. In other words, visually separate blocks of code that go together. Next, please clarify your request a bit more. You want to add some features, ok. But what is the actual problem you're having in doing so? If you want to know how to add command "flags" (e.g. something like "-b bookname" ), the usual technique is to use getopts. http://wiki.bash-hackers.org/howto/getopts_tutorial PS: More general advice: QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later. http://mywiki.wooledge.org/Arguments http://mywiki.wooledge.org/WordSplitting http://mywiki.wooledge.org/Quotes Also, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them. |
This its a second version (using case)
Code:
#!/bin/bash Now I focus on the problem, I would like it to work well Code:
sh script.sh name -p -l -s -f grateful Pablo |
Did you read David's post? He has suggested you look at getopts which will do exactly what you are looking for.
I would add that you have already given an interpreter inside the script, so to then run it with sh would seem pointless. Simply make it executable and run as you like. |
He did mention reviewing my link, grail.
In any case, we're on the right track; testing possible options with a case statement. The secret to the next step is to embed the whole thing inside a loop, allowing you to iterate over multiple inputs. getopts simply provides a ready-built way to organize and parse such input options, particularly ones that can be followed by secondary arguments. |
My bad ... didn't read til the end of the line :(
|
Thanks for comment, check the links above and let me try to adapt to my needs, I stay with this option, it is quite clear to read (looks like perl structure), now the new script its :
Code:
#!/bin/bash What should I change? Or this is not the best way to achieve what I want? my idea is "sh script.sh -option1-option2..." enter, asks the name of the project and runs whit options. regards Pablo |
I am getting a little confused ... are you going to try the getopts suggestion?
|
Quote:
Code:
#!/bin/bash Saludos Pablo |
Well the simple solution to the no options problem would be to test for no options prior to running the script. This allows you to catch it but based on your current format would also
require you to duplicate the help section (perhaps a function would make this simpler) You could reduce this to a single call to the help option if you also test for the -h and so on options are anywhere in $@, which is all options passed in |
You're right, review the documentation on function to create the menu "help" and the test input, later commented the results
|
This is the forward end of the script, add the HELP function as, in one test to see if there was the directory with the name input, also has a test that shows if the help does not enter parameters. I only have two problems to solve:
1. Display "HELP" without having to enter NAME 2. Showing error when entering an option without "-", "--" I think I could move up here with the links they gave me and the other post on the forum, but need help in this part of the most wise. The Best Pablo (Using fedora 17 and TeXLive2012) The code: Code:
#!/bin/bash |
Quote:
Quote:
I am not saying it is not correct but it does mean you are limiting the user in an unusual way. It also appears that you are using getopts in an unusual way. Again not wrong but the the general idea would be to call getopts in your loop and do the processing in one go. You may like to look up here documents for your HELP function. Also, the case calls seem to have a large amount of repetition with slight changes which would normally point to creating some functions. |
Quote:
Quote:
Quote:
Quote:
Grateful for your answer (and links) Pablo |
As the script is to only accept switches and zero parameters, based on the following line of code:
Code:
echo "Uso:sh $0 -b -l -f -s -h" Quote:
Code:
case blah in |
All times are GMT -5. The time now is 09:28 PM. |