You are on page 1of 45

Unix, Scripting, Vim

&TCLBasics
Instructions: Please type the answer in the column on the right , next to the question
Afterfinishingthetest,pleasesendyouranswersheetsfromyour
TechMmailID. Name the file name: YourName_BatchCode_UnixTestAnswers.docx
We want base data first. First version should be answered without Web Search.
.
1
a) Explain the terms Kernel Kernel
and Shell in Unix(1)  It basically acts as an interface
between user applications and
hardware.
 The major aim of kernel is to
manage communication between
software i.e. user-level
applications and hardware i.e.,
CPU and disk memory.
Shell
 Shell is a powerful user interface,
so the user interacts with the
shell by typing in the commands.
• The shell interprets the
commands, that may produce
some results,they go back to the
user and the control is given back
to the user when a command
completes (in general).
• In the case of external commands,
shell executes actual programs
that may call functions of the OS
kernel.
• These system commands are
often wrapped around a so-called
system calls, to ask the kernel
b) Explain the term  By using % sign, it means prompt
Prompt(1) here and hereafter.
 It gives useful information to the
user.
 The command to display prompt:
% less –p”Press ‘q’ to quit”
filename
 The sigh –i is to prevent
overwriting existing files and
prompt the user to confirm.

c) What are different types Different types of Unix Shells are


of Unix shells?(1) 1)tcsh (T shell, an expanded version
of csh)
2) bash ( an expansion of sh)
3)ksh (Korn Shell)
4)zhs (Z shell- Zsh is an extended
Bourne shell)
5)C shell
2
a) Describe the login flow in 1. Enter username
unix, with the help of the
flow diagram(1) 2. Enter password
3. OS confirm name and password
4. A shell is created based on your
entry
5. User is placed in home directory

b) Explain the purpose of .<shellrc>:


.<shellrc> and .profile
files in home directory(1)  rc stands for run control.
 The .<shellrc> is used to read
each time we begin a new type
of shell.
.profile files:
 profile file is located in the user-
specific folder
called /home/<username>
 A profile file is a start-up file of
an UNIX user
 It is used for logging into the
bash, ksh or sh shells.
3
a) What are environment Environment Variables are some
variables? How is it set, special variables that are defined in
modified and unset?(1) shell and are needed by programs
while execution.
They can be system defined or user
defined.

To set :
$ <var-name>=string/value
$ echo $<var-name>
To modify:
$ vi .bashrc
export <var-name>=string/value
To unset:
$ unset <var-name>
b) What is the command to printenv
list the environment
variables and their
values?(1)
c) What is the command to printenv <variable-name>
display the value of a
specific environment
variable?(1)
d) What is the special PATH: env variable which is a list
meaning of environment of directories separated by : .
variables PATH, When a command is entered in
DISPLAY and the prompt, the shell searches
LD_LIBRARY_PATH?(1) these paths in sequence to find
the executable file matching the
name with entered command
DISPLAY: environment
variable is used by all clients to
determine what server to display
on.

LD_LIBRARY PATH: Environment


variable which is a list of
directories separated by : .
This variable is used by
commands which uses shared
libraries.
4
a) If you get an error message Check for any spelling mistakes in
“Commandnotfound.”, how do the command.
you debug this error?(2) Make sure that the command or
the file which we are trying to
execute is present in the current
directory.
Else, change the directory and
execute the required command.
5
List 20 Unix commands you know  cd: used to change the
with a short note on them(10) directory
 mkdir: Creates a new
directory
 rmdir: removed the directory
specified
 chsh: used to change the
SHELL
 pwd: shows the path of
currently working directory
 vim filename: creates a new
file named filename
 chmod: used to give or
remove permission for the
file
 clear: clears the screen
 exit: exits the UNIX properly
 man: Gives information
regarding any command
 history: displays the
command used previously
head: displays the top lines of a
document
 tail: displays the bottom lines
of a document
 rm: deletes a file mentioned
 mv: copies data from one file
to another
 date: shows the present
 time: shows current time
 uname: shows the operating
system
 sleep: delay command. It
delays for the number of
seconds specified
 touch : creates an empty file
6
a) Explain the function below b)
combination keys in Unix(5)
i. CTRL+C Kills the operation
ii. CTRL+Z Suspends the
operation
iii. CTRL+A To go to the
beginning of the line
iv. CTRL+E To go to the end of
the line
v. CTRL+S Stops all on screen
vi. CTRL+Q Turns all the
stopped screen back
on
vii. CTRL+L Clear the terminal
screen
viii. CTRL+U Erases the complete
line
ix. ESC+P Displays the
previously used
command
x. ESC+N
UnixShellScripting
7
a) Write a unix shell script to mv "$f" "${fname}.orig"
rename all the files in a
directory to add a
suffix.orig(2)
b) Write a unix shell script to #!/bin/bash
move all the files in a directory cd /home/bssuser/test
which contains pattern for f in Error:
“Error:” into a subdirectory do
fail/(2) mv "$f" fail/
done
Vim
8
a) Write 20 vim edit  i : Inserts text before current
commands with a cursor location.
shortnote(10)  I : Inserts text at beginning of
current line.
 a : Inserts text after current cursor
location.
 A : Inserts text at end of current
line.
 o : Creates a new line for text
entry below cursor location.
 O : Creates a new line for text
entry above cursor location.
 r : Replace single character under
the cursor with the next character
typed.
 R : Replaces text from the cursor
to right.
 s : Replaces single character
under the cursor with any number
of characters.
 S :Replaces entire line.

b) Write 10 vim cursor k : Moves the cursor up one line.


movement commands j : Moves the cursor down one line.
h : Moves the cursor to the left one
with a shortnote(5) character position.
l : Moves the cursor to the right one
character position.
0 or | : Positions cursor at beginning
of line.
$ : Positions cursor at end of line.
W : Positions cursor to the next
word.
B : Positions cursor to previous word.
( : Positions cursor to beginning of
current sentence.
) : Positions cursor to beginning of
next sentence.
c) Write  :set dictionary=dict –Define dict as
10:set<parameters> dictionary.
in vim with a shortnote(5)  :set autoindent –Turn on auto-
indent.
 :set shiftwidth=4 –Define 4 spaces as
indent size.
 :set ignorecase –Ignore case in
searches.
 :set smartcase -Ignore case in
searches except if an uppercase
letter is used.
 :set wrap –Wraps text if exceeds the
display window.
 :set nowrap –No text wraps even if
exceeds the display window.
 :set hls –Highlight search string.
 :set nu –To set line number;Enables
the line number.
 :set nonu –Hides the line
number;Disables the line number.

d) What are the commands The command for forward search is /


for forward and followed by the word or pattern we
backward search?(1) need to search.
The command for backward search is
? followed by the word or pattern we
need to search.
9
a) Write the vim Visual select the ;lines from 10 to
command to change 20 and then press U
case of text between
line number 10 and
line number 20(both 10G+V+20G+U
inclusive) into
UPPERCASE(2)
b) Write the vim Press V+G+x
command to delete Or we can use
from the current line
till the end of the :.,$d
file(2)
c) Write the vim :g/
command to delete all
^Error:.*ModuleX$/d
lines containing a
pattern starting with
“Error:” and ending
with “ModuleX” from a
file (2)
10
a) Explain how to use text
processing Unix
commands like “awk”
or “sort” with in a vim
session
i. On the entire file(1) :%!
ii. On part of the file(2) <range of line number> !
<command>
b) How to execute Unix :!<Unix_command>
commands while being
in a vim session(1)
11
a) Explain the usage of named Using put command
buffers(using“) in vim for
copying and pasting(2)
b) Explain the usage of numbered
buffers(suchas\1,\2,etc.) for
search and substitute
commands(4)
TCL
12
a) Write examples of scalar, list Scalar: set a 22; set b 33
and array variables(3) Lists: set myList [list a b c]
Array:
set color(rose) red
set color(sky) blue
set color(medal) gold
set color(leaves) green
set color(blackboard) black
puts [array exists color]
Here color is a array variable
name.
b) Write 5 list commands with a  lappend: This command
shortnote(2) treats the variable given by
varName as a list and
appends each of the value
arguments to that list as a
separate element, with
spaces between elements.
 lindex: It is used to retrieve
elements from list.
 llength: It treats list as a list
and returns a decimal string
giving the number of
elements in it.
 linsert: Putting some values
into a list, first indexing
from the start and then
indexing from the end and
then chaining them
together.
 lreplace: Replace elements
in a list with new elements.
c) Write 5 array commands with  set color(rose) red
a shortnote(4) set color(sky) blue
set color(medal) gold
set color(leaves) green
set color(blackboard) black
puts [array exists color]
(tests if an array with the
name "color" exists)
 puts [array names color]
(returns a list of the index
strings)
 foreach item [array names
color] {
puts "$item is
$color($item)"}
(iterating through array)
 set lstColor [array get color]
(convert array to list)
 array set color $lstColor
(convert to array)
13
What are the commands for file The commands for file
handling? (2) handling are file, open,
close, gets and read, and
puts, seek, tell, and eof,
fblocked, fconfigure, flush,
fileevent, filename.
a) What is the difference between  foreach:
foreach and The foreach
foreach_in_collection? (2) command implements a
loop where the loop
variable(s) take on
values from one or
more lists.
In the simplest case
there is one loop
variable, varname, and
one list, list, that is a list
of values to assign to
varname.
foreach command
cannot used for
iterating over collection
because it requires a
list.
Using this command on
a collection will cause
the collection to be
deleted.
The body argument is a
Tcl script.
 foreach_in_collection:
Accesses each element
of a collection.
The arguments in
foreach_in_collection
are parallel those of
foreach.
All arguments are
required.
This command does not
allow a list of iterator
variables.
14
a) Write a TCL script to parse
through an input file and extract a
block starting with
“Startpoint:” and ending with
a line containing “VIOLATED” into
an output file (5)

You might also like