LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Multiple Option (http://www.linuxquestions.org/questions/programming-9/bash-multiple-option-920886/)

decryptologist 12-28-2011 10:07 AM

Bash Multiple Option
 
Is there anywhere to let Bash support multiple option like these?
Code:

rm MySongs -rf
rm MySongs -r -f
rm MySongs MyVideos -rf
rm -rf MySongs MyVideos

In the first method "rm MySongs -rf", the target to remove was $1, the option was $2

But in second method, $3 was defined also, and in the third, $2 was defined as another target to be removed, and etc...

Or, Bash can't handle the option like this?
(Sorry for my broken English ><, just try your best to understand what I'm talking)

fukawi1 12-28-2011 10:34 AM

I could be misunderstanding your post.
But:
Code:

~/tmp $ ll *
-rw-r----- 1 fukawi1 fukawi1    0 Dec 29 02:28 1.html
-rw-r----- 1 fukawi1 fukawi1    0 Dec 29 02:28 2.html
-rw-r----- 1 fukawi1 fukawi1  25 Dec 29 02:24 3.html
-rw-r----- 1 fukawi1 fukawi1    0 Dec 29 02:24 test
-rw-r----- 1 fukawi1 fukawi1  22 Dec 29 02:24 tmp

1:
total 4.0K
-rw-r----- 1 fukawi1 fukawi1  0 Dec 29 02:27 2.html
-rw-r----- 1 fukawi1 fukawi1 25 Dec 29 02:28 3.html

2:
total 16K
-rw-r----- 1 fukawi1 fukawi1 38 Dec 26 22:51 1.html
-rw-r----- 1 fukawi1 fukawi1 25 Dec 26 22:51 2.html
-rw-r----- 1 fukawi1 fukawi1 25 Dec 26 22:51 3.html
-rw-r----- 1 fukawi1 fukawi1  0 Dec 26 22:51 test
-rw-r----- 1 fukawi1 fukawi1 22 Dec 26 22:51 tmp
~/tmp $ rm -rf 1 1.html 2.html
~/tmp $ ll *
-rw-r----- 1 fukawi1 fukawi1  25 Dec 29 02:24 3.html
-rw-r----- 1 fukawi1 fukawi1    0 Dec 29 02:24 test
-rw-r----- 1 fukawi1 fukawi1  22 Dec 29 02:24 tmp

2:
total 16K
-rw-r----- 1 fukawi1 fukawi1 38 Dec 26 22:51 1.html
-rw-r----- 1 fukawi1 fukawi1 25 Dec 26 22:51 2.html
-rw-r----- 1 fukawi1 fukawi1 25 Dec 26 22:51 3.html
-rw-r----- 1 fukawi1 fukawi1  0 Dec 26 22:51 test
-rw-r----- 1 fukawi1 fukawi1 22 Dec 26 22:51 tmp
~/tmp $

rm can be passed a list of files to be deleted, by a space delimted list. as shown above.

Where my rm command, has removed the subdirectory "1" and the files "1.html" and "2.html" but left the rest of the files in tact.

Is that what you were looking for?
What specifically are you trying to achieve?

**Edit** Keeping in mind, that -rf equates to
-r = Recursive (including all subdirectories)
-f = Force (do not ask questions)

it would be advisable to familiarise yourself with these commands, because a simple type can have disasterous results, particularly if running as root.

I in no way intend to undermine your knowledge or intelligence, but for any others who may chance upon the thread.
The difference a space can make between
Code:

rm -rf *.html
and
Code:


rm -rf * .html
        ^ note space here

Could have less disastrous effects on the expected results.

NevemTeve 12-28-2011 11:03 AM

These examples aren't actually good examples, because

1. for most programs, options come before other parameters
2. bash and rm are two different programs

David the H. 12-28-2011 06:25 PM

If you're just talking about changing the order of the input options of whatever command you're using, then no, most programs won't allow it. You almost always have to put option flags that start with "-" before the main argument list.

This is just the traditional standard practice, however. Every program actually handles its own input with its own internal programming. So you may occasionally find programs that can allow such variations. You'll also find others that are even stricter than the norm.

Remember that in the end bash is just the program launcher. It simply parses the line first according to its own shell syntax, assembles whatever it finds into a final command string, and tells the system to execute it (assuming it's an external command). What happens next is up to the program launched.

http://mywiki.wooledge.org/Arguments



OTOH, you may actually be talking about writing a shell script with option flags. If so, bash has a built-in getopts command for handling "-o value" style short options.

http://wiki.bash-hackers.org/howto/getopts_tutorial

If you want long-style "--option=value" options too, then you have to use the trickier-to-handle external getopt application, or write your own input parser. (I used to have a link on getopt, but I can't find it now. Google it if you're interested.)

decryptologist 12-28-2011 09:55 PM

But, if I use the "getopts" method liked this:
Code:

./test.sh -a TestFile
( -a is the option, and TestFile is the target)
The target is $2, so the order can't be rearrange, or it will encounter an error, right?
Rearrange means:
Code:

./test.sh TestFile -a

NevemTeve 12-29-2011 12:20 AM

In shell-script, you can shift the arguments with shift; in C you can increment argv and decrement argc to skip processed arguments.

Reuti 12-29-2011 06:34 AM

Quote:

Originally Posted by David the H. (Post 4560424)
This is just the traditional standard practice, however. Every program actually handles its own input with its own internal programming. So you may occasionally find programs that can allow such variations. You'll also find others that are even stricter than the norm.

I second this. One example is the ls command on Linux (but not on AIX), where you can put options also after the list of files. If you want to implement such a behavior, you could scan the complete list of arguments and build two lists: one for options and one for the real arguments and handle it appropriate this way.

Many programs also allow a to use a double dash (i.e. a --) to end the list of options, whether following arguments are starting with a dash or not, they are handled as plain arguments only, e.g:
Code:

$ touch -- -rf
$ ls -- -rf
-rf
$ rm -- -rf


NevemTeve 12-29-2011 07:07 AM

Yes, let's not forget -l is a perfectly legal filename (so are these: * ? ~ $).


All times are GMT -5. The time now is 12:21 AM.