Sei sulla pagina 1di 12

About

UNIX Shell
Objectives
In this session, you learn about:
• Unix Shell
• Features of Shell
• Executing Shell Scripts
• Debugging Shell Scripts
• File Descriptors
• I/O Redirection
• Pipes
UNIX Shell
• Unix Shell is a command interpreter which executes
the user commands by interacting with the kernel
and displays the output to the user.
• An environment where we can run our commands,
programs and shell scripts.
- Bourne Shell(sh)
- Korn Shell(ksh)
- C Shell(csh)
- Bourne Again Shell(bash)
• Each flavor of the shell has its own recognized
commands and functions.
• The command ‘echo $0’ prints the current shell.
Features of Unix Shell
• Interprets Commands

• Allows setting variables

• Redirects I/O

• Pipes

• Customize your environment


Shell scripts

• A list of commands which are listed in the order of


execution.

• Can be created using the editors like vi/emacs.

• Shell script begins with the hash bang symbol to identify


under which shell the script has to execute.
– #!/bin/sh : Bourne shell
– #!/bin/ksh : Korn shell
– #!/bin/csh : C shell
– #!/bin/bash : Bourne Again shell
Executing Shell scripts
1) /scriptpath/sample.ksh
- File should have the execute permission.
- The script will execute in the shell that is declared on the first
line of the shell script.
- If no shell is declared on the first line of the shell script, it will
execute in the default shell(user defined shell).

2) ksh /scriptpath/sample.ksh
- Creates a korn shell and execute the script in the newly
generated shell.

3) . /scriptpath/sample.ksh
- Executes in the parent shell itself. It will not create a new
shell for execution.
Debugging Shell scripts

• set [option] in shell script (or)


ksh [option] while executing the shell script

- x: Echoes each line in the script along with the


output of the line.

- n: Checks for syntax errors without running the


script.

-v: Echoes each command before executing it.


File Descriptors
• File Descriptor is a handle to open I/O resources.

• Three different file descriptors are

• 0 – stdin (standard input)


• 1 – stdout (standard output)
• 2 – stderr (standard error)

File Descriptor 0 reads the input the user through the


keyboard.
File Descriptor 1 & 2 writes the output and error
messages to the computer screen(ie. Monitor)
Input Redirection
• Input Redirection:

< - Used to read the input from the file/user.

Ex. cat < sample.txt – Read the content of the sample.txt


file.

• Here Document:

- Used to read the input from the user until the LABEL keyword
is encountered.
<< LABEL

Ex. sample.ksh << EOF


Output Redirection
• Output Redirection:
> - Used to redirect the standard output & standard error to
files in overwrite mode.
>> - Used to redirect the standard output & standard error
to files in append mode.

Ex. cat > sample.txt – Overwrite the contents of the


sample.txt file.
cat >> sample.txt – Append to the sample.txt file.
command > output.txt 2> err.txt – Command output & error
are captured in different files.
command > output.txt 2>&1 – Command output & error are
captured in a single file.
Pipes
• Used for inter process communication.

• The output of the one command is given as input to


the other command.

• The operator “|” is used between two commands to


perform the inter process communication.

Ex. cat sample.txt | grep 'abcd'


Summary

In this session, you learned about …


• Unix Shell
• Features of Shell
• Executing Shell Scripts
• Debugging Shell Scripts
• File Descriptors
• I/O Redirection
• Pipes

Potrebbero piacerti anche