FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 03-14-2012, 05:48 PM
rcb
 
Default Bash argument expanded inside alias

On Tue 13 Mar 2012 15:49:52 +0000(+0000), Clive Standbridge wrote:
> Oh I think I misunderstood the question.
> On second thoughts, I think you probably meant to use an alias inside
> a shell script, and to use the script's argument in the alias. In that
> case, the problem would be shell parameters are not expanded inside
> single quotes. You would need to use double quotes, for example
>
> alias muda="find . -name '*' -mtime -$1"
>
>
> --
> Cheers,
> Clive

Hi Clive,

Just for the record, I tried to include in ~./bash_aliases the
following command:

alias muda="find . -mtime -$1"

(As Chris Davies correctly suggested I don't need -name "*", so I simplified it)

But after run

$source ~./bash_aliases
$alias
alias muda='find . -mtime -'

Funny thing, I realized and run:
$source ~./bash_aliases 10
$alias
alias muda='find . -mtime -10'

Finally I came to the conclusion I really can't use args inside alias,
as you stated in an earlier message. All other answers to this email
suggesting $1 inside an alias fail because of that. The alias is
created using the script arguments, not the arguments passed to the
alias during its usage.


My last solution, very interesting, is that I don't need an argument
at all. See what works! Inside ~./bash_aliases I wrote:

alias muda="find . -mtime "

Now, after run
$source ~./bash_aliases
$alias
alias muda='find . -mtime '

And I can run my alias without using a function, just typing:
$ muda -10

It will concatenate the whole alias with the arg and run.

Thats a nice solution without using any function at all. Also, it
explains why you changing the -$1 to $1 (passing a negative value,
instead of keeping the 'minus' in the alias and passing a positive
value) works. Actually, all $1 is substituted to an empty string, and
it is just like the alias above.

Unfortunately, you cannot, for example, change the directory with an
argument, because 'find' can't accept a command in an inverted order
like:
$find -mtime -10 /home/kilemal
in case which your alias could be just alias muda='find -mtime'. You
need a function to achieve that.


Thanks all for your help and patience,

--
Beco
Linux Counter #201942
The man who revived a pentium 200 MMX from 1997, with debian squeeze
and made a server with it!


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: CALuYw2wis+aT4-whf+LWNbpYWGR1U6dZ=EaFD4OtD+0OnjeXkQ@mail.gmail.co m">http://lists.debian.org/CALuYw2wis+aT4-whf+LWNbpYWGR1U6dZ=EaFD4OtD+0OnjeXkQ@mail.gmail.co m
 
Old 03-14-2012, 07:36 PM
Chris Davies
 
Default Bash argument expanded inside alias

rcb <rcb@beco.cc> wrote:
> And I can run my alias without using a function, just typing:
> $ muda -10

> Unfortunately, you cannot, for example, change the directory with an
> argument, because 'find' can't accept a command in an inverted order
> like:
> $find -mtime -10 /home/kilemal

And the function is trivial. Just pop it in your .bashrc

muda() { local M="$1" D="$2"; find "${D:-.}" -mtime -"${M:-1}"; }

muda # Find from "." anything modified within 1 day
muda 3 # Find from "." anything modified within 3 days
muda 2 /tmp # Find from "/tmp/" anything modified within 2 days

Or if you really don't want a function, create a script and drop it
into $HOME/bin

mkdir -p "$HOME"/bin; cat > "$HOME"/bin/muda <<x
#!/bin/sh
#
M="$1" D="$2"
find "${D:-.}" -mtime -"${M:-1}"
x
chmod a+x "$HOME"/bin/muda


Why are you so reluctant to use functions? Even the bash man page says,
"For almost every purpose, aliases are superseded by shell functions."

Puzzled.
Chris


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: r3c739x6kr.ln2@news.roaima.co.uk">http://lists.debian.org/r3c739x6kr.ln2@news.roaima.co.uk
 
Old 03-16-2012, 01:30 AM
rcb
 
Default Bash argument expanded inside alias

> From Chris Davies, Wed, 14 Mar 2012 20:36:43 +0000
> And the function is trivial. Just pop it in your .bashrc
> *muda() { local M="$1" D="$2"; find "${D:-.}" -mtime -"${M:-1}"; }
> Why are you so reluctant to use functions? Even the bash man page says,
> "For almost every purpose, aliases are superseded by shell functions."
> Puzzled.
> Chris

Hi Chris,

I think I just learned a lot about functions with this thread, things
I didn't know.
At first I thought the best choice to abbreviate a command would be an
alias, and functions was to encapsulate big chunks of script code.
Never occurred to me I could use it as an alias.

Thanks,
Beco


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: CALuYw2wtyBHXtG7Osf+RCyyG5+jxa0TmLyY1LpjhrmzxPGW9c g@mail.gmail.com">http://lists.debian.org/CALuYw2wtyBHXtG7Osf+RCyyG5+jxa0TmLyY1LpjhrmzxPGW9c g@mail.gmail.com
 
Old 03-16-2012, 08:16 AM
Chris Davies
 
Default Bash argument expanded inside alias

rcb <rcb@beco.cc> wrote:
> I think I just learned a lot about functions with this thread, things
> I didn't know.
> At first I thought the best choice to abbreviate a command would be an
> alias, and functions was to encapsulate big chunks of script code.
> Never occurred to me I could use it as an alias.

It's always good to learn something new. :-)
Chris


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: mvcb39xpbd.ln2@news.roaima.co.uk">http://lists.debian.org/mvcb39xpbd.ln2@news.roaima.co.uk
 

Thread Tools




All times are GMT. The time now is 10:11 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org