A friend of mine at UDSM is doing a course that requires shell scripting so I agree to take time out an cover the basic of shell scripting, and you are welcome to join us. It really for beginners in shell.
Day 1: Laying the Foundation
What Is a Shell?
A shell is a program that takes commands you typed and tell the operating system to run them. The shell interprets your commands. For example, you may use the shell to enter a command to list the files in a directory, such as ls, or a command to copy a file, such as cp.
Example: ls is a command to list the content of the current folder.
$ lsThe $ is the shell prompt, which tells you the shell awaits your commands. ls listed all the content of the current directory, worry not we will get to these in a moment.
bin/ dev/ initrd.img.old@ media/ root/ sys/ vmlinuz@
boot/ etc/ lib/ mnt/ sbin/ tmp/ vmlinuz.old@
build/ home/ lib64/ opt/ selinux/ usr/
cdrom@ initrd.img@ lost+found/ proc/ srv/ var/
How does it work?
The shell looks for a program — that is, a file with execute permissions — with the name ls. The shell looks at all the directories in your command path. The shell runs the first program found that matches the name, checks if you have permission to excutes it and if you have its excutes it and then displays the results of the program to your screen, as in the second and third lines in the code example.
The command path is stored in the environment variable named PATH
$ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:
/home/eferuzi/apps/go/bin:/home/eferuzi/apps/go/bin
A shell acts as a form of wrapper around the OS, hence the term shell.
Why Use Shells?
Unix was the first popular operating system to break free of the single-shell monopoly, which can still be seen in MS-DOS shell on Windows. In Day 2: we will cover how to break free of the single-shell monopoly on Windows.
The Unix philosophy is that one command should do one thing and do it well. So the complex commands are combination of small commands. In this context, a shell is simply another command — a command that facilitates combining other commands. The ability to combine commands allows you to create new commands, thereby adding value to your operating system.
In addition, most shells allow you to group a number of commands in a file, called a shell script. When you run the shell script file, the shell executes the commands in the script file in order.
What Kind of Shells Are There?
Since there is no monopoly on shells, you are free to run any shell you desire. That’s all well and good,but choosing a shell without knowing the alternatives isn’t very helpful. So lets look at a few main shell out there.
- The Bourne Shell - original Unix shell is known as sh, short for shell, created by Steven Bourne, and has been considered a standard part of Unix for decades.
- The C Shell - was so named because much of its syntax parallels that of the C programming language, and added some neat features to the Bourne shell, especially the ability to recall previous commands (and parts of previous commands) to help create future commands.
- The Korn Shell - like the C shell but also backward compatible with the older Bourne shell syntax.
- Bash, the Bourne Again Shell - it is said "bash shell answered a clear need, a need shown by the initial success of the Korn shell". So bash offers command-line editing like the Korn shell, file-name completion like the C shell, and a host of
other advanced features. - Other Shells - Over the years, a number of other shells have appeared, each with a small but devoted following. These shells include ash, zsh, and rc. Ash is the default shell and appears as sh on the Cygwin environment for Windows.
Choosing a Shell
For example, when administrators assume everyone runs the Korn shell, they may set up parts of the environment that break in strange ways for users of other shells, particularly users of the C shells. So before choosing to another shell make sure it will work given the assumptions made before. Most modern shell are all pretty good anyway.
Changing Your Default Shell
The chsh command, short for change shell, allows you to change your default, or login, shell.
Syntax:
chsh username new_default_shellExample:change user jkpaul to use csh, run the chsh command as follows
$ chsh jkpaul /bin/bashNote that you need to enter the full path to the shell. You may required to type you password for security reasons.The new login shell will be available for use the next time you log in.
That is it for day one, so now we are ready to get into using shell.
NEXT: Day 2: Running Shell and Commands
This wont help only the beginners but all IT personnel who want to know will be much of a help..
ReplyDelete