• Post Reply Bookmark Topic Watch Topic
  • New Topic

UNIX shell options  RSS feed

 
Lubosh Bazant
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just a novice.
I am writting a shell script that will be using some input parameters specified on the command line. I do not want to reference the arguments in my shell script using the $1, $2 (based on the position of the argument) I would like to assign the arguments to variables directly from the command line, something like: -my_argument=value -my_other_argument=value.
Example:
sh my_script -my_argument=value -my_other_argument=value
The reason why I want this is because each argument is mutually independet form each other. The script will use 5 arguments in total and I do not want the user enter all 5 arguments if I only need one of them.
Thanks for the help
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can produce your own code to parse arguments, but often its easier to use the provided "getopt". do "man getopt" on your system, and with any luck you should get an answer from section 1 (user commands) of the on-line manual.. Read it and have a play with the possibilities.
If you don't have "getopt" installed, you can still loop through the supplied parameters (available in $*), look at the first few characters of each, land set your own options based on them, but using "getopt" should definately be the first thing you try.
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason #149 to switch for Perl :roll:
Perl's -s switch does just that!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!