Docsity

Shell Scripting using Bash (SC103) Exam, Exams of Technology

Toggle sidebar
bg1
Shell Scripting using Bash (SC103) Exam
1
1. What is the purpose of the read command in Bash?
a) To write data to a file
b) To read input from the user
c) To execute a script
d) To define a variable
Answer: b) To read input from the user
Explanation: The read command is used to accept user input from the standard input
(keyboard) and assign it to a variable.
2. How can you prompt the user for input in a Bash script?
a) echo "Enter your name:"
b) read -p "Enter your name: " name
c) prompt "Enter your name:"
d) input "Enter your name:"
Answer: b) read -p "Enter your name: " name
Explanation: The -p option allows you to provide a prompt directly with the read
command.
3. Which of the following commands will store the current directory in a
variable named current_dir?
a) current_dir=$(pwd)
b) current_dir=pwd
c) current_dir=cd
d) current_dir="pwd"
Answer: a) current_dir=$(pwd)
Explanation: The $(pwd) command substitution gets the current working directory and
assigns it to the current_dir variable.
4. How do you access the first argument passed to a Bash script?
a) $1
b) $2
c) $0
d) $*
Answer: a) $1
Explanation: In Bash scripts, $1 refers to the first argument passed to the script.
5. Which command will exit a Bash script with a status of 1?
a) exit 0
b) return 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Shell Scripting using Bash (SC103) Exam and more Exams Technology in PDF only on Docsity!

1. What is the purpose of the read command in Bash?

a) To write data to a file

b) To read input from the user

c) To execute a script

d) To define a variable

Answer: b) To read input from the user

Explanation: The read command is used to accept user input from the standard input

(keyboard) and assign it to a variable.

2. How can you prompt the user for input in a Bash script?

a) echo "Enter your name:"

b) read -p "Enter your name: " name

c) prompt "Enter your name:"

d) input "Enter your name:"

Answer: b) read -p "Enter your name: " name

Explanation: The -p option allows you to provide a prompt directly with the read

command.

3. Which of the following commands will store the current directory in a

variable named current_dir?

a) current_dir=$(pwd)

b) current_dir=pwd

c) current_dir=cd

d) current_dir="pwd"

Answer: a) current_dir=$(pwd)

Explanation: The $(pwd) command substitution gets the current working directory and

assigns it to the current_dir variable.

4. How do you access the first argument passed to a Bash script?

a) $

b) $

c) $

d) $*

Answer: a) $

Explanation: In Bash scripts, $1 refers to the first argument passed to the script.

5. Which command will exit a Bash script with a status of 1?

a) exit 0

b) return 1

c) exit 1

d) quit 1

Answer: c) exit 1

Explanation: The exit command is used to terminate a script and return an exit status. exit

1 indicates an error or abnormal termination.

6. How can you use a variable inside a double-quoted string in Bash?

a) ${variable}

b) $(variable)

c) ^variable

d) variable[]

Answer: a) ${variable}

Explanation: Inside double quotes, variables can be used with ${variable} to ensure

correct interpretation and expansion.

7. Which of the following will list files in the current directory?

a) ls

b) dir

c) list

d) files

Answer: a) ls

Explanation: The ls command lists the files and directories in the current directory.

8. How do you make a Bash script executable?

a) chmod 755 script.sh

b) chmod +x script.sh

c) chmod 777 script.sh

d) chmod 644 script.sh

Answer: b) chmod +x script.sh

Explanation: The chmod +x command adds execute permissions to the script file.

9. What does the #!/bin/bash line at the beginning of a script do?

a) Specifies the interpreter for the script

b) Comments out the line

c) Defines a variable

d) Sets the script's execution time

Answer: a) Specifies the interpreter for the script

Explanation: The #!/bin/bash line, called a shebang, indicates that the script should be run

using the Bash interpreter.

c) command << file

d) command 2> file

Answer: a) command >> file

Explanation: The >> operator appends the output of a command to a file without overwriting

its existing contents.

15. What is the purpose of the echo command in Bash?

a) To execute a command

b) To display a line of text

c) To create a file

d) To delete a file

Answer: b) To display a line of text

Explanation: The echo command outputs text to the terminal or to a file.

16. How do you define a variable in Bash?

a) var=value

b) var: value

c) var = value

d) var-value

Answer: a) var=value

Explanation: Variables in Bash are defined by using the syntax var=value without spaces

around the = sign.

17. How can you list all environment variables in Bash?

a) listenv

b) env

c) showenv

d) printenv

Answer: d) printenv

Explanation: The printenv command lists all environment variables and their values.

18. What does the $(command) syntax do?

a) Runs the command in the background

b) Substitutes the output of the command

c) Creates a new command

d) Executes the command on a remote server

Answer: b) Substitutes the output of the command

Explanation: The $(command) syntax is used for command substitution, where the output of

the command replaces the $(command) expression.

19. How can you access all command-line arguments passed to a script?

a) $@

b) $*

c) $#

d) $

Answer: a) $@

Explanation: $@ represents all the arguments passed to the script as separate words,

preserving whitespace within arguments.

20. What is the purpose of the case statement in Bash?

a) To execute commands based on the value of a variable

b) To define a loop

c) To handle file permissions

d) To create a new variable

Answer: a) To execute commands based on the value of a variable

Explanation: The case statement allows you to execute different commands based on the

value of a variable, similar to a switch statement in other languages.

21. Which command will list all files and directories, including hidden ones?

a) ls -a

b) ls -l

c) ls -h

d) ls -r

Answer: a) ls -a

Explanation: The -a option with ls lists all files and directories, including those that are

hidden.

22. How can you create a new directory in Bash?

a) mkdir directory_name

b) newdir directory_name

c) create directory_name

d) make directory_name

Answer: a) mkdir directory_name

Explanation: The mkdir command is used to create a new directory with the specified name.

23. What does the -f option do in the rm command?

a) Forcefully deletes files without prompting

b) Lists files in a directory

c) Finds files by name

d) Renames files

c) arrange file

d) list file

Answer: a) sort file

Explanation: The sort command sorts the lines of a file in alphabetical order.

29. How can you redirect standard error to a file?

a) command 2> error_file

b) command 2>> error_file

c) command > error_file

d) command >> error_file

Answer: a) command 2> error_file

Explanation: The 2> operator redirects standard error (file descriptor 2) to a file.

30. What does the chmod command do?

a) Changes file permissions

b) Moves files to a different directory

c) Deletes files

d) Copies files

Answer: a) Changes file permissions

Explanation: The chmod command is used to change the file permissions of files and

directories.

31. Which of the following is a valid way to include a comment in a Bash

script?

a) # This is a comment

b) /* This is a comment */

c) // This is a comment

d)

Answer: a) # This is a comment

Explanation: In Bash, comments are added using the # symbol, and everything following

on that line is ignored by the interpreter.

32. How do you check if a string variable is empty in Bash?

a) if [ -z "$var" ]

b) if [ "$var" == "" ]

c) if [ -n "$var" ]

d) if [ "$var" = "" ]

Answer: a) if [ -z "$var" ]

Explanation: The -z test operator checks if the length of the string is zero, indicating that it

is empty.

33. How can you iterate over a list of items in Bash?

a) for item in list; do ... done

b) foreach item in list; do ... done

c) while item in list; do ... done

d) loop item in list; do ... done

Answer: a) for item in list; do ... done

Explanation: The for loop is used to iterate over a list of items and execute commands for

each item.

34. How do you test if a directory exists in Bash?

a) if [ -d directory ]

b) if [ -f directory ]

c) if [ directory ]

d) if [ -e directory ]

Answer: a) if [ -d directory ]

Explanation: The -d option checks if a directory exists.

35. What does the cut command do in Bash?

a) Extracts sections from lines of files

b) Deletes files

c) Copies files

d) Moves files

Answer: a) Extracts sections from lines of files

Explanation: The cut command is used to remove sections from each line of files based on

specified delimiters.

36. How can you display the current date and time in a Bash script?

a) date

b) time

c) current_time

d) show_date

Answer: a) date

Explanation: The date command displays the current date and time.

37. How can you search for a pattern in multiple files using grep?

a) grep pattern file1 file

b) grep -r pattern file1 file

c) grep pattern *

d) grep -l pattern file1 file

c) Displays the value of a variable

d) Renames a variable

Answer: a) Deletes a variable

Explanation: The unset command removes a variable from the environment.

43. How do you create a subshell in Bash?

a) ( command )

b) [ command ]

c) { command }

d) command

Answer: a) ( command )

Explanation: Commands enclosed in parentheses () are executed in a subshell.

44. How can you include special characters in a string?

a) Use double quotes

b) Use single quotes

c) Use backslashes

d) Use braces

Answer: a) Use double quotes

Explanation: Special characters can be included in a string by enclosing the string in double

quotes.

45. How can you execute a command in the background?

a) command &

b) command background

c) command bg

d) command -b

Answer: a) command &

Explanation: Appending & to a command executes it in the background.

46. How do you check if a command was successful?

a) By checking the value of $?

b) By checking the value of $!

c) By checking the value of $#

d) By checking the value of $@

Answer: a) By checking the value of $?

Explanation: The $? variable holds the exit status of the last executed command.

47. What is the purpose of the trap command?

a) To handle signals and execute commands when a signal is received

b) To set file permissions

c) To display system information

d) To search for files

Answer: a) To handle signals and execute commands when a signal is received

Explanation: The trap command allows you to specify commands to be executed when the

script receives certain signals.

48. How can you define a function in Bash?

a) function_name() { ... }

b) def function_name { ... }

c) function function_name { ... }

d) func function_name { ... }

Answer: a) function_name() { ... }

Explanation: Functions in Bash are defined using the function_name() { ... } syntax.

49. How do you pass arguments to a Bash function?

a) Using $1, $2, etc.

b) Using arg1, arg2, etc.

c) Using %1, %2, etc.

d) Using @1, @2, etc.

Answer: a) Using $1, $2, etc.

Explanation: Inside a function, arguments are accessed using $1, $2, etc., corresponding to

the positional parameters.

50. How do you create an array in Bash?

a) array=(value1 value2 value3)

b) array{value1, value2, value3}

c) array[value1, value2, value3]

d) array<value1, value2, value3>

Answer: a) array=(value1 value2 value3)

Explanation: Arrays are created using parentheses () with space-separated values.

51. How can you access the third element of an array?

a) ${array[2]}

b) ${array[3]}

c) ${array[1]}

d) ${array[0]}

Answer: a) ${array[2]}

Explanation: Array indices start at 0, so the third element is accessed using ${array[2]}.

Answer: a) command1 || command

Explanation: The || operator executes the second command only if the first command fails

(returns a non-zero exit status).

57. How can you redirect both standard output and standard error to the

same file?

a) command > file 2>&

b) command >> file 2>&

c) command 2> file 1>&

d) command 1> file 1>

Answer: a) command > file 2>&

Explanation: The 2>&1 syntax redirects standard error to the same destination as standard

output.

58. How can you read the entire content of a file into a variable?

a) variable=$(cat file)

b) variable=< file

c) variable=cat file

d) variable=read file

Answer: a) variable=$(cat file)

Explanation: The $(cat file) command substitution reads the entire content of a file into

the variable.

59. What does the set -e command do in a script?

a) Exits the script if any command fails

b) Sets the execution environment

c) Enables debugging mode

d) Lists all environment variables

Answer: a) Exits the script if any command fails

Explanation: The set -e option causes the script to exit immediately if any command

returns a non-zero exit status.

60. How can you display the number of lines in a file?

a) wc -l file

b) lines file

c) count lines file

d) file -n

Answer: a) wc -l file

Explanation: The wc -l command counts the number of lines in a file.

61. How do you check if a variable is set and not null in Bash?

a) if [ -n "$var" ]

b) if [ -z "$var" ]

c) if [ "$var" ]

d) if [ "$var" != "" ]

Answer: a) if [ -n "$var" ]

Explanation: The -n option checks if the variable is set and not null (i.e., it has a non-zero

length).

62. What does the cut command do with the -d option?

a) Specifies a delimiter

b) Removes duplicate lines

c) Changes the file extension

d) Compresses the file

Answer: a) Specifies a delimiter

Explanation: The -d option in cut specifies the delimiter to be used when cutting fields

from lines.

63. How can you get the exit status of the last command in Bash?

a) $?

b) $!

c) $@

d) $#

Answer: a) $?

Explanation: The $? variable holds the exit status of the last executed command.

64. What is the purpose of the source command?

a) Executes commands from a file in the current shell

b) Creates a new file

c) Lists all files in a directory

d) Changes the current directory

Answer: a) Executes commands from a file in the current shell

Explanation: The source command runs commands from a specified file in the current

shell, allowing changes to environment variables.

65. How can you check the status of a service in a Bash script?

a) service status

b) systemctl status

c) status service

d) check service

c) merge file1 file2 > output_file

d) append file1 file2 > output_file

Answer: a) cat file1 file2 > output_file

Explanation: The cat command concatenates the contents of multiple files and redirects the

output to a new file.

71. What is the purpose of the .bashrc file?

a) To configure system-wide environment variables

b) To store user-specific shell configuration

c) To set up system services

d) To manage user permissions

Answer: b) To store user-specific shell configuration

Explanation: The .bashrc file is used to store user-specific shell configuration and

initialization commands for non-login interactive shells.

72. What does the -e option do in a Bash script?

a) Enables debugging

b) Exits the script if a command fails

c) Echoes commands as they are executed

d) Executes commands in the background

Answer: b) Exits the script if a command fails

Explanation: The -e option causes the script to exit immediately if any command exits with

a non-zero status.

73. How do you make Bash script files executable?

a) chmod +x script.sh

b) chmod 777 script.sh

c) set +x script.sh

d) bash script.sh

Answer: a) chmod +x script.sh

Explanation: The chmod +x command sets the execute permission on the script file, making

it executable.

74. What does the -x option do in Bash?

a) Executes commands in the background

b) Displays commands and their arguments as they are executed

c) Exits the script if a command fails

d) Enables extended pattern matching

Answer: b) Displays commands and their arguments as they are executed

Explanation: The -x option prints each command and its arguments to the terminal before

executing it, which is useful for debugging.

75. How can you disable the display of command traces in a script?

a) set +x

b) set -x

c) set +e

d) set -e

Answer: a) set +x

Explanation: The set +x command disables the display of command traces that were

enabled with set -x.

76. What does the -n option do in Bash?

a) Executes commands non-interactively

b) Suppresses command output

c) Specifies a non-interactive shell

d) Makes a script file non-executable

Answer: b) Suppresses command output

Explanation: The -n option checks the script for syntax errors without executing it.

77. What is the role of the PS1 variable in Bash?

a) Specifies the prompt string for interactive shells

b) Defines the script file path

c) Sets the default editor for Bash

d) Manages history file size

Answer: a) Specifies the prompt string for interactive shells

Explanation: The PS1 variable defines the primary prompt string that appears in interactive

shells.

78. How do you set the Bash history file size?

a) HISTSIZE

b) HISTFILESIZE

c) HISTORYSIZE

d) HISTFILE

Answer: b) HISTFILESIZE

Explanation: The HISTFILESIZE variable sets the maximum number of lines contained in

the history file.

79. How can you make changes to the .bash_profile take effect?

a) source ~/.bash_profile

b) chmod +x ~/.bash_profile

c) bash ~/.bash_profile

d) export ~/.bash_profile

a) Performs a case-insensitive search

b) Displays line numbers

c) Shows only matching parts of the line

d) Inverts the match

Answer: a) Performs a case-insensitive search

Explanation: The -i option makes grep perform a case-insensitive search.

85. How do you specify the delimiter for the cut command?

a) -d

b) -f

c) -s

d) -t

Answer: a) -d

Explanation: The -d option specifies the delimiter used by the cut command to separate

fields.

86. What does the -a option do when used with the tar command?

a) Extracts all files from the archive

b) Appends files to an existing archive

c) Lists the contents of the archive

d) Creates a new archive

Answer: b) Appends files to an existing archive

Explanation: The -a option appends files to an existing archive.

87. How can you display the last 10 lines of a file using tail?

a) tail file

b) tail -n 10 file

c) tail -l file

d) tail -10 file

Answer: a) tail file

Explanation: By default, tail displays the last 10 lines of a file.

88. What is the purpose of the -f option with the tail command?

a) Follows a file as it grows

b) Specifies the number of lines to display

c) Filters lines based on a pattern

d) Forces the file to be overwritten

Answer: a) Follows a file as it grows

Explanation: The -f option makes tail follow the file as new lines are added.

89. How do you configure Bash to automatically correct spelling errors?

a) shopt -s cdspell

b) shopt -s nocorrect

c) set -o errspell

d) shopt -u cdspell

Answer: a) shopt -s cdspell

Explanation: The shopt -s cdspell command enables automatic correction of spelling

errors in directory names.

90. What is the purpose of the HISTCONTROL variable?

a) Controls how commands are stored in the history file

b) Sets the maximum number of history entries

c) Defines the format of the history file

d) Manages the history file location

Answer: a) Controls how commands are stored in the history file

Explanation: The HISTCONTROL variable can be used to control how commands are stored in

the history file, such as ignoring duplicate commands or commands starting with a space.

91. How do you make a Bash script run as a login shell?

a) By adding #!/bin/bash -l at the top of the script

b) By executing bash --login script.sh

c) By setting the LOGIN_SHELL variable

d) By sourcing the script from .bash_profile

Answer: b) By executing bash --login script.sh

Explanation: Running bash --login script.sh executes the script with a login shell.

92. What does the -c option do in the bash command?

a) Reads commands from a string

b) Specifies the configuration file

c) Compiles the script

d) Clears the terminal screen

Answer: a) Reads commands from a string

Explanation: The -c option allows bash to read and execute commands from a provided

string.

93. How can you list all the environment variables?

a) printenv

b) env

c) set

d) listenv

Answer: c) set

Explanation: The set command lists all environment variables and shell functions.