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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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