Solved

alias + cp misery

Posted on 2003-11-09
14
756 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

810 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