Linux and X-Windows Programming - Assignment IV

advertisement
Code – ETCS-307
LINUX AND X WINDOWS PROGRAMMING
Assignment IV
Question1:
a) What happens when you use grep a b c?
Ans: grep searches for a from file b and c
b) Display the files in the current directory that contain the string A HREF
in either uppercase or lowercase.
Ans: grep –li “a href’ *
c) Can you use grep to display lines containing pattern <TABLE> and five
lines below?
Ans: grep –A 5 “<TABLE> filename
d) What does expression gg* specify?
Ans: one or more occurrences of g
e) How do you locate the lines containing harris and Harrison using
grep?
Ans: grep –i “harris*n*”
f) What will the regular expression a. *b match?
Ans: The longest pattern starting with a and ending with b, as close to the
left of the line as possible.
g) How can grep locate lines longer than 15 characters? State two ways.
Ans: grep “…………….” filename
(16 dots)
Or
grep “.\{16\}” filename
h) If you do not have wc command, how would you use grep to count the
number of users currently using your system?
Ans: who | grep –c “”
i) How would you remove blank lines from a file using grep?
Ans: grep –v –e ‘^$’ filename
Arena Nishant
Page 1
j) What does grep “^\.*” do? Is the \ really necessary?
Ans: Words which start from * will be searched. * is a regular expression, to
negate the meaning of * a \ is used.
k) What does this command do? What are the two $s doing here?
grep “$SHELL$” /etc/passwd | cut –d: -f1
Ans: run this command to know the output. Here second $ is used as a
regular expression
l) Print every line of a file twice.
Ans: sed –n ‘$!p’ filename
m) How do you insert a blank line after each line that is read?
Ans: use command sed ‘a\
‘ filename
n) Use a command sequence to locate the line containing the last
occurrence of a pattern.
Ans: $ grep <search_string> filename | tail -1
o) How do you list the ordinary files in your current directory that are not
writable by the owner?
Ans: ls –ltr | grep –v ‘w’
p) Locate the beginning and ending of line with a dot and containing
anything between them.
Ans: grep –e ‘^.’ Filename | grep –e ‘\.$’
q) What does grep “**” foo match?
Ans: Search everything in file
r) What does grep * do? When this command will not work?
Ans: Searches all values when used as regular expression.
Question 2:
a) Difference between wildcard and regular expression?
b) What is a shell script.
Arena Nishant
Page 2
c)
d)
e)
f)
Explain the three modes of vi editor.
Differentiate between various shells.
Differentiate between sed and awk.
What are the various redirection operators available in linux. Explain
their use with example.
g) List the advantages of shell programming.
h) What is the use of filters in Linux OS
Question 3:
a) The x has the value 6, and you reassign it with x=”expr$x + 10”,
what is the new value of x?
Ans: syntax error. Correct syntax is
$ x=6; x=`expr $x + 10`; echo $x
b) If a script uses 12 parameters, how will you access the last one?
Ans: shift 3 and then $9
c) The expression expr -7%2 evaluates to?
Ans: syntax error. Correct syntax is a=`expr -7 % 2`; echo $a
d) What is the internal value associated with the standard error device?
Ans: 2
e) If you define a variable at the command prompt, how can you make its
value available in the shell script?
Ans: export variable_name=value
f) A script containing the statement while [${ count:=1 } –lt
50] is not executing at all. What could be the possible reason?
Ans: Correct syntax is while [${count} –lt 50]
Question 4: Write shell scripts
a)
b)
c)
d)
e)
To sort the numbers
To find the factorial of given numbers
To find whether the given number is prime or not
To find largest number among 5 numbers
To display the context of a folder. Folder name should be given as the
input to the program.
Arena Nishant
Page 3
f) Sum of all digits of a given number.
g) Print fibonacci series
Arena Nishant
Page 4
Download