LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-28-2011, 10:07 AM   #1
decryptologist
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
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)

Last edited by decryptologist; 12-28-2011 at 10:14 AM.
 
Old 12-28-2011, 10:34 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 189Reputation: 189
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.

Last edited by fukawi1; 12-28-2011 at 10:40 AM.
 
Old 12-28-2011, 11:03 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
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
 
Old 12-28-2011, 06:25 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.)
 
Old 12-28-2011, 09:55 PM   #5
decryptologist
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-29-2011, 12:20 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
In shell-script, you can shift the arguments with shift; in C you can increment argv and decrement argc to skip processed arguments.
 
Old 12-29-2011, 06:34 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,320

Rep: Reputation: 252Reputation: 252Reputation: 252
Quote:
Originally Posted by David the H. View Post
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
 
1 members found this post helpful.
Old 12-29-2011, 07:07 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,948

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


Reply

Tags
argument, bash, getopts, option


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
Multiple values in a search option gene.rye Linux - Newbie 3 11-29-2010 11:13 AM
How to use getopts to accept multiple parameters for a single option ? bittus Linux - Newbie 1 09-04-2009 02:38 AM
have multiple Option USB modems in one computer? theVOID Linux - Hardware 1 02-07-2008 11:02 PM
Using the Multiple tape option with TAR bassman Programming 0 07-08-2004 01:49 PM


All times are GMT -5. The time now is 09:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration