Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
and for this, what I need is, "websiteadress" can either work by doing '-w "websiteadress"', or '--website="websiteadress"', and the same for '-f "filename"', that equals '--filename="filename"', there should be only one more option, which is '-h', the same as '--help', that should print the scripts usage, and should also be printed, in case '-f' or '-w' parameters are missing, both are mandatory, and should translate to constants, so, later in the script, values entered can be translated to something as ${FILENAME} and ${WEBSITEADRESS}, from there on, I think I already know how to make it work
I apologise for the work, I understand it may be a somewhat big request, but I believe that someone knowledgeable, helping on how to do this correctly, the most efficient way, on a script that will prove useful to me, will have better results, even on future projects, than reading on an unrelated script subject, and trying to adapt it
Note, I also know that I could accomplish this with something like setting '$1' and '$2', but this would not be as effective, as would not give any help on it's usage, and both parameters would have to be entered on the same order, every time
Use a while loop, then case, and shift. Like this:
Code:
# loop until number of arguments is zero
while [ $# -ge 1 ]; do
case "$1" in
-h|--help)
# show help text and exit
exit 0
;;
-f|--filename)
# shift the positional parameters
shift
# now, what was in $2 is now $1
FILENAME="$1"
;;
-w|--website)
shift
WEBSITE="$1"
;;
esac
# advance to the next parameter
shift
done
Last edited by i92guboj; 10-05-2008 at 06:42 PM.
Reason: corrected code
Thank you for your quick help
If I may make an observation, there is something missing in the example you gave me, I was getting an error, and examining the code, found it was after 'exit 0', you forgot ';;', I also believe the ';;' after 'WEBSITE="$1"' was not necessary, I think it was giving me problems later in the script
I also used your code as reference, and following the webpage I mentioned before, from http://linuxcommand.org, I organized it a little bit, and added the other options I wanted, such as printing usage when no arguments are given, and making both '-f' and '-w', mandatory
Code:
#!/bin/bash
##### Function
function usage
{
echo 'Usage: my_script [-f filename | --filename filename] [-w websiteadress | --website websiteadress]'
}
##### Main
# Show usage and exit, if no arguments are given
if [ "$1" = "" ]; then
usage
exit 1
fi
# loop until number of arguments is zero
while [ "$1" != "" ]; do
case "$1" in
-f | --filename ) shift
FILENAME="$1"
;;
-w | --website ) shift
WEBSITE="$1"
;;
-h | --help ) usage
exit 0
;;
* ) usage
exit 1
esac
shift
done
# Show usage and exit, if either '-f' or '-w' are missing
# First, check for '-f'
if [ "${FILENAME}" = "" ]; then
echo 'my_script: "--filename" option is missing'
exit 1
fi
# Then, check for '-w'
if [ "${WEBSITE}" = "" ]; then
echo 'my_script: "--website" option is missing'
exit 1
fi
Until now, everything is working as I want, I've been testing and altering it since I saw your response, and everything seems to be coming together pretty well, thanks again for your help
Thank you for your quick help
If I may make an observation, there is something missing in the example you gave me, I was getting an error, and examining the code, found it was after 'exit 0', you forgot ';;',
Yup hehe, I was in a hurry and missed it. I'll edit my post so no one gest confused about it in the future.
Quote:
Until now, everything is working as I want, I've been testing and altering it since I saw your response, and everything seems to be coming together pretty well, thanks again for your help
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.