LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-05-2008, 04:12 PM   #1
onepostonly
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Rep: Reputation: 0
looking for help in writing a bash script


I have read http://linuxcommand.org , specially http://www.linuxcommand.org/wss0130.php , and what I am looking to do, is something of the likes of

Code:
my_script -w websiteadress -f filename
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
 
Old 10-05-2008, 04:27 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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
 
Old 10-05-2008, 05:57 PM   #3
onepostonly
LQ Newbie
 
Registered: Apr 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 10-05-2008, 06:42 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by onepostonly View Post
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
Glad to be of any help.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Me>> Need help in writing Bash script lamak_98 Programming 6 10-04-2007 10:44 AM
Writing a bash script. lebabyg Linux - General 2 03-31-2007 11:39 AM
Bash (help writing script) lebabyg Linux - General 7 07-04-2006 05:22 PM
writing a bash script poiuytrewq Linux - Newbie 2 10-07-2004 10:26 PM
writing bash script ankitgdit Programming 4 08-19-2003 06:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:42 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