Link to home
Start Free TrialLog in
Avatar of CEHJ
CEHJFlag for United Kingdom of Great Britain and Northern Ireland

asked on

alias + cp misery

Can anyone tell me why the following alias

alias deploy='cp -v $1 $CATALINA_HOME/webapps/'

managed to produce the infuriating error message

cp: omitting directory...

etc. and do absolutely nothing when invoked as follows:

deploy struts-blanks.war

or

deploy /jakarta-tomcat-4.0.1/scratch/struts-blank.war

?
Avatar of arjanh
arjanh

Alias doesn't seem to have parameter substition...
I suggest you make a small shell script somewhere in your PATH called 'deploy' containing the following line

cp -v $1 $CATALINA_HOME/webapps/

Then your command 'deploy <file>' should work.
What does $CATALINA_HOME expand to? Does it contain spaces? The cp command in your alias will only accept a file as the source ($1). Is struts-blanks.war a file or is it a directory?
Avatar of CEHJ

ASKER

>>Alias doesn't seem to have parameter substition

Not true - at least on my system. The following works perfectly well:

alias deploy='echo $1'


>>What does $CATALINA_HOME expand to?

/jakarta-tomcat-4.0.1

>>Is struts-blanks.war a file or is it a directory?
     
The former
SOLUTION
Avatar of arjanh
arjanh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ

ASKER

Good work guys! But, jlevie, i'm rather partial to my aliases as I can edit them all in one file (i'm keeping them all directly in bashrc). Can you think of a way with the alias?
You can place shell fucntions in your .bashrc. I have several in mine and since a function can be multi-line they are far more powerful than a simple alias. For example:

      prt () { if [ $# = 0 ]; then
                 pr -w90 | /usr/lib/lp/postscript/postprint -l 60 | lp
               else
                 pr -w90 $* | /usr/lib/lp/postscript/postprint -l 60 | lp
               fi; }

will print from either STDIN (like in a pipe) or from a file, depending on whether an argument is specifed to 'prt'.
Well, after a lot of playing around with different approaches, this should work. It can be included with your alias definitions in the bashrc file:
function deploy { cp -v $1 $CATALINA_HOME/webapps/ ; };
Avatar of CEHJ

ASKER

Thanks guys - going with the function in bashrc - trying to increase the points so you can have half each...
It looks like you are using single quotes which force an absolute string, ie  "deploy test1" is passing off to cp as "cp $1 $DIRNAME" instead of "cp test1 $DIRNAME".  If you use double quotes and this vairable ($CATALINA_HOME) exists, your alias should be correct.
Avatar of CEHJ

ASKER

DOH - i didn't spot that - but looks like i wasn't the only one ;-) I'll check this later - but i'm sure you're right as I don't see why positional parameters should suddenly fall over, merely because of an alias (some of my others have double quotes)
Double quotes give the same behaviour:
~$ alias deploy="echo $1 second"
~$ deploy first
second first

Alias in Bash doesn't know about positional parameters. It tries to substitute the environment variable $1 in that place. Even worse, with _double quotes_ substitution is done at declaration time of the alias, not execution time:
~$ export a=test
~$ alias deploy="echo $a second"
~$ export a=foo
~$ deploy first
test second first

With _single quotes_ it is done at execution time:
~$ export a=test
~$ alias deploy='echo $a second'
~$ export a=foo
~$ deploy first
foo second first
I stand corrected.  All my aliases would feasibly work without the positional parameter because the commands come before the criteria ie.

alias lps="ps aux | grep $1"

lps soffice

translates to "ps aux | grep soffice" because of placement not positional parameter.
Avatar of CEHJ

ASKER

Right, thanks.