Académique Documents
Professionnel Documents
Culture Documents
Commands
Rachit Bajpai
What is a SHELL ?
What is a Shell ??
A Unix shell, also called "the command line", provides the traditional user
interface for the Unix operating system. Interprets the commands given
by the user and interacts with the kernel to execute the commands
Example of shells
/bin/bash GNU Bourne Again Shell
/bin/sh Bourne Shell
/bin/csh C Shell
/bin/tcsh TENEX C shell
We shall be taking BASH as it is the most popular one
BASH Bourne Again Shell
What are these ??
konsole
gnometerminal
Eterm
aterm
xterm
Any guesses ???
What are these (contd..)
Actually they are terminal emulator for the X
Window System
.bashrc
✔
Every thing in this file is executed every time
BASH is loaded.
✔
Located in your home directory.
✔
Its a hidden file.
✔
DEMO
Some UNIX commands
Unix Commands
You already know these
✔
mkdir
✔
cd
✔
rm
✔
mv
✔
kill
✔
ls
Here are some more
less
cat
echo
export
wc
grep
GREP and Pattern Matching
'.' The period matches any single character
'?' The preceding item is optional
'*' The preceding item is matched 0 or more times
'+' The preceding item is matched 1 or more times
{M} The preceding item is matched exactly M times
{M,N} The preceding item is matched more than M times but less
than N times
Examples
More Examples
grep '\(abc\)*' file matches abc , abcabcabc etc.
(i.e. , any number of repetitions of the string abc , including the
empty string.)
grep [Hh]ello file matches lines containing hello
or Hello
Ranges:
[03] is the same as [0123]
[ak] is the same as [abcdefghijk]
[AC] is the same as [ABC]
[ACak] is the same as [ABCabcdefghijk]
Shell Scripting
Shell Scripting
●
Why Scripting is needed ??
●
Different Scripting Languages
Shell Script
Perl
Python
●
Which one to be choosen
●
#!/bin/bash
Command Execution in Shell
✔
Command on Shell is interpreted
✔
Escape Characters are replaced
Example: * is replaced by all the files
in the current directory
Shell Scripting
A shell script is little more than a list of commands that are run
in sequence. Conventionally, a shell script should start with a
line such as the following:
#!/bin/bash
This indicates that the script should be run in the bash shell
regardless of which interactive shell the user has chosen. This is
very important, since the syntax of different shells can vary
greatly.
Hello World
Lets start with our traditional example
#!/bin/bash
echo “Hello World”
ls #list all the files
Notice a few thing
●
# Means a comment
●
Save it to a file helloworld.sh and change
permission to be executable
ie. chmod +x helloworld.sh
Variables
Any programming language needs variables. You
define a variable as follows:
X="hello"
and refer to it as follows:
$X
Variables (Contd..)
BASH gets unhappy if you leave a space on either side of the =
sign. For example, the following gives an error message:
X = hello
Quotes are not always necessary. Where you need quotes is
when your value include spaces. For example,
Single Quotes versus double quotes
Basically, variable names are expanded within
double quotes, but not single quotes. If you do not
need to refer to variables, single quotes are good to
use as the results are more predictable.
Variables (Contd..)
Example:
#!/bin/bash
echo -n '$USER='
echo "$USER"
echo "\$USER=$USER"
Environment Variables
The bash environment variables are commands that
are simply expected to be there. As long as bash has
the right pointers, it will fulfill your commands
quickly.
You can look at them by the shell command 'env'
$env
examples: PATH, http_proxy, ftp_proxy
Piping
Its a very powerful feature of BASH
You can pipe the output of one command to another
command.
Syntax
$command1 | command2
DEMO
InputOutput Redirection
You can redirect the output of a command to a file
rather than to a screen.
$ls > a.txt
This will create a new file. For appending use >>
Note: Only the output of stdout is redirected not of
stderr. (How to do that, Assignment Question 1)
Conditionals, if/then/elif
if condition1
then
statement1
..........
elif condition2
then
statement3
statement4
........
fi
Demo
Loops
The for loop is a little bit different from other programming
languages. Basically, it let's you iterate over a series of 'words'
within a string.
The while executes a piece of code if the control expression is
true, and only stops when it is false (or a explicit break is found
within the executed code.
The until loop is almost equal to the while loop, except that the
code is executed while the control expression evaluates to false.
DEMO
Assignment
1. How to redirect the output of stderr to a file ?
2. Write a shell script to find the number of FTP
servers in your hostel. (Prize for the one who
mails me the script first.)
3. Write a shell script to list all files in a directory
which were modified in last 24 hours.
Questions ???
Thank You
Rachit Bajpai
rachitb@iitk.ac.in