?
Solved

alias + cp misery

Posted on 2003-11-09
14
Medium Priority
?
762 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 5

Assisted Solution

by:arjanh
arjanh earned 260 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 260 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 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