Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aliases and command line arguments 3

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I want to test for a command line argument within an alias ...

alias mb3='if [[ -z $1 ]];\
then\
echo "yes";\
else\
echo "no";\
fi;\
echo $1'

It doesn't seem to recognize the $1 in the if stmt (I always get "no"), but it does recognize it in the echo stmt.

I've tried escaping the 1st $1 (i.e. - \$1) to no avail.

Can this be done?

example ...
> mb3
> no
>

> mb3 abc
> no
> abc
 
correction ...

the reason I'm getting "abc" is NOT because of the echo stmt, but only because it's echoing what I type after the alias name, independent of the alias script.

Doesn't apear you can use CLA's within aliases. How about then in a function within an alias? Can you define a function within an alias?
 
Why not just use a function?

e.g.
Code:
function mb3
{
    if [[ -z $1 ]]; then
        echo "yes"
    else
        echo "no"
    fi
    echo $1
}
 
alias processing doesn't do what you are expecting it to do...

If you have an alias like ll='ls -l', then when you run the command
ll /tmp
the shell looks for an alias ll and converts your command line from
ll /tmp
to
ls -l /tmp
It is simply doing a string substitution. Then it processes that converted command line. So it is (in this case) the command "ls" which will process any flags/arguments you provide.
If you need to process CLAs, you need a command script (or a function, as chipperMDW suggests).


HTH,

p5wizard
 
BTW, I'm using ksh shell ...

Yeah, I've come to that conclusion that ksh and aliases and CLA's don't jive. I just recall seeing/remembering that csh shell had the ability to use CLA's from within an alias (e.g. - "\!" or something like that).

Yeah, I know I can write a script to process CLA's (either via a function or just plain scripting), but I was trying to avoid creating these tiny little scripts just so I could utilize CLA's.

Anyway, can I define/write a function within an alias which uses CLA's? I haven't been able to...
 
But why do you insist on alias ?
Simply write functions in your $ENV file.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Never given it a thought about using a function in .kshrc (i.e. - $ENV) file ...

works just as good as an alias!!

Thanks to all !!!

FYI - it appears that given an alias, a function, and a script all with the same name, the order of precedence is: alias, function, script ... does that sound right (yeah, why would anyone want to do that, huh?).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top