What output will the following command sequence produce?
echo '1 2 3 4 5 6' | while read a b c; do
echo result: $c $b $a;
done
E
Explanation:
The while loop reads a line from the standard input and splits it into words using the IFS variable,
which by default contains spaces, tabs, and newlines. The read command assigns the first word to
the variable a, the second word to the variable b, and the rest of the line to the variable c. Therefore,
in this case, a=1, b=2, and c=3 4 5 6. The echo command prints the values of c, b, and a in reverse
order, separated by spaces. The output is result: 3 2 1. The loop terminates after reading the first line,
since there is no more input to read. Reference:
Bash while Loop | Linuxize
,
Bash Scripting - While
Loop - GeeksforGeeks
When the command echo $ outputs 1, which of the following statements is true?
C
Explanation:
The $? variable in bash is a special parameter that holds the exit status of the last command
executed in the current shell. The exit status is a numerical value that indicates whether the
command was successful (zero) or failed (non-zero). The echo command simply prints its arguments
to the standard output. Therefore, when the command echo $? outputs 1, it means that the previous
command failed with an exit status of 1. Reference:
[LPI Linux Essentials - Topic 103: Command Line Basics]
[Bash Special Parameters]
[Exit status - Wikipedia]
Which command makes the shell variable named VARIABLE visible to subshells?
B
Explanation:
The export command makes the shell variable named VARIABLE visible to subshells. This means that
any child process that is spawned from the current shell will inherit the value of VARIABLE. The
export command does not need a dollar sign ($) before the variable name, as that would expand the
variable to its value. The set command only affects the current shell and does not export the variable
to subshells. The env command can be used to run a command in a modified environment, but it
does not export the variable to subshells either. Reference:
[LPI Linux Essentials - Topic 105: Shells, Scripting and Data Management]
[LPI Linux Administrator - Exam 102 Objectives - Topic 105: Shells and Shell Scripting]
What output will the command seq 10 produce?
B
Explanation:
The seq command in Linux is used to print a sequence of numbers, which can be piped to other
commands or used in for loops and bash scripts1
. The command can generate a list of integers or
real numbers, with options to control the start, end, and increment of the sequence.
The general
syntax of the command is seq [options] specification1
.
If you launch seq with a single number as a command-line parameter, it counts from one to that
number.
It then prints the numbers in the terminal window, one number per line2
. For example, seq
10 will produce the following output:
Therefore, the correct answer is B. The numbers 1 through 10 with one number per line.
Reference: 1
:
10+ Seq Commands with Examples in Linux – LinuxWizardry 2
:
How to Use the seq
Command on Linux - How-To Geek
By default, the contents of which directory will be copied to a new user's home directory when the
account is created by passing the -m option to the useradd command? (Specify the full path to the
directory.)
/etc/skel
Explanation:
The /etc/skel directory contains files and directories that are used as a template for creating a new
user’s home directory. The useradd command uses the -m (or --create-home) option to create the
user home directory as /home/username and copy the files from /etc/skel to it. The files in /etc/skel
are typically initialization files such as .bashrc, .profile, and .bash_logout that set the user’s
environment variables, aliases, and other preferences.
The system administrator can customize the
/etc/skel directory to provide a consistent and convenient initial setup for new users. Reference:
https://www.howtouselinux.com/post/create-new-user-with-home-directory-in-linux
https://linuxize.com/post/how-to-create-users-in-linux-using-the-useradd-command/
After issuing:
function myfunction { echo $1 $2 ; }
in Bash, which output does:
myfunction A B C
Produce?
A
Explanation:
In Bash, a function is a block of code that can be invoked by its name. A function can take arguments,
which are passed to the function as positional parameters. The $1 variable refers to the first
argument, $2 to the second argument, and so on. The function can access the number of arguments
passed to it by using the $# variable. In this case, the function myfunction simply echoes the first and
second arguments to the standard output. Therefore, when the command myfunction A B C is
executed, the output is A B, since the third argument C is ignored by the function. Reference:
[LPI Linux Essentials - Topic 103: Command Line Basics]
[Bash Functions]
Which of the following commands puts the output of the command date into the shell variable
mydate?
A
Explanation:
∗∗
∗∗
(date)"
Comprehensive
Thecorrectwaytoputtheoutputofthecommanddateintotheshellvariablemy
dateistousecommandsubstitutionwiththesyntax(command). This will execute the command in a
subshell and replace the expression with its standard output. The double quotes around the
expression will prevent word splitting and globbing of the output. The other options are incorrect
because they will either assign a literal string to the variable, use an invalid syntax, or try to execute
the command as an arithmetic expression. Reference:
[LPI Linux Essentials - Topic 105: Shells, Scripting and Data Management]
[LPI Linux Administrator - Exam 102 Objectives - Topic 105: Shells and Shell Scripting]
Which of the following files, when existing, affect the behavior of the Bash shell? (Choose TWO
correct answers.)
B, E
Explanation:
The Bash shell can be configured by various files that affect its behavior, such as setting environment
variables, aliases, functions, options, and prompts. Some of these files are global, meaning they
apply to all users of the system, and some are local, meaning they apply to individual users.
The
global files are usually located in the /etc directory, while the local files are usually located in the
user’s home directory, which is denoted by the tilde (~) symbol1
.
The local files that affect the Bash shell are:
~/.bash_profile: This file is executed when a user logs in to the system. It is used to set up the user’s
environment, such as the PATH, the default editor, the umask, and other variables. It can also run
commands that are needed only once per login session, such as ssh-agent or fortune.
This file can
also source other files, such as ~/.bashrc, to inherit their settings12
.
~/.bashrc: This file is executed when a user starts a new interactive shell, such as opening a terminal
window or running a script with the shebang #!/bin/bash. It is used to set up the user’s shell
preferences, such as aliases, functions, options, and prompts.
It can also source other files, such as
/etc/bashrc, to inherit their settings12
.
~/.bash_logout: This file is executed when a user logs out of the system.
It is used to perform any
cleanup tasks, such as clearing the screen, deleting temporary files, or printing a farewell message1
.
The other files listed in the question are not valid Bash configuration files and do not affect the
behavior of the shell. Therefore, the correct answer is B. ~/.bashrc and E. ~/.bash_profile.
Reference: 1
:
Bash Shell Configuration Files - Land of Linux 2
:
Bash Startup Files - GNU Project
What is the difference between the commands test -e path and test -f path?
C
Explanation:
The test command is used to perform checks and comparisons on files and values. The -e option tests
if a given path exists, regardless of its type (file, directory, link, etc.). The -f option tests if a given path
exists and is a regular file, not a directory or a special file. For example, if we have a directory named
dir and a file named file, we can use the test command as follows:
test -e dir && echo “dir exists” dir exists test -f dir && echo “dir is a regular file” (no output) test -e
file && echo “file exists” file exists test -f file && echo “file is a regular file” file is a regular file
Reference: https://www.howtoforge.com/linux-test-command/
https://www.computerhope.com/unix/bash/test.htm
How can the existing environment variable FOOBAR be suppressed for the execution of the
script./myscript only?
C
Explanation:
The env command can be used to run a utility or command in a custom environment without having
to modify the currently existing environment1
.
The -u or --unset option can be used to remove a
variable from the environment12
. Therefore, the command env -u FOOBAR./myscript will run the
script./myscript in an environment where the variable FOOBAR is suppressed. The other options are
incorrect for the following reasons:
A . unset -v FOOBAR;./myscript: This will unset the variable FOOBAR in the current shell, not just for
the script execution. The semicolon (;) separates two commands, so the script will run in the same
environment as the unset command.
B . set -a FOOBAR=“”;./myscript: This will set the variable FOOBAR to an empty string, not suppress
it. The -a option means that the variable will be exported to the environment of subsequent
commands, so the script will still see the variable FOOBAR, but with no value.
D . env -i FOOBAR./myscript: This will run the script in an empty environment, not just suppress the
variable FOOBAR.
The -i or --ignore-environment option means that no environment variables will be
passed to the command12
. Reference:
env command in Linux with Examples - GeeksforGeeks
,
env -
Wikipedia
.
When the command echo $$ outputs 12942, what is the meaning of 12942?
B
Explanation:
In bash, the PID of a shell script’s subshell process is stored in a special variable called $$.
This
variable is read-only, and you cannot modify it in a shell script1
.
You can use echo $$ to get the PID of
the current bash shell you are using2
. Therefore, when the command echo $$ outputs 12942, it
means that the PID of the current shell is 12942. Reference:
[LPI Linux Essentials - Topic 103: Command Line Basics]
[Bash Special Parameters]
How to get the process ID (PID) of a shell script
How to know the process id of current bash session?
What output will the following command produce?
seq 1 5 20
B
Explanation:
The seq command in Linux is used to generate a sequence of numbers from FIRST to LAST in steps of
INCREMENT1
. The syntax for the seq command is:
seq [OPTION]… LAST or seq [OPTION]… FIRST LAST or seq [OPTION]… FIRST INCREMENT LAST
In this case, the command seq 1 5 20 has three arguments: FIRST = 1, INCREMENT = 5, and LAST = 20.
This means that the command will produce numbers from 1 to 20 in steps of 5. The output will be:
1 5 10 15
The output will not include 20 because it is not a multiple of 5.
The output will be printed on
separate lines by default, unless a different separator is specified with the -s option2
. Reference:
Seq Command in Linux [Explained With Examples]
seq Man Page - Linux - SS64.com - SS64 Command line reference
Which of the following words is used to restrict the records that are returned from a SELECT SQL
query based on a supplied criteria for the values in the records?
C
Explanation:
The SQL WHERE clause is used to restrict the records that are returned from a SELECT SQL query
based on a supplied criteria for the values in the records12
. The WHERE clause follows the SELECT
and FROM clauses and contains one or more conditions that must be true for a record to be included
in the result set. The general syntax of the WHERE clause is:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The condition can be a comparison, a logical operation, a pattern matching, a subquery, or a
combination of these using various operators12
. For example, the following query selects all the
records from the customers table where the country is ‘USA’:
SELECT * FROM customers
WHERE country = 'USA';
The other words listed in the question are not used to filter records based on values. They have
different meanings and purposes in SQL:
CASE: This is a conditional expression that returns a value based on a set of conditions3
. It can be
used in SELECT, UPDATE, DELETE, or WHERE statements. For example, the following query uses a
CASE expression to assign a rating to each customer based on their credit limit:
SELECT customer_name, credit_limit, CASE WHEN credit_limit > 10000 THEN ‘High’ WHEN
credit_limit > 5000 THEN ‘Medium’ ELSE ‘Low’ END AS rating FROM customers;
FROM: This is a clause that specifies the table (s) or view (s) from which the data is retrieved. It
follows the SELECT clause and precedes the WHERE clause. For example, the following query selects
the customer name and order date from the customers and orders tables:
SELECT customer_name, order_date FROM customers JOIN orders ON customers.customer_id =
orders.customer_id;
IF: This is a control flow statement that executes a block of code based on a condition. It can be used
in stored procedures, functions, triggers, or batch files. For example, the following code snippet uses
an IF statement to check if a variable is positive or negative:
DECLARE @num INT; SET @num = -10; IF @num > 0 BEGIN PRINT ‘Positive’; END ELSE BEGIN PRINT
‘Negative’; END
Reference: 1
:
SQL WHERE Clause - W3Schools 2
:
How to Write a WHERE Clause in SQL |
LearnSQL.com 3
: [SQL CASE Statement - W3Schools] : [SQL FROM Clause - W3Schools] : [SQL IF…ELSE
Statement - W3Schools]
Which of the following commands lists all defined variables and functions within Bash?
B
Explanation:
The set command lists all defined variables and functions within Bash, including local, environment,
and shell variables, as well as aliases and functions. The output of set can be very long, so it is often
piped to less, grep, or other commands for filtering or paging. The set command can also be used to
set or unset shell options and positional parameters.
The -o posix option to set limits the output to
only variables, as defined by the POSIX standard123
.
The env command lists only the environment variables, which are a subset of the shell variables that
are passed to child processes. The env command can also be used to run a command in a modified
environment, or to print or set environment variables.
The -a option to env is not valid in most
implementations45
.
The echo command prints a line of text to the standard output. The $ENV variable is not a predefined
variable in Bash, but it can be set by the user or by other programs.
If it is not set, echo $ENV will
print a blank line1
. Reference:
Which of the following SQL queries counts the number of occurrences for each value of the field
order_type in the table orders?
B
Explanation:
The correct SQL query to count the number of occurrences for each value of the field order_type in
the table orders is:
SELECT order_type,COUNT(*) FROM orders GROUP BY order_type;
This query uses the SELECT statement to retrieve the values of the order_type field and
the COUNT(*) function to count the number of rows for each order_type. The GROUP BY clause
groups the rows by the order_type field, so that the count is calculated for each distinct value of
order_type. The result of this query is a table with two columns: order_type and count, where each
row shows the number of orders for a specific order_type.
The other options are incorrect for the following reasons:
A: This query uses a WHERE clause that is always true, since order_type=order_type for every row.
Therefore, this query returns the same result as SELECT order_type,COUNT(*) FROM orders;, which
is a table with one row that shows the total number of orders, regardless of the order_type.
C: This query is syntactically invalid, since the COUNT function cannot take a subquery as an
argument. The correct way to use a subquery with COUNT is COUNT((SELECT order_type FROM
orders));, which returns the total number of orders, regardless of the order_type.
D: This query uses the ORDER BY clause to sort the rows by the order_type field, but it does not
group them by order_type. Therefore, this query returns the same result as SELECT COUNT(*) FROM
orders;, which is a table with one row that shows the total number of orders, regardless of the
order_type.
E: This query is syntactically invalid, since there is no such function as AUTO_COUNT in SQL, and
the COUNT function cannot take a field name as an argument. The correct way to use COUNT with a
field name is COUNT(order_type);, which returns the number of non-null values in the order_type
field.
Reference:
[SQL COUNT Function]
[SQL GROUP BY Statement]
[SQL SELECT Statement]