Scripting in bash


To get started, you can create some command aliases to simplify a few command-line tasks. Aliases in Linux are environment variables that behave in the same way they sound as another name for a command. You can define aliases on-the-fly at the command line, but those are good for only one session. It's preferable to have these available to you all the time.

Before beginning to write your script, take a look at the default aliases defined by SUSE. Go to a shell and type alias at the prompt. You will see the following listing. If you are migrating from Windows, try not to be too offended.

alias +='pushd .' alias -='popd' alias ..='cd ..' alias ...='cd ../..' alias A:='echo -e '\''Error: There is no such thing as a drive      A: in Linux.\n If you want to access your floppy, try "mount /dev/fd0"      and then look\n in the directory /media/floppy !'\''' alias C:='echo -e '\''Error: There is no such thing as a drive      C: in Linux!\n Your harddisk should already be mounted (via /etc/fstab or      autofs).'\''' alias a:='A:' alias beep='echo -en "\007"' alias c:='C:' alias cd..='echo '\''Error: Try: cd ..'\''' alias chkdsk='echo -e '\''Error: Your filesystems are     checked on bootup.\n If you want to do it manually, use fsck.\n Use df and mount for an overview of your disks.'\''' alias copy='echo '\''Error: Try the command: cp -piv'\''' alias del='echo '\''Error: Try the command: rm -iv'\''' alias dir='ls -l' alias format='echo -e '\''Error: The DOS concept of formatting      disk media is screwed.\n If you want to create a filesystem use "mkfs". To format      a floppy, use\n "fdformat /dev/fd0" and then "mkfs.minix /dev/fd0".'\'' #' alias l='ls -alF' alias la='ls -la' alias ll='ls -l' alias ls='/bin/ls $LS_OPTIONS' alias ls-l='ls -l' alias md='mkdir -p' alias mem='echo '\''Error: Try the command: free'\''' alias move='echo '\''Error: Try the command: mv -iv'\''' alias o='less' alias rd='rmdir' alias rehash='hash -r' alias sys='echo -e '\''Error: Linux cannot be transferred     like that.\n If you want to install Linux or create bootfloppies,     please use YaST.'\'' #' alias unmount='echo "Error: Try the command: umount" 1>&2; false' alias ver='echo '\''Error: Try the command: uname -a'\''' alias which='type -p' alias you='yast2 online_update' 

These aliases are set in the systemwide environment settings files: /etc/profile and /etc/bash.bashrc and they apply to every user by default. Any user can customize his or her own aliases by modifying the ~/.bashrc file and you can write a script containing any aliases you want and then reference that script in .bashrc.

For example, open a text editor and type these lines:

#!/bin/sh alias ls='ls -l' alias ldir='ls -aF' alias copy='cp' alias ft='fortune -a' 

Here's what you've done with this script:

  • In line 1, you declare that this is a (bash) shell script. This line has several colorful nicknames that refer to the two characters at the front: hash-bang, sh-bang, or just plain bang.

  • Line 2 tells bash that when you type ls to get a directory listing, you always want it to give you the detailed (long) listing, with permissions, size, and date/time information. This results in the same display as the default alias ll.

  • Line 3 gives you a list of files and directories in color, with no details, when you type ldir.

  • Line 4 changes what the shell does when you type the DOS command copy: instead of getting the slightly rude reminder of proper Linux syntax, the shell will actually do what you meant it to.

  • Line 5 defines a shortcut to getting a fortune cookie as ft. The -a switch means that fortune will consult all of its fortune collections and may result in something crude displayed on your screen. Use this switch carefully.

Add other aliases as you choose, and then save the file to ~/bin/myaliases. There's no extension required. When you're done editing your script, you have two tasks remaining. First, this is just a text file as it is. To make it a script, it must be executable. Open your shell, change to the /bin directory and type chmod +x myaliases. Because you have ownership of all files in your home directory, this file is now executable.

Tip

Get into the commenting/documentation habit early in your scripting career. At the top of each script, explain what the purpose of the script is. For the preceding example, the comment could be

#This script adds some directory listing #aliases to the default set. 

Be sure to add the hash mark (pound sign) to the beginning of each line of your comment, otherwise the shell will try to interpret your comment as a command.


To add these aliases to your basic configuration, open ~/.bashrc in your editor and add this line to the bottom of the file:

~/bin/myaliases 

Save and exit. Restart your computer. Open a shell and type alias. Your four new aliases should now be on the list.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net