Solved

alias + cp misery

Posted on 2003-11-09
14
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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
 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

688 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