Expt 2 - COS - 008
Expt 2 - COS - 008
Expt No: 2
Title: Shell Programming and system calls.
COs to be achieved:
CO 1: Explain the fundamental concepts of operating system
CO5: Explain basic features of Unix and Mobile OS.
Theory:
The shell provides you with an interface to the UNIX system. It gathers input from you and executes
programs based on that input. When a program finishes executing, it displays that program's output.
Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of execution. A
good shell script will have comments, preceded by a pound sign, #, describing the steps.
NOTE: Before adding anything to your script, you need to alert the system that a shell script is being
started. This is done using the shebang construct. For example −
#!/bin/sh.
2. Write a shell script that accepts integer and find the factorial of the number.
On a typical Linux system, several shells are commonly used. Bash (Bourne Again Shell) is the
most widely used, providing features like advanced scripting capabilities, command history, and job
control. It is the default shell on many distributions due to its extensive functionality. Sh (Bourne
Shell) is one of the oldest Unix shells known for its simplicity and portability, often serving as a
standard for basic scripting and compatibility. Dash (Debian Almquist Shell) is a lightweight,
POSIX-compliant shell optimized for speed and efficiency, commonly used as `/bin/sh` on Debian-
based systems. Zsh (Z Shell) is favored for its advanced features, including improved tab completion
and a customizable prompt, making it popular among power users. Ksh (Korn Shell) offers
advanced scripting capabilities, including built-in floating-point arithmetic and associative arrays,
useful for complex scripting needs. Tcsh (Tenex C Shell) enhances the C shell (`csh`) with features
like command-line editing and filename completion, preferred by users of C shell syntax. Fish
(Friendly Interactive Shell) emphasizes user-friendliness with features like syntax highlighting and
autosuggestions, appealing to those who prioritize an intuitive interactive experience.
To find out which shell you are currently using on a Linux system, you can use
several methods:
1. `echo $SHELL`: This command prints the path of the current shell as specified
in your user account settings. For example, it might output `/bin/bash` or
`/usr/bin/zsh`.
2. `ps -p $$`: This command shows information about the process with the ID of
your current shell. The output will include the shell’s name. For example, it might
show `bash` or `zsh` as the command associated with the shell process.
3. `echo $0`: In some cases, this command can display the name of the shell used to
invoke the current script or session. However, it might not always reflect the
current interactive shell.
4. `cat /etc/passwd`: You can look at your user entry in the `/etc/passwd` file. Find
your username and check the last field, which shows the default shell for your
account. For example, it might show `/bin/bash` or `/usr/bin/zsh`.
Each of these methods provides information about the shell you are currently using
or the default shell configured for your user account.
Shell scripting offers a variety of advantages and disadvantages, making it suitable for certain tasks
while less ideal for others.
One major advantage of shell scripting is its simplicity and ease of use. Shell scripts are typically
written in plain text and can be executed directly in a terminal, making them accessible for
automating routine tasks and system administration. They provide a straightforward way to combine
and automate command-line operations, making repetitive tasks more efficient. Additionally, shell
scripts are highly portable and can run on any Unix-like system, including Linux and macOS,
without needing modifications. This makes them an excellent choice for cross-platform automation
tasks.
However, there are notable disadvantages to shell scripting as well. Shell scripts can become
difficult to manage and debug, especially when they grow in complexity. The lack of advanced error
handling and debugging tools can make troubleshooting challenging. Furthermore, shell scripting
often lacks the robustness and performance of compiled programming languages, which can be a
limitation for more complex or performance-sensitive tasks. Security concerns are also relevant, as
shell scripts can be prone to issues like command injection if not written with care. This requires
careful scripting practices to ensure that scripts do not inadvertently expose systems to security
vulnerabilities. In summary, while shell scripting provides a simple and effective way to automate
tasks and manage system operations, it may not be suitable for all scenarios, especially those
requiring advanced error handling, performance optimization, or robust security measures.
Conclusion:
Thus, we have completed the study of the shell script and written the program using shell.