ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
You might consider using the bash built-in "getopts". It works like the C language function of the same name. If an option requires an argument, a semicolon will follow the option letter in the option string. Then you can simply read the $OPTARG variable for the value of the argument. Typing in "help getopts" in the bash shell will give you some information. Also detailed information should be in the bashref manual.
Your question reminds me of the optional destination directory option of the "mv" command, i.e. the '-t' option, which allows its use with the "xargs" command.
If you have regular arguments, rather than option arguments, you can use the value of $OPTIND to determine where to start regular processing. If you will only have one optional command argument, then you don't need to use options. Instead check for the existence of an argument, if one doesn't exist, i.e. $# is 0, then use $PWD; otherwise if $# is 1, use the supplied argument.
if you want to use flags and arguments all you have to do if find out how many arguments were sent into the script and use a case block to execute what you want
case "$number_of_args" in
1)echo "this executes if 0 flags"
1)echo "this executes if 1 flags"
2)echo "in here you could put like another case, lets say you have"
echo "2 flags and now you know that the first has to be the flag"
echo "and the other has to be the pathname"
echo "then you do another case, instead of all this text"
case $1 in
-d)case $2 in
echo "do whatever you want here"
*) echo "invalid input"
etc) you get it