Solved

alias + cp misery

Posted on 2003-11-09
14
753 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
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.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add some character at the end of line in vi 7 64
su - oracle could not open session 6 53
linux redhat 7.2 10 47
Coding C# in Linux 8 36
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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…
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.

863 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

22 Experts available now in Live!

Get 1:1 Help Now