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 Bc (bash Calculator) In A Shell Script? |
|
Answer» Use the below Syntax to use BC in shell SCRIPT. Variable=`ECHO “OPTIONS; expression” | bc`. Use the below Syntax to use bc in shell script. Variable=`echo “options; expression” | bc`. |
|
| 2. |
How To Define Functions In Shell Scripting? |
|
Answer» A function is SIMPLY a block of of CODE with a name. When we GIVE a name to a block of code, we can then call that name in our script, and that block will be executed. Example is SHOWN below:$ disk usage () { df -h ; } A function is simply a block of of code with a name. When we give a name to a block of code, we can then call that name in our script, and that block will be executed. Example is shown below:$ disk usage () { df -h ; } |
|
| 3. |
Basic Syntax Of Do-while Statement? |
|
Answer» The do-while statement is similar to the while statement but PERFORMS the statements before CHECKING the CONDITION statement. The FOLLOWING is the format for the do-while statement: do { statements } while (condition) The do-while statement is similar to the while statement but performs the statements before checking the condition statement. The following is the format for the do-while statement: do { statements } while (condition) |
|
| 4. |
How To Perform Arithmetic Operation? |
|
Answer» There are two WAYS to perform arithmetic operations:
There are two ways to perform arithmetic operations: |
|
| 5. |
How To Unset Or De-assign Variables? |
|
Answer» ‘unset’ command is USED to de-assign or unset a VARIABLE. Syntax is shown below: # unset <Name_of_Variable> ‘unset’ command is used to de-assign or unset a variable. Syntax is shown below: # unset <Name_of_Variable> |
|
| 6. |
How To Get Input From The Terminal For Shell Script? |
|
Answer» ‘read’ COMMAND READS in data from the terminal (using KEYBOARD). The read command takes in whatever the user types and places the TEXT into the variable you name. Example is shown below: # VI /tmp/test.sh #!/bin/bash echo ‘Please enter your name’ read name echo “My Name is $name” # ./test.sh Please enter your name LinuxTechi My Name is LinuxTechi ‘read’ command reads in data from the terminal (using keyboard). The read command takes in whatever the user types and places the text into the variable you name. Example is shown below: # vi /tmp/test.sh #!/bin/bash echo ‘Please enter your name’ read name echo “My Name is $name” # ./test.sh Please enter your name LinuxTechi My Name is LinuxTechi |
|
| 7. |
How To Put Comments In Your Shell Script? |
|
Answer» Comments are the MESSAGES to yourself and for other users that describe what a script is SUPPOSED to do and how its WORKS. To PUT comments in your script, start each comment line with a hash SIGN (#). Example is shown below: #!/bin/bash # This is a command echo “I am logged in as $USER” Comments are the messages to yourself and for other users that describe what a script is supposed to do and how its works. To put comments in your script, start each comment line with a hash sign (#). Example is shown below: #!/bin/bash # This is a command echo “I am logged in as $USER” |
|
| 8. |
How To Test Files In A Shell Script? |
|
Answer» TEST command is used to perform different test on the files. Basic test are LISTED below: Test: Usage
Test command is used to perform different test on the files. Basic test are listed below: Test: Usage |
|
| 9. |
What Are The Special Variables Set By Bourne Shell For Command Line Arguments? |
|
Answer» The following table lists the special VARIABLES set by the Bourne shell for COMMAND line ARGUMENTS. Special Variables: Holds
The following table lists the special variables set by the Bourne shell for command line arguments. Special Variables: Holds |
|
| 10. |
How Compare The Strings In Shell Script? |
|
Answer» TEST command is USED to COMPARE the text strings. The test command compares text strings by COMPARING each CHARACTER in each string. Test command is used to compare the text strings. The test command compares text strings by comparing each character in each string. |
|
| 11. |
How To Debug A Shell Script? |
|
Answer» A shell script can be DEBUG if we EXECUTE the script with ‘-X’ option ( sh -x myscript.sh). Another WAY to debug a shell script is by using ‘-NV’ option (sh -nv myscript.sh). A shell script can be debug if we execute the script with ‘-x’ option ( sh -x myscript.sh). Another way to debug a shell script is by using ‘-nv’ option (sh -nv myscript.sh). |
|
| 12. |
What Is The Syntax Of For Loop In Shell Script? |
|
Answer» BASIC SYNTAX of for LOOP is GIVEN below: for VARIABLES in list_of_items do command1 command2 …. last_command done Basic Syntax of for loop is given below: for variables in list_of_items do command1 command2 …. last_command done |
|
| 13. |
What Is The Use Of “#!/bin/bash” ? |
|
Answer» #!/bin/bash is the first of a shell script , KNOWN as SHEBANG , where # SYMBOL is called HASH and ‘!’ is called as BANG. It shows that command to be executed via /bin/bash. #!/bin/bash is the first of a shell script , known as shebang , where # symbol is called hash and ‘!’ is called as bang. It shows that command to be executed via /bin/bash. |
|
| 14. |
How To Make A Shell Script Executable? |
|
Answer» USING the CHMOD command we can make a SHELL SCRIPT executable. Example is shown below : # chmod a+x myscript.sh Using the chmod command we can make a shell script executable. Example is shown below : # chmod a+x myscript.sh |
|
| 15. |
What Is The Basic Syntax Of While Loop In Shell Scripting? |
|
Answer» LIKE the for LOOP, the while loop repeats its block of commands a number of times. UNLIKE the for loop, however, the while loop iterates until its while CONDITION is no longer true. The basic syntax is : while [ test_condition ] do commands… done Like the for loop, the while loop repeats its block of commands a number of times. Unlike the for loop, however, the while loop iterates until its while condition is no longer true. The basic syntax is : while [ test_condition ] do commands… done |
|
| 16. |
Tell Me The Syntax Of “case Statement” In Linux Shell Scripting? |
|
Answer» The basic SYNTAX is SHOWN below: case word in command1 command2 ….. last_command !! command1 command2 …… last_command ;; esac The basic syntax is shown below: case word in value1) command1 command2 ….. last_command !! value2) command1 command2 …… last_command ;; esac |
|
| 17. |
What Is The Use Of Continue Command In Shell Scripting? |
|
Answer» The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the ENTIRE loop. Continue command is useful in some scenarios where ERROR has occurred but we STILL WANT to execute the next COMMANDS of the loop. The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where error has occurred but we still want to execute the next commands of the loop. |
|
| 18. |
What Is The Use Of Break Command? |
|
Answer» The break COMMAND is a simple way to escape out of a loop in progress. We can USE the break command to exit out from any loop, INCLUDING while and until loops. The break command is a simple way to escape out of a loop in progress. We can use the break command to exit out from any loop, including while and until loops. |
|
| 19. |
How To Compare Numbers In Linux Shell Scripting? |
|
Answer» Test COMMAND is used to compare numbers in if-then statement. Example is SHOWN below: #! /bin/bash X=10 y=20 if [ $x -gt $y ] then echo “x is GREATER than y” else echo “y is greater than x” fi Test command is used to compare numbers in if-then statement. Example is shown below: #! /bin/bash x=10 y=20 if [ $x -gt $y ] then echo “x is greater than y” else echo “y is greater than x” fi |
|
| 20. |
What Is The Use Of “$?” Sign In Shell Script? |
|
Answer» While writing a shell script, if you want to check whether PREVIOUS command is executed successfully or not, then we can use “$?” with if statement to check the EXIT status of previous command. Basic example is SHOWN below: root@localhost:~# ls /usr/bin/shar /usr/bin/shar root@localhost:~# echo $ If exit status is 0 , then command is executed successfully root@localhost:~# ls /usr/bin/share ls: cannot ACCESS /usr/bin/share: No such file or directory root@localhost:~# echo $ 2 If the exit status is other than 0, then we can say command is not executed successfully. While writing a shell script, if you want to check whether previous command is executed successfully or not, then we can use “$?” with if statement to check the exit status of previous command. Basic example is shown below: root@localhost:~# ls /usr/bin/shar /usr/bin/shar root@localhost:~# echo $ 0 If exit status is 0 , then command is executed successfully root@localhost:~# ls /usr/bin/share ls: cannot access /usr/bin/share: No such file or directory root@localhost:~# echo $ 2 If the exit status is other than 0, then we can say command is not executed successfully. |
|
| 21. |
What Is The Syntax Of “nested If Statement” In Shell Scripting? |
|
Answer» Basic Syntax is shown below: if [ CONDITION ] then command1 command2 ….. else if [ condition ] then command1 command2 …. else command1 command2 ….. fi Basic Syntax is shown below: if [ Condition ] then command1 command2 ….. else if [ condition ] then command1 command2 …. else command1 command2 ….. fi fi |
|
| 22. |
How To Redirect Both Standard Output And Standard Error To The Same Location? |
|
Answer» There two METHODS to REDIRECT STD output and std error to the same location: Method:1 2>&1 (# ls /usr/share/doc > out.txt 2>&1 ) Method:2 &> (# ls /usr/share/doc &> out.txt ) There two methods to redirect std output and std error to the same location: Method:1 2>&1 (# ls /usr/share/doc > out.txt 2>&1 ) Method:2 &> (# ls /usr/share/doc &> out.txt ) |
|
| 23. |
What Are The Different Type Of Variables Used In A Shell Script? |
|
Answer» In a SHELL script we can use TWO types of variables:
System defined variables are defined or created by Operating System (Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command. User defined variables are created or defined by system USERS and the values of variables can be viewed by using the command “echo $<Name_of_Variable>”. In a shell script we can use two types of variables: System defined variables are defined or created by Operating System (Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command. User defined variables are created or defined by system users and the values of variables can be viewed by using the command “echo $<Name_of_Variable>”. |
|
| 24. |
What Is The Default Login Shell And How To Change Default Login Shell For A Specific User? |
|
Answer» In LINUX like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change default shell using the “chsh” COMMAND. Example is shown below: # chsh <username> -s <new_default_shell> # chsh linuxtechi -s /bin/sh In Linux like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change default shell using the “chsh” command. Example is shown below: # chsh <username> -s <new_default_shell> # chsh linuxtechi -s /bin/sh |
|
| 25. |
What Is Shell Script And Why It Is Required? |
|
Answer» A SHELL Script is a text file that CONTAINS one or more commands. As a system administrator we often need to issue number of commands to accomplish the task, we can ADD these all commands together in a text file (Shell Script) to complete DAILY ROUTINE task. A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish the task, we can add these all commands together in a text file (Shell Script) to complete daily routine task. |
|