Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Dramatically reduce the CPU usage of 'top'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Dramatically reduce the CPU usage of 'top'
Authored by: robleach on Feb 21, '04 01:31:08AM
robg says that he aliased top to ttop so he can use both commands easily. On the contrary, I think this is cumbersome. You have to memorize new names for commands. You can use the same name and still be able to run it without the alias just as easily. All you have to do is escape the command. You do this simply by putting a backslash in from of the command. Doing this causes aliases to be ignored. Observe:


[dantooine:~] robleach% which ls
ls:      aliased to ls -F
[dantooine:~] robleach% which \ls
/bin/ls
[dantooine:~] robleach% ls
Applications/                   Library/                        School/
...
[dantooine:~] robleach% \ls<BR>
Applications                    Library                         School
...

I only make other names for commands when I like to occassionally run the commands in different ways. For example, my .aliases file contains:


alias ls "ls -F"
alias l "ls -alF"
alias ld 'ls -Fa | grep "/"'

While I'm talking about aliases, here's another tip. Use \!* when you want to insert arguments supplied to an alias in a specific place. For example, I have this alias for my xemacs command:


alias xe 'xemacs \!* &'

This allows me to make a call like `xe myfile.txt` and the alias puts the "run in the background character: '&'" at the end of the command.

I suppose this is probably all stuff a lot of you already know. If so, give me a break. I've been couped up in my apartment too long with a sinus and ear infection. I'm going stir crazy. :o)

Rob

[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: Graff on Feb 21, '04 03:11:26AM

One big potential problem with doing this is what if a script or application tries to use the command without using its entire path. Yeah yeah I know you should always use the entire path of a command in a script or application but sometimes programmers take shortcuts.

Since you have modified the default command there is now a huge potential for disaster. The application will call your alias instead of the default command and will use your customizations in addition to any that it adds. Depending on the circumstances this could cause an innocent script which cleans up files to go amok and erase something unintended. It's probably unlikely but why take the risk.

When dealing with the command line it is almost always better to be as safe as possible. Your best bet is to bite the bullet and make an alias that is similar, but not the same as an existing command. Do something like repeat the first or last letter of the command and be consistent with it, that way you only need to remember that simple rule to get to all of your common alternate commands. Something like ttop is not that hard to remember especially if you also have other alternates like lls, mmv, or rrm.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: adrianm on Feb 21, '04 03:23:42AM

True, but generally, shell scripts should be writtin in the basic /bin/sh (bourne shell) as this is the only shell pretty much always going to be there and a lot less susceptible to modification from the global environment.

So if you but #!/bin/sh at the top of your script, you won't be picking up any aliases.

Also, aliases in your .bashrc, .zshrc (for bash/zsh) won't be available in scripts as they only apply to interactive shells.

.cshrc and csh/tcsh - well, if you use that shell, you deserve everything you get.



[ Reply to This | # ]
Dramatically reduce the CPU usage of 'top'
Authored by: FenrisUlf on Feb 22, '04 02:13:42AM

True, writing shell scripts, you should use /bin/sh

...
(not that you were trying to incite yet another shell war....)

However for an interactive shell, it doesn't matter which one you use.... some people prefer csh/tcsh.... If you don't program in the shell, there's nothing saying you can't use it. (since tcsh has just as much usefulness as any other shell in interactive mode.)

... if you use Windows, you deserve what you get.


---
---
Who are you that walk across the graves of giants at this late hour?



[ Reply to This | # ]