Resit questions Flashcards

Practicing resit questions

1
Q

BASIC QUESTION 1 (marked correct for all):

Given the following Shell script named echo_func.sh

#!/usr/bin/env bash
echo_input() {
  echo $0
}
echo_input "World"

If we run the script with the following command, what is the expected output?

$ ./echo_func.sh Hello World

Q1 ANSWERS:

  • ./echo_func.sh
  • None of the above since $0 is ambiguous in this context
  • Hello
  • World
A

./echo_func.sh is the answer

  • The script echo_func.sh defines a function echo_input that echoes the value of $0.
  • In a bash script, $0 is the name of the script itself.
  • Thus running command ./echo_func.sh Hello World, output is the name of the script, which is ./echo_func.sh.

Note: the arguments Hello and World passed in the command line are not used in this script. The function echo_input is called with the argument “World”, but within the function, this argument is not used. Instead, the script’s name ($0) is printed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

BASIC QUESTION 2 (marked correct for all):

Suppose that we have a script file named vars.sh and whose contents are shown below.

<vars.sh>
foo="'$foobar'"
foobar='$foo'
echo "$foo"

What will be the output of the following command?

$ bash vars.sh

Q2 ANSWERS:

  • ‘$foo’
  • $foo
  • ‘$foobar’
  • ’ ‘
A

Output is : ‘ ‘

  • In the first line, foo is assigned the value of “$foobar”.
  • At this point, foobar has not been defined, so $foobar is empty.
  • Therefore, foo is assigned the string ‘ ‘.
  • In the second line, foobar is assigned the string ‘$foo’. This is a literal string, and variable substitution does not occur because the string is enclosed in single quotes.
    -Finally, the script echoes the value of foo, which we already established is ‘ ‘.

This is because the single quotes inside double quotes are preserved as part of the string. The double quotes around $foo in the echo statement ensure that the value of foo is printed as is, including the single quotes. So, the output is a pair of single quotes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

BASIC QUESTION 3 (marked correct for all):

Assume the following directory layout. All entries except for those with .txt extensions are directories.

root
├── a
│   ├── bar
│   │   └── file1.txt
│   └── foo
└── b
    ├── a
    ├── bar
    └── baz	

Which of the following command will move directory a into directory b? Assume that your working directory (from where you issue the command) is root.

The following are the explanations for the various options according to the man pages.

cp
  -r: copy directories recursively.

rm
  -r: remove directories and their contents recursively
  -f: ignore nonexistent files and arguments, never prompt

Q3 ANSWERS:
- mv a b
- cp a b; rm a
- We cannot move a into b since b already has a directory named a
- cp -r a/* b; rm -rf a

A

CORRECT ANSWER:
mv a b

Don’t ask me why brother question is tripping me up :(

I would say cp -r a/* b; rm -rf a ??

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

BASIC QUESTION 4 BASIC (marked correct for all):

Which of the following options is the output for the tr command below?

$ echo "AaBbCcDd" | tr -s 'AZ' 'a-z'

Note that the flag -s modifies the command behavior as follows.
-s replace each sequence of a repeated character that is listed in the last
specified SET, with a single occurrence of that character

Q4 ANSWERS:

  • aBbCcDd
  • abcd
  • abbccdd
  • None of the above; command has an error
A

CORRECT ANSWER: aBbCcDd

The tr ‘AZ’ ‘a-z’ part of the command is trying to translate ‘A’ to ‘a’ and ‘Z’ to ‘z’? So, aaBbCcDd

Since -s option “deletes” reoccurrence it becomes: aBbCcDd?

Idk this question is trippy to me :(

I would think the correct answer is None, command has an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

BASIC QUESTION 5:

$ cat test
cry
eve
shy
wry
zoo

$ grep -E '<REGEX>' test

Which of the following regular expressions (when used in place of <REGEX> above) matches words that contain no vowels?</REGEX>

The man page lists the following for the option -E: “Interpret PATTERNS as extended regular expressions”

Q5 ANSWERS:
- [aeiou]{0}
- [^aeiou]+
- [^aeiou]
- [^aeiou]{3}

A

CORRECT ANSWER: [^aeiou]{3}

  • [aeiou]{0}: This matches zero occurrences of any vowel, which means it matches an empty string.
  • [^ aeiou]+: This matches one or more occurrences of any character that is not a vowel. This is the correct answer.
  • This matches exactly one occurrence of any character that is not a vowel. It will not match words with more than one character.
  • [^ aeiou]{3}: This matches exactly three occurrences of any character that is not a vowel. It will not match words with less or more than three characters.

In the context of the specific words provided in the question, which are all three letters long, the regular expression [^aeiou]{3} would be the most precise answer. This expression will match exactly three characters that are not vowels, which fits the criteria for the words given.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

BASIC QUESTION 6:

Suppose the file structure on a system is the following.

root
├── a
│   ├── bar
│   └── foo
├── b
│   ├── bar
│   └── baz
└── c
    ├── car
    └── far

Now, if you issue the following command from within root/c/far, which directory will you end up in?

$ cd .././car/.././../b/../../.././../

Q6 ANSWERS:
- b
- root
- Root/c/far
- None of the above; the invocation has an error

A

Root is the correct answer.

Here’s how the provided command works giving you are located at root/c/far at the start:

  • ../ moves you up one directory, so you go from root/c/far to root/c.
  • ./ keeps you in the same directory, so you stay in root/c.
  • car/ moves you into the car directory, so you go to root/c/car.
  • ../ moves you up one directory, so you go from root/c/car to root/c.
  • ./ keeps you in the same directory, so you stay in root/c.
  • ../ moves you up one directory, so you go from root/c to root.
  • b/ moves you into the b directory, so you go to root/b.
  • ../../../ moves you up three directories. However, since you’re already at the root directory, you stay in root.
  • ./ keeps you in the same directory, so you stay in root.
  • ../ moves you up one directory. However, since you’re already at the root directory, you stay in root.

So, after executing the command cd .././car/.././../b/../../.././../, you will end up in the root directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

BASIC QUESTION 7:

The following is a simple shell script.

<args.sh>
#!/usr/bin/env bash
echo $#

Guess the output of the above script based on the example invocation below.

$ bash ./args.sh This,is "for testing, 'a simple input'"

Q7 ANSWERS:
- 4
- 3
- 5
- 2

A

Answer is 2!

When you run the command bash ./args.sh This,is “for testing, ‘a simple input’”, the arguments are:

  1. This,is
  2. “for testing, ‘a simple input’”

Even though the second argument contains spaces, it is considered as one argument because it is enclosed in double quotes. Therefore, the total number of arguments is 2, and the output of the script will be 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

BASIC QUESTION 8:

Assume you are in the test branch, and you want to merge the commits on the test branch to the master branch. What should you do?

Q8 ANSWERS:
-Checkout the master branch and run git merge test
-Run git merge master directly
-Checkout the master branch and run git merge master
-Run git merge test directly

A

Correct answer: Checkout the master branch and run git merge test

When you want to merge changes from one branch (in this case, test) into another branch (in this case, master), you need to be on the branch that you want to merge into. So, you first checkout the master branch with git checkout master. Then, you merge the test branch into the master branch with git merge test. This will bring all the commits from the test branch into the master branch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

BASIC QUESTION 9 (marked correct for all):

Suppose that you have three text files in a directory: first, second, and third. Assume that you want to create a fourth file (line-count.txt) containing the counts of lines in some subset of these files. Specifically, let us suppose that you want to store the line count of the file named “second” in the first line and that of the file named “first” in the second line, and you have the following partial Makefile recipe.

<Makefile>
line-count.txt: first second third

What option below represents the correct command for the above Makefile recipe to compute the line counts as described above?

The man page describes option -l of the command wc as follows: “print the newline counts”

Q9 ANSWERS:
- @wc -l $(word 2, $^) > $@
@wc -l $(word 1, $^) > $@

  • @wc -l $(word 1, $^) > $@
    @wc -l $(word 0, $^)&raquo_space; $@
  • @wc -l $(word 2, $^) > $@
    @wc -l $<&raquo_space; $@
  • @wc -l $(word 2, $^) > $@
A

3rd answer is correct:
@wc -l $(word 2, $^) > $@
@wc -l $<&raquo_space; $@

  • $(word 2, $^) gets the second prerequisite of the target, which is second.
  • $< gets the first prerequisite of the target, which is first.
  • wc -l counts the number of lines in a file.
  • > redirects the output to a file, overwriting the file if it exists.
  • > > appends the output to a file, creating the file if it doesn’t exist.

So, the first command counts the lines in second and writes that count to line-count.txt. The second command counts the lines in first and appends that count to line-count.txt. This matches the desired behavior of storing the line count of second in the first line of line-count.txt and the line count of first in the second line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

BASIC QUESTION 10:

Suppose a Makefile contains the following lines.

P = Value for P is $(Y)
Y := $(X)
X := 1
Z := Value for Z is $(X)

show:
        @echo $(P)
        @echo $(Z)

Which one of the options below would be the output for the following command?

$ make

Q10 ANSWERS:
- Value for P is
Value for Z is 1

  • Value for P is 1
    Value for Z is
  • Value for P is
    Value for Z is
  • Value for P is 1
    Value for Z is 1
A

1st answer is correct:
Value for P is
Value for Z is 1

In Makefiles, there are two types of variable assignments which are, = and :=.

  • = is a recursive assignment. The right-hand side is expanded when the variable is used, not when it’s declared.
  • := is a simple assignment. The right-hand side is expanded at the time of declaration.

In our Makefile, P is assigned the value Value for P is $(Y) using =. So $(Y) will be expanded when P is used, not when it’s declared. At the time of declaration, Y has no value, so $(Y) is empty.

Y is then assigned the value $(X) using :=. At this point, X has no value, so Y is assigned an empty value.

X is then assigned the value 1 using :=.

Z is assigned the value Value for Z is $(X) using :=. So $(X) is expanded at the time of declaration, and Z is assigned the value Value for Z is 1.

When you run make, it executes the show target, which echoes $(P) and $(Z). $(P) is Value for P is $(Y), and since Y is empty, it echoes Value for P is . $(Z) is Value for Z is 1, so it echoes Value for Z is 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

BASIC QUESTION 11:

What will be the output of the following command?

$ echo 'Hello' && echos 'World' 2> /dev/null

Q11 ANSWERS:
- World
- HelloWorld
- No output, because of the typo and use of &&
- Hello

A

The correct answer is Hello.

The && operator in a shell command runs the second command only if the first command succeeds.

In this case, echo ‘Hello’ will succeed and print Hello.

The second command echos ‘World’ is likely a typo and will fail because echos is not a valid command. However, 2> /dev/null redirects the error message to /dev/null, effectively discarding it. So, no error message will be printed.

Therefore, the only output from this command will be Hello. The other options are not correct because World will not be printed due to the typo in echos, and there will be output (Hello) despite the typo and use of &&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

BASIC QUESTION 12 BASIC:

The following are the contents of a simple shell script hello.sh.

#!/a/location/that/does/not/exist bash
echo "Hello '$USER!'"

Guess the output of the above script when run by a user ‘neo’ based on the example invocation below.

$ bash ./hello.sh

Q12 ANSWERS:
- Hello’${USER!}’
- Hello ‘neo!’
- An error message as follows: “The file ‘./hello.sh’ specified the interpreter ‘/a/location/that/does/not/exit bash’ which is not an executable command.”
- Hello “

A

Answer is Hello ‘neo!’

When you run a shell script with bash ./script.sh, bash ignores the shebang line (#!/a/location/that/does/not/exist bash) and runs the script in a new bash shell. So, even though the path in the shebang line does not exist, it doesn’t cause an error.

The echo command prints the string Hello ‘$USER!’. In a double-quoted string in bash, variable references ($USER) and command substitutions are replaced with their values.

So $USER is replaced with the username of the current user, which is neo in this case.
Therefore, the output of the script when run by a user neo is Hello ‘neo!’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

BASIC QUESTION 13:

Assuming that the following command is issued from the location /tmp, which is currently empty, how many directories does it create?

mkdir -p foo/a,{b,c}

The man page provides the following explanation for option -p: “no error if existing, make parent directories as needed”

Q13 ANSWERS:
* 4
* 3
* None; command is invalid
* 2

A

Answer is 3. Why though?

The command mkdir -p foo/a,{b,c} creates the following directories:
3. foo
4. foo/a
5. b
So, it creates 3 directories in total. The {b,c} part is brace expansion in bash, which will generate b. The c is not created because it’s not included in the path??

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

BASIC QUESTION 14 BASIC:

Suppose the current directory contains the following. Please pay attention to the file names and the last-modification timestamps.

$ ls -l
.rw-r--r--@ 15 balac  5 Dec 20:07 first
.rw-r--r--@ 90 balac  5 Dec 20:06 Makefile

The Makefile’s contents are shown below.

first:
    @echo "» First: done" &> /dev/null > second

second: first
    @echo "» Second: done"

Now, what will be the output for the following commands in the current directory?

$ make second && make second

Q14 ANSWERS:
» First: done
make: ‘second’ is up to date.
make: ‘second’ is up to date.

> > First: done
Second: done
Second: done

> > Second: done
Second: done

> > Second: done
make: ‘second’ is up to date.

A

CORRECT ANSWER:
Making third.txt
Making third.txt

Here’s what happens when you run make second:
1. make looks at the second target and sees that it depends on first. So, it first executes the first target.
2. The first target has this command: @echo “» First: done” &> /dev/null > second. This command does two things:
* It prints “» First: done” to the standard output, but immediately redirects this output to /dev/null, effectively discarding it.
* It creates a new file named second. The content of this file is not specified, so it’s an empty file.
3. After executing the first target, make moves on to the second target. The second target has this command: @echo “» Second: done”. This command prints “» Second: done” to the standard output.

The make command will execute the commands associated with the second target each time it’s called, regardless of the timestamps of the files. This is because the second target does not create or modify a file named second ( it simply executes the command @echo “» Second: done”). Because this command does not change the filesystem, so make has no way of knowing whether the target is up to date and needs to be run again.
(Lowkey IDK i hate make files)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

BASIC QUESTION 13:

Suppose that we use the read command to accept input from a user. The part in bold is the user input.

$ read x y
“Introduction to Computer Science”

What will be the output of the following commands?

$ echo "---"; echo $x; echo $y; echo "---"

BASIC Q14 ANSWERS:
~~~

Introduction to Computer
Science

“Introduction
to Computer Science”

Introduction
To Computer Science

“Introduction to Computer Science”

~~~

A

2nd answer is correct:
- - -
“Introduction
to Computer Science”
- - -

  • “The read command in bash reads a line from the standard input and splits it into words. These words are assigned to sequential variables named as arguments”
  • If there are more words than variables, read assigns the leftover words (along with the intervening whitespace) to the last variable.
  • In your case, read x y assigns the first word to x and all the remaining words to y.
  • Hence x=”Introduction and y = to Computer Science”

The commands do the following:
1. echo “—”; prints “—” on one line
2. echo $x; prints “Introductio on another new line
3. echo $y; prints Computer Science” on another new line
4. echo “—”; prints “—” on another new line

Note: ; is like new line /n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

QUESTION 1 INTERMEDIATE:

Observe the following sequence of commands to predict the output.

$ touch -t 202001201000 a.txt

$ ls -l
total 0
-rw-rw-r-- 1 balac balac 0 Jan 20  2020 a.txt

$ wc a.txt b.txt 1>&2 2> /dev/null

The man page lists the following for the option -t of the touch command.

-t STAMP : use [[CC]YY]MMDDhhmm[.ss] instead of current time

INTERMEDIATE Q1 ANSWERS:

  • 0000 a.txt
    000 total
  • 000 a.txt
    wc: b.txt: No such file or directory
  • wc: b.txt: No such file or directory
  • 000 a.txt
A

CORRECT ANSWER:
0000 a.txt
000 total

This is because the wc command is used to count the number of lines, words, and characters in files.

In this case, a.txt is an empty file (as shown by the ls -l command), so the counts for lines, words, and characters are all 0. The b.txt file does not exist, but the 2> /dev/null part of the command redirects standard error to /dev/null, effectively discarding the error message that would normally be printed when wc tries to read a non-existent file.

Therefore, the output of the command is the counts for a.txt followed by the total counts, which are all 0

17
Q

QUESTION 2 INTERMEDIATE:

Suppose that have an array of numbers in a variable.

$ num_array=(10 20 30 40)
$ echo ${num_array[@]}
10 20 30 40

We can retrieve the number of elements in the array as follows.

$ echo ${#num_array[@]}
4

Now, what will be the output of the following commands?

$ unset num_array[$((${#num_array[@]}-4))]
$ echo ${num_array[@]}

INTERMEDIATE Q2 ANSWERS:
- 20 30 40
- 10 20 30
- 10 20 30 40
- No output, since array has no elements

A

1st answer is correct:
20 30 40

The command unset num_array[$((${#num_array[@]}-4))] removes the element at the index ${#num_array[@]}-4 from the array num_array.

In this case, ${#num_array[@]} is the number of elements in the array, which is 4.

So, 4-4 equals 0. Therefore, the command removes the element at index 0, which is the first element of the array (10).

After this operation, the array num_array contains the elements 20, 30, and 40.

So, the command echo ${num_array[@]} prints 20 30 40.

18
Q

QUESTION 3 INTERMEDIATE:

Suppose the file words.txt contains the following.

<words.txt>
madam
noon
..w..
tenet
.*.
.*^..
(.)(*)_(*)(.)
mom
peep
rotor

What is the output of the following command?

$ sed -n '/^\(.\)\(.\).\2\1$/p' < words.txt | awk 'BEGIN{OFS=","} {print $0}'

The man page lists the following for the option -n and flag p (in the pattern) in the sed command.

-n, –quiet, –silent : suppress automatic printing of pattern space
p : Print the current pattern space

The built-in variable OFS in awk means the following (according to the man page).

OFS : inserted between fields on output, initially = “ “.

A

CORRECT ANSWER:
madam
..w..
tenet
rotor

  • sed -n ‘/^(.)(.).\2\1$/p’ < words.txt: This command uses sed to filter lines in the file words.txt that match a specific pattern.
  • The -n option suppresses automatic printing
  • The p flag prints the current pattern space. The pattern ^(.)(.).\2\1$ matches lines that are 5 characters long and have the first character equal to the fifth and the second character equal to the fourth. (PALINDROME)

!!!NOTE: Each line is separated by a newline character, not a comma, because the awk command prints each matching line separately. The OFS variable in awk only affects the separator between fields within a line, not between lines.

MORE ABOUT THE PALINDROME PATTERN:
* ^ and $ are start and end of line anchors respectively. They ensure that the pattern matches the entire line, not just a part of it.
* . matches any single character.
* (.) and (.) are two groups of any single character. The parentheses () are used to create groups in regular expressions. The first (.) matches the first character on the line and the second (.) matches the second character on the line.
* \2 and \1 are backreferences to the second and first groups respectively. \2 matches the same character as the second group (the second character on the line), and \1 matches the same character as the first group (the first character on the line).
The . in between the backreferences matches any single character.

19
Q

QUESTION 4 INTERMEDIATE:

Given the following Shell script.

<inp-args.sh>
#!/usr/bin/env bash

foo() {
    input=$2
}

input=$1
foo "$0 $1"

echo "-$input-"

If we run the script with the following command, what will be the expected output?

$ bash ./inp-args.sh hello world

INTERMEDIATE Q4 ANSWERS:
- -hello-
- -world-
- –
- -./inp-args.sh-

A

CORRECT ANSWER: -hello-

In the script, input=$1 sets the variable input to the first argument passed to the script, which is hello.

Then, the function foo is called with the argument “$0 $1”, but within the function, input is set to $2, which is the second word of the argument passed to foo.

However, foo is called with only one argument (“$0 $1” is a single argument because it’s enclosed in quotes), so $2 within foo is empty and input remains hello.

Finally, echo “-$input-“ prints
-hello-.

20
Q

QUESTION 5 INTERMEDIATE:

Given the following Bash script.

<fun.sh>
#!/usr/bin/env bash

n=$1

x=0
while [ $n -ne $x ]; do
    x=`expr $x \* 10 + $n % 10`
    n=`expr $n / 10`
done
echo $x

What will be the expected output of the following command?

$ ./fun.sh 20877802

INTERMEDIATE Q5 ANSWERS:
- 20877802
- 20877802
- 7802
- 2087

A

CORRECT ANSWER:
2087

This script reverses the digits of the input number.

It does this by repeatedly taking the last digit of the number ($n % 10), appending it to x ($x * 10 + $n % 10), and then removing the last digit from n ($n / 10).

This continues until n is equal to x.

However, the condition in the while loop is $n -ne $x, which means the loop continues until n is not equal to x.

When you run the script with the command ./fun.sh 20877802, the loop will stop prematurely when n and x are both 2087, because at this point, n is equal to x.

So, the output of the script will be 2087, which is the value of x (and n) at the point when the loop stops. The remaining digits of the original number are not processed because the loop has already stopped :)

21
Q

QUESTION 6 INTERMEDIATE:

Assume the following directory layout.

root
├── bar
│   └── x
│       └── y
└── foo
    └── x

Which of the following command-line invocations will delete all of these directories? Note that option -p to rmdir will “remove DIRECTORY and its ancestors” according to the manpage.

Note that option -p to rmdir will “remove DIRECTORY and its ancestors” according to the manpage.

INTERMEDIATE Q6 ANSWERS:

  • cd ./root; rmdir -p ../root/foo/x 2> /dev/null || rmdir -p ../root/bar/x/y 2> /dev/null
  • cd ./root; rmdir ../root/bar/x/y 2> /dev/null
  • cd ./root; rmdir -p ../root/foo/x 2> /dev/null && rmdir -p ../root/bar/x/y
  • cd ./root; rmdir -p ../root/bar/x 2> /dev/null
A

CORRECT ANSWER:
cd ./root; rmdir -p ../root/foo/x 2> /dev/null || rmdir -p ../root/bar/x/y 2> /dev/null

  • This command first changes the current directory to root.
  • Then it tries to remove the foo/x directory and its ancestors. If this fails (for example, if foo/x doesn’t exist or isn’t empty), it will then try to remove the bar/x/y directory and its ancestors.
  • The 2> /dev/null part discards any error messages by redirecting them to /dev/null.
    The || operator in this command ensures that if the removal of foo/x fails, it will still attempt to remove bar/x/y
22
Q

QUECTION 1 ADVANCED:

Which of the following options is the output for the awk command below?

$ cat lines.txt
a
a
b
a
c
b
b
d
$ awk '!line[$0]++' lines.txt

ADVANCED Q1 ANSWERS:

  • c
    d
  • a
    b
    a
    c
    b
    d
  • a
    c
    b
    d
  • a
    b
    c
    d
A

4th (last) option is the correct answer:
a
b
c
d

The awk ‘!line[$0]++’ lines.txt command is a common idiom used in awk to remove duplicate lines from a file:
* $0 is the current line.
* line[$0]++ is an associative array that keeps track of the number of times each line has been seen.
* !line[$0]++ is true only the first time a line is seen (because the increment happens after the check due to the postfix ++).

So, this awk command prints each line the first time it’s seen and skips any duplicates.

Therefore, the output will be the first occurrence of each unique line in the order they appear in the file, which is a, b, c, d.

23
Q

QUECTION 2 ADVANCED:

Assume we have the following Bash script.

<loop.sh>
#!/usr/bin/env bash
if [[ x -ne 0 ]]; then
    echo $x
    x=$((x-2))
    if [[ x -eq 1 ]]; then
        x=$y
    fi
    ./loop.sh
fi

What is the expected output if we run the following commands?

$ x=5; y=$((x-1))
$ export x
$ export y
$ bash ./loop.sh

ADVANCED Q2 ANSWER:

  • 5
    3
    1
    4
    2
  • 5
    3
  • 5
    3
    4
    2
  • 5
    3
    1
    4
    2
    0
A

3rd answer is the correct one:
5
3
4
2

  1. You start with x=5 and y=4.
  2. You run the script with bash ./loop.sh.
  3. The script checks if x is not equal to 0. Since x is 5, it enters the if statement.
  4. It echoes the value of x (which is 5) and then decrements x by 2, so x becomes 3.
  5. It checks if x is equal to 1. Since x is 3, it skips this if statement.
  6. It calls itself recursively with ./loop.sh.
  7. Now x is 3, so it echoes 3 and decrements x by 2, so x becomes 1.
  8. It checks if x is equal to 1. Since x is 1, it sets x to the value of y, which is 4.
  9. It calls itself recursively with ./loop.sh.
  10. Now x is 4, so it echoes 4 and decrements x by 2, so x becomes 2.
  11. It checks if x is equal to 1. Since x is 2, it skips this if statement.
  12. It calls itself recursively with ./loop.sh.
  13. Now x is 2, so it echoes 2 and decrements x by 2, so x becomes 0.
  14. It checks if x is equal to 1. Since x is 0, it skips this if statement.
  15. It calls itself recursively with ./loop.sh.
  16. Now x is 0, so it doesn’t enter the if statement and the script ends.
    So, the output of the script is 5, 3, 4, 2
24
Q

QUECTION 3 ADVANCED:

Assume that the current directory contains the following files.

$ ls -l
.rw-r--r--  14 neo  5 Oct 17:01 first.txt
.rw-r--r--@ 80 neo  5 Oct 17:05 Makefile
.rw-r--r--  15 neo  5 Oct 17:02 second.txt
.rw-r--r--  54 neo  5 Oct 17:03 third.txt

The contents of the Makefile are as follows.

WC := wc -l

third.txt: first.txt second.txt
    @echo "Making $@"
    @$(WC) $^ > $@

Suppose we issue the following commands in the current directory.

$ touch -m -t 202412310000 first.txt
$ make && make

The following are the explanations of the options passed to touch.

-m Change the modification time of the file. The access time of the file is not changed unless the -a flag is also specified.
-t Change the access and modification times to the specified time instead of the current time of day. The argument is of the form “[[CC]YY]MMDDhhmm[.SS]”

(Hint: Pay attention to the modification time passed to touch.)

What will be the output of the make && make command?

ADVANCED Q3 ANSWER:

  • Making third.txt
    make: ‘third.txt’ is up to date.
  • Making third.txt
  • make: ‘third.txt’ is up to date.
  • Making third.txt
    Making third.txt
A

CORRECT ANSWER:
Making third.txt
Making third.txt

The make command reads the Makefile and executes the commands associated with the target you specify. In this case, the target is third.txt, which depends on first.txt and second.txt.
The touch -m -t 202412310000 first.txt command changes the modification time of first.txt to 2024-12-31 00:00. This makes first.txt newer than third.txt.
When you run make, it checks the timestamps of third.txt and its dependencies (first.txt and second.txt). If any of the dependencies are newer than third.txt, it executes the commands associated with third.txt.
In this case, because first.txt is newer than third.txt, make executes the commands for third.txt:

The $@ variable represents the target (third.txt), and the $^ variable represents all the dependencies (first.txt and second.txt). So, the commands print “Making third.txt” and writes the line counts of first.txt and second.txt to third.txt.

When you run make again, make checks the timestamps again. The modified time of first.txt is 2024-12-31 00:00 is still a year newer then the time at which we make third.txt during the exam => HENCE meaning third file is STILL NOT UP TO DATE and the output will once again print third.txt

25
Q

QUECTION 4 ADVANCED:
Suppose you are given the following Bash script fun-num.sh

#!/usr/bin/env bash
let n=$1
let a=0
let b=0
let i=0
while [ $n -gt 0 ]; do
  c=`expr $n % 10`
  if [ `expr $i % 2` -eq 1 ]; then
    b=`expr $b \* 10 + $c`
  else
    a=`expr $a \* 10 + $c`
  fi
  n=`expr $n / 10`
  i=`expr $i + 1`
done
echo $a$b

What is the expected output if we run the following command?

$ bash ./fun-num.sh 123456

ADVANCED Q3 ANSWERS:

  • 123456
  • 456123
  • 654321
  • 642531
A

4th (last) answer is correct:
642531

The script fun-num.sh takes a number as input and processes it digit by digit from right to left. It separates the digits at odd and even positions, then joins them together. First he digits at even positions followed by the digits at odd positions.

So, for n=123456, the digits at even positions are 6, 4, and 2, and the digits at odd positions are 5, 3, and 1. Therefore, a becomes 642 and b becomes 531, and the output of the script is a followed by b, which is 642531.

  1. The script starts with n=123456, a=0, b=0, and i=0.
  2. It enters a while loop that continues as long as n is greater than 0.
  3. In each iteration of the loop, it calculates c, the remainder of n divided by 10. This gives the last digit of n.
  4. It checks if i is odd (i % 2 equals 1). If i is odd, it adds c to the end of b (b multiplied by 10 plus c). If i is even, it adds c to the end of a.
  5. It divides n by 10 to remove the last digit.
  6. It increments i by 1.
  7. It repeats steps 3-6 until n becomes 0.
    Finally, it echoes a followed by b.
26
Q

QUECTION 5 ADVANCED:
Assume we have the following Bash script.

<check_file.sh>
#!/usr/bin/env bash
cat $1 &> /dev/null
if [[ $? -gt 0 ]]; then
    exit 0
else
    exit 1
fi

If we run the script with the following command, which of the following statements is true? (Tip: $? provides the return code of the previous command)

$ ./check_file.sh sys_log.txt && echo "A" || echo "B"

ADVANCED Q5 ANSWERS:

  • If file sys_log.txt does not exist, output A and B
  • If file sys_log.txt does not exist, there will be no output
  • If file sys_log.txt does not exist, output A
  • If file sys_log.txt does not exist, output B
A

Correct answer is:
If file sys_log.txt does not exist, output A

The cat $1 &> /dev/null command tries to read the file sys_log.txt.

The question wants us to look at scenraios where the DOES NOT EXIST:

If the file does not exist, this command will fail, and the return status ($?) will be non-zero.

In the script, if $? is greater than 0 (which means the previous command failed), the script exits with a status of 0 beacuse of “exit 0”(indicating success in the context of the script because it successfully determined that the file does not exist).

Since the script exits with 0 (indicating success), the command after && will execute. So, it will output “A”. The command after || is only executed if the previous command fails, which it doesn’t in this case.

27
Q

QUECTION 6 ADVANCED (CORRECT):

An Internet Protocol version 4 (IPv4) address is a 32-bit number that uniquely identifies a machine on a network or the Internet. An IPv4 address is typically written in decimal digits, formatted as four 8-bit fields that are separated by periods, e.g., 10.20.30.40. All four fields are mandatory, and each one must have a value between 0 and 255, including the end points.

Suppose we use the following command to test the format of a given IPv4 address (mentioned in <IPV4>).</IPV4>

echo <IPv4 address> | sed -nE '/<REGEX>/p'

Which of the following regular expressions when used in place of <REGEX> in the above invocation most accurately checks whether a given IPv4 address adheres to this aforementioned format?</REGEX>

A

Correct answer:

^( (25[0-5] | 2[0-4][0-9] | [01]? [0-9][0-9]?) .){3}

(25[0-5] | 2[0-4][0-9] | [01]? [0-9][0-9]?) $

Note: not so many spaces or any new lines just easier for me to read n remenber