Using The Shell VII
Summary:- Command Aliases
- Shell Functions
- Where To Go From Here
- Shell FAQ
- How do I turn that &*#1.1 beep off?
- Why do I get bash: command: command not found?
- The execution bit is set, I have permissions to execute but permission is denied anyway. Why?
- How do I change the file listing colors?
- I've put a script 'foo' into my '~/bin' directory, but every time I try to run it, a different command also called 'foo' is started. Why?
- What does bash: command: bad interpreter mean?
- My terminal freezes everytime I press
1.1
Command Aliases And Shell Functions
Remembering all sorts of commands and their options and typing them each time is tedious work. Fortunately you don't have to. You can define shortcuts for frequently used commands. These shortcuts can either be defined in the relatively simple form of command aliases or in the somewhat more complex syntax of shell functions.Command Aliases
For example, I use this command to upload my stuff to MUO:rsync -e ssh -z -t -r -vv ––progress /home/tom/web/muo/rsmuo/docs muo:/www/mandrakeuser/docs
alias upmuo='rsync -e ssh -z -t -r -vv ––progress /home/tom/web/muo/rsmuo/docs muo:/www/mandrakeuser/docs'
alias shortcut='command'
export MUOHOME=$HOME/web/muo/rsmuo/docs
alias upmuo="rsync -e ssh -z -t -r -vv ––progress $MUOHOME muo:/www/mandrakeuser/docs"alias rm='ls -l'
Some aliases that might be useful (don't forget the quotes1.1 ):
- . Now rpmq string will list all installed RPMs which contain string in their name. *
alias rpmq='rpm -qa | grep'
.alias ls='ls -ho ––color | more'
ls will now print a colored and paged listing with file sizes in KB. *.alias use='du ––max-depth=1 | sort -n | more'
use gives you a paged list of subdirectory sizes ordered by size. **. Frequently used directories can be aliased as well. Other prospective candidates are for example the subdirectories of '/mnt'.alias dkd='cd /usr/src/linux/Documentation'
Aliased directories may also be on removable media:.alias dlm='/mnt/cdrom/Mandrake/RPMS/'
Shell Functions
Writing shell functions already boarders on the topic of shell scripting, which is beyond the scope of this article (and mine ;-)). In fact, shell functions are shell scripts, but they have the advantage of being preloaded and being executed in the same shell (whereas a shell script opens at least one sub-shell). With shell functions, you can do a lot of things you can't do with aliases. One example: function apros() { apropos $1 | egrep -v '(3|(n)'; } This defines a new command, called 'apros'. apros name will execute 'apropos name' (i.e. a search command for man pages) and pipe (|) the output of that command through 'egrep' which filters out all man pages from sections '3' and 'n', which usually are not of interest, but tend to mess up the output of the 'apropos' command.Functions allow you to use arguments given to the function name at any place of the function command. With aliases, only one argument is allowed and that argument has to be at the end of the command line (like in the 'rpmq' alias above).
'$1' is a so-called 'positional parameter', it's a placeholder for the first argument given to the function. Of course there are more. function apros() { apropos $1 | egrep -v "($2"; } If you now run the 'apros' command like this: apros name man_section_number it searches for name, but excludes all man pages from man_section_number: apros menu 3 returns all man page titles which contain 'menu', except those from section 3 (programming). Notice that you have to quote twice and that you have to use double quotes:
- You must quote the 'egrep' search pattern in order to protect it from the shell. * You must use double quotes to get the second positional parameter interpreted correctly. * You must quote the round bracket again in order to tell 'egrep' to take it literally and not as a special control character .
Where To Go From Here
This article is but the beginning. Shell scripting can help you to automate a lot of tasks, to fix errors in scripts by others yourself and to accustom your Mandrake Linux system to your liking in (almost) every aspect. If you plan to learn one of the more complex programming languages out there, shell scripting is a good place to start because the basic concepts are similar. The BASH Programming - Introduction HOW-TO will explain the topics covered in this article in more depth and introduce you to the world of shell programming. You can then continue with the very recommendable (and free) Advanced Bash-Scripting Guide by Mendel Cooper. If you prefer books, I can recommend S. Veeraraghavan's 'Teach Yourself Shell Programming', Sams Publishing. In contrast, I found 'Learning the bash Shell' by Newham/Rosenblatt, O'Reilly, rather unhelpful and confusing, but maybe that's just me ;-). Apart from that: practice, practice, practice. Read shell scripts by others and try to understand what they do, how and why. Don't run your test scripts as 'root'. Have fun. section indexShell FAQ
How do I turn that &*#1.1 beep off?
With this command: setterm -blength 0Why do I get bash: command: command not found?
If it isn't a typo, most likely the command you are trying to execute is located in a directory which is not part of your $PATH. Supply the full path to the command. If you are in the same directory as the command, do ./command. 1.1 Why do I get bash: command: Permission denied? In order for a file to be executable, the execute permission must be set for the user who wants to execute the file. Do a: chmod 755 file If that doesn't work, read the article on permissions.The execution bit is set, I have permissions to execute but permission is denied anyway. Why?
Check the '/etc/fstab' entry of the partition where that file is. Make sure it doesn't contain the option 'noexec'. If it contains the option 'user', the option 'exec' must be set, too.How do I change the file listing colors?
Copy '/etc/DIR_COLORS' to your home directory and rename it to '.dir_colors'. Everything you need you'll find in that file.I've put a script 'foo' into my '~/bin' directory, but every time I try to run it, a different command also called 'foo' is started. Why?
Have a look at your $PATH and you will see that your personal '~/bin' directory is the last or at least very close to the end of the $PATH. The shell will search the directories listed in $PATH one after the other. As soon as it finds the first matching command, it executes this command. If there is a command on your system with the same name as your script, it is likely that this command will be executed, not your script. So rename your script.What does bash: command: bad interpreter mean?
This usually happens with third party installation binaries or Java applications. These applications need a shell of their own, so instead of command you have to run sh commandMy terminal freezes everytime I press 1.1
Don't do that then ;-).
Using The Shell VII
Version 1.10 last modified by Flink on 28/08/2006 at 10:48
Version 1.10 last modified by Flink on 28/08/2006 at 10:48
Document data
- Lost account?
- Join the community, be part of the Club: it's free!
- Get the PWP Download Subscription!


