bash script parameters and logic
Hello everyone,
I am trying to create a basic script with the following requirements: When no command line arguments at all are present, the script will print a helpful "Usage" message listing the possible options and arguments, and exit. I think i got this one. Example: sh myscript.sh The script's default function when no options are given on the command line, but a <name> is supplied, is to print all lines in my address_book file. Example: sh myscript.sh Bill If the '-a' option is present, the script will prompt for the data items in the record (name,and email address), construct a data record with the appropriate format, and add it to the 'address_book' file. Example: sh myscript.sh -a If the '-b' option is present, the script will mail a personalized greeting to the email address of every person in 'address_book' whose day of birth is today. Example: sh myscript.sh -b I am new to bash scripting language and I am having trouble to construct script logic. I create the first part it worked fine and I am trying to construct the rest part. Here is what i have so far. Code:
[asistant@myserver]$ cat test5 Any input greatly appreciated. Thanks. |
How do you add record to the address book and how do you send a personalized greeting? I mean the commands.
|
It would be simpler to separate command line parsing (and error trapping) from the actions. Assuming you would like to add a -h option to get verbose help, then something like this
Code:
# Parse command line |
May I also suggest for the usage / help function that you have a look at here documents. I find this particularly helpful, especially
if the size of the information will grow or vary. Also, whilst getopts presented by catkin is fine, if you wish to play a bit more with bash you could produce your own simple parser to search for strings starting with a hyphen and use a similar case to set the appropriate options: Code:
for arg |
Quote:
|
Quote:
I would add that long options are also handled a little easier than with getopts. |
Quote:
Quote:
It handles -a a-param -b the same as -ab a-param, and also supports long options with or without values, i.e --help, --type=value or --type value . I'm sure sharp minds here could find ways to enhance it, though. :study: |
Than you very much everyone for the help and inputs.
Here what i have so far it looks like it is working fine at this point now I am trying to fill each functions to perform actual work. Code:
#!/bin/bash Elizabeth:Miller:A:55 W. Main Street:Camp Hill:CA:94500:717-502-0334:elizabeth.miller@wolpublishing.com:01/18/1980 John:Johnson:A:2155 North Street:Harrisburg:PA:17101:717-233-1100:john.johnson@wolpublishing.com:02/16/1967 I am having trouble with grep command in my function Code:
function search_name { [asistant@myserver ~]$ ./test3 John any idea? Thanks you. |
I would suggest putting set -xv just after the shebang and you will see the reason for this. As a nudge in the right direction, functions and scripts behave similarly when called with parameters :)
|
grail, sorry how do you do that?
|
Quote:
Put another way, $1 in a function refers to the first parameter to the function. Bluntly: it does not refer to the first positional parameter the caller might have, as asistant seems to think. Functions only see their own positional parameters. ($0 is special, though: it describes the script name. For Bash sripts, it gets set to whatever file Bash is executing.) |
Quote:
Code:
#!/bin/bash |
thanks grail I am getting closer. Here is what happens
When i execute the script like this; [asistant@myserver]$ ./test4 John Code:
if [ -n "$1" ]; then any ideas? Thanks you. |
Quote:
|
thank for pointing me right direction. Here what i come up with.
added #Make arguments global to the script. args=("$@") changed the function as; #Create function for searching name. function search_name { addressfile=~/myaddrbook grep -i ${args[0]} $addressfile | tr ":" " " } Here is my new version of the script. Code:
#!/bin/bash |
All times are GMT -5. The time now is 03:59 AM. |