InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How to use pipe commands? |
|
Answer» Pipe command allows you to use several commands in the same way, in which the output of one is used as input for another. Like a pipeline, each process output is directly input to the next one. A pipe is represented by the symbol "|". The flow of data through a pipeline is UNIDIRECTIONAL, i.e., from left to right. Syntax : command_1 | command_2 | command_3 | .... | command_NConclusionYou can automate a lot of repetitive manual tasks with shell scripting, which is an easy and powerful programming method. Modern programming languages offer many of the features that make them appealing to programmers. DevOps engineers and automation testers often find this concept vital in preparing for interviews. Shell scripting allows the development of simple to complex scripts. A number of daily tasks can be AUTOMATED using shell scripting as well. We THOUGHT of making your job easier by putting TOGETHER an ensemble of the most frequently asked interview questions and sample answers on shell scripting. These interview questions will DEFINITELY help you understand that shell is basically an interface between the user and the operating system, which interprets user commands to be executed by the kernel or operating system. Useful Resources: Unix Interview Automation Testing Interview Devops Interview |
|
| 2. |
How can we create a function in shell script? |
|
Answer» In other programming languages, shell FUNCTIONS are much like subroutines, procedures, and functions. The syntax for declaring a function is as FOLLOWS: function_name () { list of commands}Function_name is the name of your function, and that's what you'll use to call it from anywhere in your script. The function name must be followed by PARENTHESES, then a list of commands enclosed in braces. Example: The FOLLOWING example shows how function can be used: #!/bin/sh# Define your function here Hello () { echo "Hello World" } # Invoke your functionHelloThe following output will be displayed after execution: $./test.shHello World |
|
| 3. |
Write difference between grep and find command. |
Answer»
Example: grep “apple” file1.txt //Displays all the lines with the word “apple” in the file1grep “apple” file1.txt file2.txt //Scan MULTIPLE documents and search the word “apple” in both files.
Example: find –type f // Command will find all the filesfind –type d //Command will find all the directoriesfind . –name file1.txt //Command will find file1.txt in the CURRENT directory. |
|
| 4. |
Write the difference between $$ and $!? |
|
Answer» You can USE $$ to get the PROCESS ID of the current process. However, $! displays the process id for a background process that recently WENT away. |
|
| 5. |
Name the command that one should use to know how long the system has been running. |
|
Answer» Using the Uptime command, you can see how long your system has been ACTIVE. You can also determine the number of users with RUNNING sessions, and the AVERAGE system load for 1, 5, and 15 minutes. The information displayed at once can also be filtered based on your specified options. Example: $ uptime Entering the above command at the shell prompt, namely $ uptime, will PRODUCE the following output: 9:21am up 86 day(s), 11:46, 3 users, load average: 2.24, 2.18, 2.16 |
|
| 6. |
Explain how you will open a read-only file in Unix/Shell. |
| Answer» | |
| 7. |
How will you find total shells available in your system? |
|
Answer» Within your system, there are several shells AVAILABLE. If you want to find all the shells on the system, you can USE $ CAT /etc/shells. Example: $ cat /etc/shells EXECUTION over SHELL Interpreter/Editor: $ cat /etc/shells Output: /bin/sh /bin/bash /sbin/nologin /bin/ksh /bin/csh |
|
| 8. |
What do you mean by metacharacters? |
|
Answer» In a DATA FIELD or program, metacharacters are special characters that provide information about other characters. In SHELLS, they're called regular expressions. A character that is neither a letter nor a number is generally considered a metacharacter. Using shell metacharacters, you can group together commands, redirect and pipe input/output, put commands in the background, reduce the size of file names and path names, etc. You can USE them as wildcards to specify a file name without typing in the file's full name. The most COMMON metacharacters are as follows:
|
|
| 9. |
Name different commands that are available to check the disk usage. |
|
Answer» The following commands are available to CHECK disk space USAGE: |
|
| 10. |
Name the command that is used to display the shell's environment variable. |
|
Answer» Shell functions, along with other Linux programs, are controlled by environmental variables. Any child PROCESS of the shell has access to an ENVIRONMENT variable. These variables are NECESSARY for some programs to function properly. ENV or printenv commands can be used to DISPLAY the shell's environment variables. |
|
| 11. |
Write the difference between “=” and “==”. |
Answer»
|
|
| 12. |
What is the importance of sed command and awk command? |
Answer»
In this case, “s” command in sed replaces the string Hi with “Hello”. Output: Hello Gourav
An awk command/utility assigns variables in following way: $0 -> For whole LINE (e.g. Hello Aisha) $1 -> For the FIRST FIELD i.e. Hello $2 -> For the second field Execution over Shell Interpreter/Editor awk ‘{print $0}’ script10The above script prints all the 3 lines completely. Output: Hello Aisha Hello Rohan Hello GouravExecution over Shell Interpreter/Editor awk ‘{print $1}’ script10The above script prints only the first word i.e., Hello from each line. Output: Hello Hello Hello |
|
| 13. |
What is the best way to debug the shell script/program problems? |
|
Answer» Following are some common methods of DEBUGGING a script: |
|
| 14. |
How will you debug a shell script? |
|
Answer» With set, you can turn on or off DEBUGGING OPTIONS in the SHELL: |
|
| 15. |
What do you mean by the “s” permission bit in a file? |
|
Answer» The SUID (Set User ID) bit is known as the "s" bit. SUIDS are special FILE permissions for executable files that enable other USERS to run the file as the file owner. There will be an s (to INDICATE SUID) special permission for the user instead of the normal x, which represents EXECUTE permissions. A file that has the "s" bit set will grant the process the rights of the file's owner while the program is running. Example: Executing the "passwd" COMMAND to change the password, for example, allows the user to write the new password to the shadow file even though its owner is "root". |
|
| 16. |
How will you pass and access arguments to a script in Linux? |
|
Answer» In SCRIPTS, ARGUMENTS are passed as FOLLOWS: scriptName "Arg1" "Arg2"…."ARGN" Arguments in a script can be ACCESSED as follows: $1 , $2 .. $n |
|
| 17. |
What are interactive and non-interactive shells? |
|
Answer» Interactive SHELL: /bin/bash and /bin/sh is interactive shell. It is a non-login shell that gets started from a COMMAND line. It first copies the parent ENVIRONMENT and then invokes. Non-Interactive shell: /sbin/nologin shell is non-interactive shell. It is PRESENT when the shell SCRIPT is running and just inherits the parent's environment. |
|
| 18. |
Write down the Syntax for all the loops in Shell Scripting. |
|
Answer» In shell scripting, you can USE three looping statements as given below:
Syntax is as follows:
Syntax: for VAR in word1 word2 ... wordN do Statement to be executed doneExample: for a in 1 2 3 4 5 6 7 8 9 10 do if [ $a == 3 ] then break fi echo "Iteration no $a" doneOutput” $bash -f main.sh Iteration no 1 Iteration no 2
Syntax: while command do Statement to be executed doneExample: a=0 while [ $a -lt 3 ] do echo $a a=`expr $a + 1` doneOutput: $bash -f main.sh 0 1 2 3
Syntax: until command do Statement to be executed until command is true doneExample: a=0 until [ $a -gt 3 ] do echo $a a=`expr $a + 1` doneOutput: $bash -f main.sh 0 1 2 3 |
|
| 19. |
Name the command that is used to compare the strings in a shell script. |
|
Answer» To compare text strings, use the test command. By COMPARING each character in each STRING, the test command usually COMPARES text strings. In most cases, tests are generally included as a conditional execution of shell commands. It can be used for:
Using square brackets ([ and ]) around the EXPRESSION is equivalent to testing it with the test. Syntax of test command:
Examples:
Because 50 is greater than 40, this command prints the text "Yes".
As the two strings are identical, this command prints "0" because the expression is true. |
|
| 20. |
Write the difference between $* and $@ |
|
Answer» Unlike $@, where each QUOTED argument is TREATED as a SEPARATE argument, $* treats each positional parameter as a single argument. |
|
| 21. |
Name the command that is used to take the backup. |
|
Answer» The backup is TAKEN using the TAR command. Tar is an ACRONYM for tape archive and is used to create backups using tar, gzip, and bzip. FILES can be saved and restored from and to a tape using this command. In this, files and directories are generally compressed into tarballs, an archive file. For this purpose, it is among the most widely used commands. Furthermore, the tarball can be easily moved from one server to another. |
|
| 22. |
Name two files of crontab command. |
|
Answer» cron.deny and cron.allow are two FILES in the /etc/cron.d directory that controls access to the crontab COMMAND. Crontab command tasks such as creating, editing, displaying, or removing crontab files are relegated to specific users through these files. Both files usually consist of a list of user NAMES, one user name per line. Together, these access control files perform the following functions: |
|
| 23. |
What do you mean by crontab? |
|
Answer» Crontab stands for "cron table," meaning that it makes use of the cron scheduler to perform tasks. In other words, it is the list of commands that you WISH to run on a regular schedule and the command that will let you MANAGE it. It is possible to VIEW or EDIT the table of commands using the crontab command. In addition to the schedule, the term "Crontab" also refers to the name of the program USED to edit the schedule. |
|