Solved

alias + cp misery

Posted on 2003-11-09
14
750 Views
Last Modified: 2010-04-21
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

?
0
Comment
Question by:CEHJ
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 5

Expert Comment

by:arjanh
ID: 9710197
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.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 9710201
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?
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9710231
>>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
0
 
LVL 5

Assisted Solution

by:arjanh
arjanh earned 65 total points
ID: 9710318
> Not true - at least on my system. The following works perfectly well:
> alias deploy='echo $1'

That's what I started with. Now try the following and see what it prints out :)
alias deploy='echo $1 second'
deploy first

At least for bash the output is
second first
0
 
LVL 40

Accepted Solution

by:
jlevie earned 65 total points
ID: 9710333
Thinking about this, I realized that the real problem is that the argument to your alias isn't in $1b, but is simply appended to the end of the alias expansion. So wehn you execute 'deploy stuts-blank.war' the resultant command becomes 'cp -v /jakarta-tomcat-4.0.1 struts-blanks.war'. You can easily demonstrate that this is the case with:

chaos> alias foobar='echo "$1 arg is here:"'
chaos> foobar argument
chaos> foobar argument
 arg is here: argument

The simple solution to your problem, assuming you are using the bash shell, is to create a shell function, like:

deploy () { cp -v $1 $CATALINA_HOME/webapps; }

Shell functions place their arguments in $1, $2, etc.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9710391
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?
0
 
LVL 40

Expert Comment

by:jlevie
ID: 9710554
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'.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:arjanh
ID: 9710566
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/ ; };
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9710691
Thanks guys - going with the function in bashrc - trying to increase the points so you can have half each...
0
 

Expert Comment

by:ragoley
ID: 9764232
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.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9764587
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)
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9765738
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
0
 

Expert Comment

by:ragoley
ID: 9765935
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.
0
 
LVL 86

Author Comment

by:CEHJ
ID: 9766012
Right, thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now