The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - Newcomers (http://hintsforums.macworld.com/forumdisplay.php?f=15)
-   -   make an alias to a command with an argument? (http://hintsforums.macworld.com/showthread.php?t=2269)

loren_ryter 04-14-2002 10:16 PM

make an alias to a command with an argument?
 
some kind user posted a way to get a process by name.

I was thinking wouldn't it be great to have a "getpid" command.

So, having no idea really what i was doing, I tried to create the following file in Pico:

-----

#!/bin/sh
ps -auxxw | grep $

----

I tacked on the $ cause i figured that might be a way to pass an argument. (laugh away).

then i saved it out, and did a "chmod 755 getpid" and then typed rehash.

then i tried:

%getpid itunes

and got a whole pid listing. not what i wanted.

how would i pass an argument to that script or whatever the heck it is I just made.

;-)

--loren

mervTormel 04-15-2002 12:19 AM

Code:

for tcsh shell, define an alias:

alias zfp 'ps wwaxl | egrep -i "\!:1|PPID CPU"' # find process

for bash shell, define a function to pass args:

zfp  () { ps axwwl | egrep -i "$@|PPID CPU" ; } # find process

results:

$ zfp bash
  UID  PID  PPID CPU PRI NI      VSZ    RSS WCHAN  STAT  TT      TIME COMMAND
  501  429  395  0  31  0    5032  1160 -      Ss+  p1    0:00.39 -bash (bash)
  501  1254  395  0  31  0    5032  1120 -      Ss  std    0:00.56 -bash (bash)
  501 19045  1254  0  31  0    5032    84 -      R+  std    0:00.00 egrep -i bash|PPID CPU"

this will find anything in the process's command path string. like you could look for processes from /System. and the egrep will always find the header record, too.

loren_ryter 04-15-2002 12:36 AM

so how long does the alias persist and...
 
Great! thanks. but how long does that alias persist (from reboot to reboot or is it temporary?). If it persists, where is it stored. (I used the tsch one)

also, i have no idea at all how i could recreate that with a different syntax with an argument.

if i type

alias command_name 'some command string'

then where does the argument go, in general terms?

TIA

mervTormel 04-15-2002 01:47 AM

the tcsh shell maintains a list of aliases which can be manipulated with the alias and unalias commands.

they are persistent for that shell's existence. to get aliases defined automatically at shell startup, define them in the file ~/.tcshrc. or you can read the file at /usr/share/init/tcsh/README and implement the suggested scriptage there, but some find that overly rococo.

check the man pages for tcsh to find the docs on aliases; you'll have to search for the alias command:

% man tcsh

arguments in aliases follow the form of the command your referencing, e.g.:
Code:

alias zloc      "locate \* | perl -ne 'print if m#\!*#i'"
alias zrmx      'mail -f ~/mbox'
alias ll        'ls-F -lAh --color \!* | more'
alias zsnf      'sudo tcpdump -i en0'
alias zc        'awk "BEGIN{ print \!* }" '  # cli calculator - zc 5*5
alias zmx      'chmod u=rwx \!*'
alias zdv      'df -h'
alias ztp      'top -u -s3 10'
alias zw        'echo $\!:1'    # echo $var -> zw var
alias zmem      'vm_stat'
alias ztsl      'tail -vf -n50 /var/log/system.log'

arguments passed to an alias follow tcsh's script arg notation and need to be properly quoted. that is beyond the scope of this reply.

you've got some homework to do. read up on aliases in the tcsh man pages, and look for examples on the web and in some books.

jmeador 04-15-2002 02:29 AM

I've also had luck w/ defining alias commands in my "~/.login"

cd ~
pico .login

alias bye logout
alias ftplog cat /var/log/ftp.log
alias jawho 'last | grep still'
alias cuthere 'echo "-------------- Cut Here -------------- " > /dev/console'


etc...

xchanyazy 04-15-2002 03:14 AM

I use my ~/.cshrc file (in tcsh) and the form
Code:

alias srchfrlmn "cat \!:1 | grep lmn"
works for me.

To use it, I would say something like srchfrlmn alphabet.txt, and the command would look through alphabet.txt and print out instances of lmn.

Therefore, the code you're looking for to pass a variable is \!:1, at least in tcsh aliases.

loren_ryter 04-15-2002 12:29 PM

thanks
 
thanks, yall. that's what i was looking for. especially:

> the code you're looking for to pass a variable is \!:1

you're right i have some homework to do, but reading through man pages to learn more about the ins and outs of unix shells is not it! I'm trying to have my computer make the other elements of my life easier, but its taking over! help! lol (anyway, that's why i'm thankful for a forum like this--people like yall sharing some knowledge ;-)

thatch 04-17-2002 03:18 AM

tail system log
 
Hey mT,

I was intrigued by your last alias listed in your post from above but got:

tail: illegal option -- v

... when I tried it.

Please toss me a hint!

TIA and regards

mervTormel 04-17-2002 06:15 AM

hey thatch, whoops, i run fink here...
Code:

$ wiz tail
/sw/bin/tail /usr/bin/tail

$ tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
...
  -v, --verbose    always output headers giving file names
...

take out the v switch and you should get a clean tail. wiz? wiz is my homegrown command locator a la which/whereis.

thatch 04-18-2002 03:22 AM

Thanks mT. I should have known but I haven't seen tail in any of the fink packages. Which one is it in so I can grab it? TIA!

I like the idea of a homegrown command locator. I did the whereisit alias tip from the recent hint on that and it seems to work great.

Regards,

thatch

sbur 04-18-2002 10:53 AM

tail
 
Unless I'm missing something in the exchange, the tail command isn't a fink thing. It's standard c-shell stuff. (I don't know about other shells as I've only used c-shells and their derivatives like tcsh.)

Same with head.

tail -n xxx.txt give you the last n lines of a text file xxx.txt

head -n xxx.txt gives you the first n lines of the file xxx.txt


I use them all the time and don't have fink installed.

xchanyazy 04-18-2002 11:41 AM

Yes, tail comes with the standard OS X install, however fink gives you an updated version, with, for instance, the -v option.
Code:

[11:39][brandg /Users/brandg] %echo abc > abc.txt
[11:39][brandg /Users/brandg] %cat abc.txt
abc
[11:39][brandg /Users/brandg] %/sw/bin/tail -v abc.txt
==> abc.txt <==
abc
[11:39][brandg /Users/brandg] %/usr/bin/tail -v abc.txt
/usr/bin/tail: illegal option -- v
usage: tail [-f | -r] [-b # | -c # | -n #] [file ...]


thatch 04-18-2002 01:32 PM

Yes, but what's the name...
 
xchanyazy,

Do you know what the name of the package that contains the head and tail commands in fink is? If so, could you please tell me what it is so I can install it? TIA

xchanyazy 04-18-2002 02:24 PM

I'm pretty sure it's textutils.

thatch 04-18-2002 03:03 PM

Yup!
 
Thanks xchanyazy. That was it! :)

Regards,

thatch

macmath 11-17-2004 08:22 PM

Quote:

Originally Posted by xchanyazy
I use my ~/.cshrc file (in tcsh) and the form
Code:

alias srchfrlmn "cat \!:1 | grep lmn"
works for me.

To use it, I would say something like srchfrlmn alphabet.txt, and the command would look through alphabet.txt and print out instances of lmn.

Therefore, the code you're looking for to pass a variable is \!:1, at least in tcsh aliases.

How would you pass an argument to a script in sh ?

Below is just background. You don't have to read it unless you want to know the purpose of the script.
-----------------------
While at work, I like to contact my home computer, which has a dynamic ip. So, I have scripts set up on my home computer to get its wan address from the router (without www.whatismyip.com or other such things) and then upload it to my ftp account on campus. Then I have a script called homeip on my work computer which grabs that ip address from that ftp server. It is now getting old to have to check my frequently changing ip address first before I do a scp, so I wrote the following scripts to try to get around that:

Code:

#!/bin/sh
ip=`/usr/local/bin/homeip`
scp \!:1 shortname@$ip:\!:1

and when that one did not work:
Code:

#!/bin/sh
ip=`/usr/local/bin/homeip`
scpFile=`\!:1`
scp $scpFile shortname@$ip:$scpFile

Ideally, typing
scpThing nameOfFile
would start an scp, with my input only that of typing my passphrase. Because my training in shell scripting comes from reading posts at MacOSXHints.edu or searching at Google.edu, I don't know how to assign outputs of scripts to variables to be used later, except in sh. And I don't know how to pass arguments to scripts except in tcsh (which I learned from this post). That is the reason for my question.

hayne 11-17-2004 10:19 PM

Quote:

Originally Posted by macmath
How would you pass an argument to a script in sh ?

Se the section on "positional parameters" in this document:
http://www.tldp.org/LDP/abs/html/othertypesv.html

macmath 11-18-2004 12:47 AM

Thank you very much, hayne!

Code:

#!/bin/sh
ip=`/usr/local/bin/homeip`
scp $1 shortname@$ip:$1

works like a charm. Thank you as well for sending me the link instead of merely writing the answer—now I have the raw materials for doing something else later.

chaostic_2k1 01-19-2005 04:18 AM

Is it possible to do this in a single alias?
like macmath, i would like to scp from my school's server to my mac.
the normal command would be scp username@address:~/$1 ~/desktop/ with $1 being a file path like st/rt/file.gzip
When I try to alias it, it will not copy the $1 into the alias.
Any help?

hayne 01-19-2005 04:30 AM

Quote:

Originally Posted by chaostic_2k1
When I try to alias it, it will not copy the $1 into the alias.

Are you using the "bash" shell or the "tcsh" shell ?
In bash, aliases are not as powerful - you need to use bash functions to pass parameters.
E.g.:
grepfind () { find . -type f -name "$2" -print0 | xargs -0 grep "$1" ; }


All times are GMT -5. The time now is 10:40 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.