Script creation (how to include options in the script)
Hi guys i have written a script which takes the options given to him and execute itself accordingly.
for example if a script name is doctortux then executing doctortux without option should made doctortux to be executed in automatic mode i.e. doctortux -a or if a doctortux is needed to run in interactive mode we need to specify -i option. To simulate above conditions i m using following code. Is it the best practice to achieve the above mentioned goal? Code:
if [ -z "$1" ] || [ "$1" == "-a" ] |
So firstly, well done on using the code tags, but if you don't bother to indent it is almost as unreadable as not having used them at all.
As for best practice, probably horses for courses, but you could take a look at the getopts command in bash |
Quote:
1)How would i consider automatic mode when no option is provided with the script name? 2)why "/usr/local/sbin/doctortuxv1: illegal option -- f" illegal option is said when i have given -f? Code:
while getopts ai option Code:
# doctortuxv1 -a |
How about a simple if that tests to see if the first argument passed in ($1) has a dash (-).
If yes use getopts, if no then set default. |
Quote:
as Code:
man --help |
in many shells, it's commonly done this way:
Code:
while [ "$#" -gt 0 ]; do |
Quote:
Code:
#!/bin/bash |
I also used getopts before and both are fine to me. It's ok to use whatever is comfortable or easy for you. I wonder but probably getopts is not available in all shells that are not bash. Not really important if it's only for bash.
|
All times are GMT -5. The time now is 01:12 PM. |