You are on page 1of 5

AN ONLINE TRAINING ON UNIX SHELL

C.P. RAVIKUMAR, TEXAS INSTRUMENTS, INDIA

INTRODUCTION
A shell is an interactive command-line interface to an operating system. There are several Unix shells; among them, “tcsh” and
“bash” are both popular. “tcsh” is a modern version of the “csh” program, whereas “bash” is a modern version of the “sh” program.
They have similar capabilities, but the command interface has minor syntax variations. A “shell script” is a sequence of shell
commands which achieves something meaningful. You can also call it a “shell program.” Shell commands include ways to (a) declare
variables (b) assign variables and manipulate them e.g. carry out arithmetic operations (c) perform if-then-else type of control
sequencing based on the settings of variables and (d) iterations using “for” and “while” constructs. Shell scripts are a great way to
improve your personal productivity. As an example, suppose that you receive a “ZIP” file from a friend which contains a number of
files, including some PDF files. You wish to print each PDF file and then remove the PDF file since it takes too much file space. A shell
script can be written to perform these actions without manual intervention. You can write the script to receive the folder as an
argument, so that the script can be reused again, if required. While graphical user interfaces to operating systems are easy to use,
you will need to learn a command-line interface to become more productive in an industry.

GET STARTED!
In this online training, you will learn to use “bash.” You will need access to a computer with an Internet connection.

STEP 1. Visit https://www.tutorialspoint.com/execute_bash_online.php

This website provides an interactive shell where you can practice “bash” commands. When you visit this website, you will
see three windows. Of these the “green” window at the bottom is where you can type “bash” commands. The window on
the top right is where you can create shell scripts. If you logout and go back to the website again next day, your work will be
lost – you will start with a clean slate again! It is recommended that you do all the commands mentioned in Step 2 in one
sitting (You can take breaks, but do not logout.).

STEP 2.
Visit the following website for online documentation of commands in “bash.” https://ss64.com/bash/ The commands are
listed in the alphabetical order. Clicking on the name of the command can provide additional details about the command.
Although you will see a whole lot of commands, we recommend that you go through them in the following order. Shell is
case sensitive! Be careful about the spaces between commands – sometimes, the command will be interpreted differently
depending on whether a space is provided/not provided.

Command to be typed Explanation


1 mkdir mydir Create a directory called mydir
2 cd mydir Change the present working directory to mydir.
Simply running “cd” will put you in the home
directory. To change to a directory one level above,
use “cd ..” and to change to a folder called mydir
under the current working directory, use “cd
./mydir”. Use “~/mydir” if the “mydir” is in the
home directory.
3 pwd Check which directory you are in. It should show
that you are in “mydir”.
4 echo ”Hello World” Echo back the arguments of the program.
Tip Back Arrow key Use the back arrow key to repeat the previous
command! Repeat to locate commands from your

ONLINE TRAINING FOR INTERNS - TEXAS INSTRUMENTS Page 1


history.
5 User-defined Variables x=1 will set the variable x to constant 1. (No
spaces before/after the = sign).
6 echo echo “Value of x is $x” … $x is a way to access the
value of variable x.

7 expr expr $x + $y will print the sum of variables x and


y. Also try subtraction.
8 help help <commandname> will print a short summary of the
command
9 history Prints the history of the commands you have typed
10 echo “Hello World” > f1 Creates a file called f1 and puts the output of echo
command in it. This is called output redirection.
11 if test $x = 1 ; then This is an if statement. “test” is a program that
echo "helo"; fi checks if a given condition is true or false. It is
important not to omit the spaces before and after
the “=” sign.
12 help test Shows all the conditions that “test” program can
check. Try “test –a f1” and “test –f f1” within an
“if” statement, as in command [11] above. Try “help
test > test.help” and explain what this does. Use
“help test | more” and see what happens. See the
“pipe” construct (command 40).
13 expr $x > 1 This is supposed to check if $x is greater than 1.
But “>” will result in output file redirection as in
command [10] above. To avoid this, try the next
command.
14 expr $x \> 1 Avoids the special meaning for the “>” character. If
the variable 1 is set to 1, you will see a “0”
(“false”) as the output. Otherwise, you will see a
“1” (“true”). Get “help” on “expr”. Try out other
commands such as expr $x \< 1, expr $x != 2, etc.
15 ls Shows the listing of the files in your directory.
Explain each of the files you see. Notice that the
list of the files is sorted on their names. Try “ls
–r” : the files will be listed in the reverse order
of their names.
16 ls -l Shows additional information about each file. You
will be able to find the date and time at which the
file was last updated/created and the size of each
file.
17 ls -t You can see that the files are ordered in the order
in which they are created; the most recent file will
appear on the top. Try creating another file by
running “history > h” and try command [17] again.
What do you notice?
18 ls –S Files will be ordered based on their size; the
larger files will appear first.
19 ls –t -r Reverse the time order when displaying the names of
the files
20 ls –S -r What do you conclude about the order in which files
are displayed?
21 ls –l –r -t Shows the most recent file at the end.
22 ls -lrt This is another way to run the command [21]. Also
try “ls –trl” and “ls –rtl”. What do you infer?
23 help ls “-l”, “-r”, “-t” and “-s” are called options. Find
out about other options to “ls” command.
24 cp f1 f1.new Copies file f1 to f1.new; verify that this is the
case using “ls –l”

ONLINE TRAINING FOR INTERNS - TEXAS INSTRUMENTS Page 2


25 mv f1 f1.old Moves file f1 to f1.old; the original file f1 will
be removed. Verify this using “ls –l”
26 rm f1.old Removes f1.old. You cannot recover it back.
27 cp f1.new f1 From the backup of f1, recreate it.
28 rm –i f1 Gets your permission to remove the file; type “y” if
you wish to remove, else type “n”.
29 mkdir subdir Creates a sub-directory under mydir

30 cp f* subdir Copy all files whose names begin with “f” to subdir.
Notice that “*” is a wildcard character in shell.
Tip2 After typing cp f*, type s “bash” will help you complete the filename “subdir”.
and then type ESCAPE. You can try “ESCAPE” any time when you are trying to
type a file name.
31 echo * Echoes the names of all files in the current working
directory!
32 echo “*******” This is the correct way to print a string of
asterisks! Don’t forget the quotes. (What will
happen if you do? Go to [31] if you forgot.)
33 ls –l subdir/* Shows all files in subdir
34 ls –l subdir/f* Shows all files in subdir, but their filenames must
begin with “f”
35 help > helpfile; cat Two commands are separated by a “;”. They will be
helpfile executed one by one, from left to right. You can use
“;” to separate more commands. “cat” is a way to
type out files. “cat” is a short form for
“concatenate.”
36 echo “Hello” > h; echo We created three files called h, w, and hw. The file
“World” > w; cat h w > hw; hw is the concatenation of files h and w. Verify
cat hw this by ls –l.
37 more hw Allows you to view the file hw in leisure. Use space
bar to view more of the file. Use “b” to go
backward. Use “q” to quit browsing the file.
38 less hw Another way to browse the file. Similar to “more.”
39 wc –l h Shows the number of lines in file “h”. Also try “wc
–w” to get the number of words, and “wc –c” to get
the number of characters.
40 cat h w | wc -l This is an example of the “pipe” construct in shell.
The two commands are executed side by side (not one
after another). The output of “cat h w” command will
be sent as input to the “wc –l” command. Thus wc –l
will be applied to a “ghost” file which contains the
concatenation of files h and w.
41 ls ? “?” is the set of all files which have a single
character name such as “h” and “w”. Similarly, try
“ls ??” or “ls h?”. Note that “?” is sometimes
useful in locating files that match a particular
pattern.
42 seq 1 5 The list of numbers from 1 to 5 is printed
43 x=$(seq 1 5); echo $x The first command will run “seq 1 5” and return its
output to a variable called x. The second command
echoes the variable x.
44 allfiles=* Explain what happened. Verify by doing “echo
$allfiles”
45 for file in $allfiles When you type the first line with the “for”
do statement, you will see that “bash” puts out a “>”
wc –l $file prompt. Type the other commands after “>” prompt.
done This “for” loop ran a “wc –l” command on every file
in the directory.
46 x=10 This is the “while” loop in bash. What is the value

ONLINE TRAINING FOR INTERNS - TEXAS INSTRUMENTS Page 3


y=0 of variable y after the loop completes? Understand
while test $x \> 0 what is going on inside the loop. Refer to [43]
do above to understand the syntax y=$(expr …).
y=$(expr $y + $x);
x=$(expr $x - 1);
echo $y;
done
47 head -5 hw Prints the first 5 lines of file “hw”. Try “head -1”
and “head -2500”. What do you conclude?
48 tail -5 hw Prints the last 5 lines of “hw”. Also try “tail -1
hw” and “tail -2500 hw” commands. What do you infer?
49 tar cvf hwfiles.tar h* w* Creates an archive of files whose names begin with h
or w. The name of the archive file (also called “tar
file” or “tar ball”) is hwfiles.tar
50 gzip hwfiles.tar Creates the file hwfiles.tar.gz after compressing
the file hwfiles.tar.
51 gunzip hwfiles.tar.gz Uncompresses hwfiles.tar.gz and creates hwfiles.tar
52 tar tvf hwfiles.tar Show the table of contents in hwfiles.tar
53 rm h* w* Remove all the files which we archived
54 tar xvf hwfiles.tar The files got recreated from the archive!
55 cp –r subdir subdir.backup Creates subdir.backup folder and copies all files in
subdir to it recursively
56 rm –r subdir Remove the files under subdir and then remove the
folder itself
57 mv subdir.backup subdir Rename the folder subdir.backup to subdir
58 vi greet.c Use the “vi” editor to create a file called greet.c.
Do this after you do Step 3 below.
59 echo “ramesh” > names; Creates a file called “names” and writes “ramesh"
echo “suresh” >> names into it. The second command appends another line to
“names” and puts “suresh” in it.
60 echo “mesh” > topologies; Similar to [59] above. Verify by doing a more on the
echo “star” >> topologies files created.
61 grep esh * Check each file if the pattern “esh” appears in any
line. Print the lines.
62 grep –c ram * Shows the count of the number of times the pattern
“ram” appears in each file
63 grep –v esh topologies Show the lines that do not match the pattern “esh”
in the file topologies
64 grep “^r” * In every file, locate lines that begin in “r” and
print them
65 grep “h$” * In every file, locate lines that end with “h”
andprint them
66 alias type=”cat” We have created an alias called “type” for the
command “cat”. Try “type topologies” and verify that
it works.
67 unalias type Remove the alias
68 gcc –o greet greet.c Runs the C compiler and creates the program “greet”.
Do this after you complete entering greet.c using
“vi”. If you notice errors, you may have made
mistakes in typing. Edit the file and correct the
errors.
69 ls –l greet Notice that the pattern -rwxr-xr-x appears against
“greet” executable. This should be interpreted as (–
)(rwx)(r-x)(r-x). The first (-) means “greet” is not
a directory (directories will have a “d”). (rwx)
means the user has read, write, execute access. (r-
x) means the group has read and execute access only.
The second (r-x) means the others have read and
execute access.

ONLINE TRAINING FOR INTERNS - TEXAS INSTRUMENTS Page 4


70 chmod o-x greet Remove the execute access for others
71 chmod g-r greet Remove the read access for others. Do an “ls –l
greet” and see the access permissions have changed.
72 id Shows the user id, group id of the user
73 set Shows current settings of shell. There are many
useful settings. Find out more about them!
74 set noclobber Set this variable to prevent accidental overwriting
of files through output redirection (“>”).
75 ps -l Shows the processes running under your name. Also
try ps –aux and the command “top” to view the
processes. Run the help command to find out more
about each field of the output of ps command.
STEP 3. The “vi” editor is one of the most popular ones among Unix community. You can learn to use “vi” at
http://www.openvim.com/ First, go through the tutorial and try the commands given below. [You can also do this in Step 2 above, if
you invoke “vi greet.c” as in Step 58. Discover the power of “vim” by doing more exercises suggested in the website above. Several
other editors are available in Unix, such as “emacs” and several editors with GUI. You may use any one of these as per your
convenience.

Command to be typed in vi Explanation


i Begins to insert text. This is the “INSERT” mode.
ESCAPE Stops inserting text into the file. This is the “ESCAPE”
mode.
i#include <stdio.h> Enters the first line and second line of the program into the
#include <stdlib.h> file
ESCAPE Stops inserting text.
Arrow keys Navigate within the file.
x Delete the character under the cursor
u Undo the change
5xu Delete 5 characters and then undo the change
Position on line 2 and You will now be appending text into the file
then type “a”
main() Enter the program and then go to the ESCAPE mode.
{
printf(“Hello World\n”);
}<ESCAPE>
:w<RETURN> Save the file
:1d Delete the first line
:1,2d Delete lines 1..2
uu Undo the previous two statements
/Hello Position the cursor on the line where “Hello” appears
n Search for the next occurrence of “Hello”
:s/World/World\!/ Replace World by World!
:set nu Show line numbers
:2 Go to line 2
:set nonumber Do not display line numbers
:w Save your file again
:1,3t$ Duplicate the lines 1..3 after the last line
u Undo the previous command
Control-r Redo the previous command
Uu Undo previous two commands
:wq Save your file and quit. If you do not wish to save, use
“:q!”
:q! Quit without saving

QUIZ: Take the quiz at TI Internship 2017: Quiz #1

ONLINE TRAINING FOR INTERNS - TEXAS INSTRUMENTS Page 5

You might also like