MCQs 1 mark each (Bash)
168. Which of the following is used in bash for redirection of output from an executable to a file?
A) <
B) >>
C) &
D) >
Answer: D
169. Which of the following will print the string onetwothree ?
A) for n in one two three {echo $n}
B) for one two three do {echo } done
C) for n in one two three; do echo -n $n ;done
D) echo < one two three
Answer: C
170. What does the following command do:
find ~/Downloads -iname \*.pdf -size +8M
a) It finds all pdf upto 8MB and shows on stdout
b) It finds all pdf above 8MB and shows on stdout
c) Both a and b
d) None of these
Answer: B
171. How to append /tmp/bin in PATH?
a) PATH=$PATH:/tmp/bin;export PATH
b) export PATH=$PATH:/tmp/bin
c) both a and b are true
d) None of above
Answer: C
172. From the following loop statements, select the
appropriate one.
i) for each in `ls -1 /tmp`; do echo $each | grep -vi ^H; done
ii) for each in `ls -1 /tmp`; do echo $each | grep -i ^H; done
a) Both i) and ii) will print file names containing 'H'
b) Only ii) will not print file(s) containing 'H'
c) i) will not print file(s) starting with 'h'
d) ii) will not print files starting with '^H'
Answer: C
173. `ls` command lists’ all the files within a directory including hidden files aka (.) dot files
Answer: B
MCC 1 mark each (Bash)
174. Running ls -l on tmp.txt gives the following output:
drwxr--r-- 1 fossee users 0 2011-11-13 15:30 tmp.txt
Which of the following is (are) true?
A) tmp.txt is a directory
B) tmp.txt is a file
C) tmp.txt is an executable
D) tmp.txt can be written only by the owner
Answer: A and D
Upload Script (Bash assignment)2.5 marks each
175. Write a bash script retrieves 10-20 lines from a file, where file is a command line argument.
Solution:
#!/bin/bash
sed -n ‘10,20’p $1
176. Write a bash script retrieves number of files or directory in a directory passed as a command line argument.
Solution:
#!/bin/bash
ls -al $1 | echo `expr $(wc -l) - 1`
177. You have two files of 100 lines each. file1 contains names and file2 contains marks of students. Write a command that will produce a single output file file3 whose alternative lines are from file1 and file2. For example, if file1 contained A, B and C in three lines and file2 containted 10 20 42, the output file file3 should contain
A
10
B
20
C
42
Both the files contain equal number of lines.
Solution: paste -d"\n" file1 file2 > file3
178. Write a bash script which will take a single filename as a command line input and print the file with line numbers as a prefix. For example:
running ./Q6.sh file1
on a file called file1 with contents
This is a line.
This is another.
This is yet another.
should print
1:This is a line.
2:This is another.
3:This is yet another.
Solution:
#!/bin/bash
grep -h -n “.” $1
179. Write a shell script which will take a filename and prints “Too Big” if the size of the file is greater than 1000 bytes.
Solution:
#!/bin/bash
size=$(wc -c < $1)
if [ $size -gt 1000 ]
then
echo "Too Big"
fi
180. Write a shell script that takes three numbers as command line inputs and prints the largest number.
Solution:
#!/bin/bash
if [ $1 -ge $2 -a $1 -ge $3 ]
then
echo $1
fi
if [ $2 -ge $1 -a $2 -ge $3 ]
then
echo $2
fi
if [ $3 -ge $2 -a $3 -ge $1 ]
then
echo $3
fi
182.) What does this command do?
$ git config user.email "your_email@youremail.com"
as against what was shown in class? (1 mark)
Answer> ‘git config user.email’ sets the email address of the user with respect to the repository, in which the command is run, as opposed to running the command with ‘--global’ option, where the email attribute is set system wide for all git repositories.
2. Download the tarball from the link below. This question is linked to the tarball. The directory "repo" has a sample repository.
Explore the repository and draw the "graph" of the commit history showing the various branches and
their commits (with the first 5 chars of the commit ID).
The commits should be numbered in chronological sequence.
The sketch should clearly mark all the branches and all the commits like in example below.
Answer>
-----------X-----------