Shell Scripting Interview Questions & Answers | Linux Admin Certification Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you're looking for linux shell scripting interview questions for the experienced freshers or the intermediate level you're at the right place hi all I'm a partner from Ed Eureka and in this module we are going to discuss all you need to crack a shell scripting interview but before we begin let's discuss the agenda for today first of all I'm gonna discuss a little bit about the market for shell scripting and then we are going to go ahead straight up to the questions and answers on shell scripting I've divided this into three segments for beginners intermediates and for the advanced level professionals each of those segments would have half the questions on theory and have the questions executed on the terminal so without much ado let's get straight into the module so there are a lot of opportunities from many reputed companies in the world and according to research Linux shell scripting has a market share of about 17 percent with a three month moving average salary of 60,000 pounds in the UK in permanent IT jobs citing shell script which also makes Linux one of the top ten occurring IT job requirements since 2018 Linux has started to expand its market rapidly and if you're interested you still have an opportunity to move ahead in your career using shell scripting so I'd like to keep the segment short and sweet let's straight up move ahead to the questions so first of all the most basic what is a shell nutshell is a command-line interpreter basically what it does is it translates the commands entered by the user and then converts them into a language that is understood by the kernel the typical operations that it performs include file manipulation program execution and printing texts so basically the shell is the utility that processes your when you type in a command at your terminal the shell interprets that command and calls the program that you want the shell uses standard syntax for all the commands so obviously the next logical question is what is a shell script and can you name some of its advantages so shell script is basically a computer program designed to be run by a Linux or a UNIX shell which is in the form of a command containing text file which has one or more commands that are written in an order of execution it has two main advantages one is it facilitates you to develop your own OS with relevant features best suited for you and secondly you can design software applications according to your desired platform next what are the different types of variables that are used in the shell script now there are basically two types of variables there are system variables and user defined variables so the system variables are defined or created by the operating system or Linux itself whereas the user defined variables are created or defined by the system users now the system defined variables are generally defined in capital letters and can be viewed by the set command whereas the user defined variables can be viewed using the command echo question number four what are the different kinds of commonly used shells on a typical Linux systems now firstly there are two main kinds of shells you have your bourne shell and your c shell some common derivatives of bourne shell that we use some very popular ones are the bourne shell or the regular shell you have the cone shell you have the bourne-again shell or bash and the POSIX shell some common sea shell types are the sea shell the 10x sea shell and the sea shell next how do you create a shortcut in Linux now you can create a shortcut in Linux with the help of links there two kinds of links you have the soft link and you have the hard link so before you understand what a soft link is hard link is I also like to explain what and in orders now there's something called an e note for every file of the file system which has information along with all the file attributes except for its name now a soft link is an actual link to the original file these links will have different notes with different values the soft link points to the original file so if the original file is deleted then the soft link fails if you delete the soft link nothing will happen to the file the reason for this is that the actual file or directories in ode is different from the soft link created with the files in ODEs a hard link acts like a mirror copy of the original file these links share the same enols changes made to the original or the hard link file will reflect on another that is why when you delete a hard link nothing will happen to the other file hard links cannot cross file systems whereas soft links can hence deleting the original file does not affect the hard linked one whereas deleting the original file makes the soft link inactive next okay now this is a question which is very common can you tell me something about the superblock in shell scripting now the superblock is basically a program that contains all the information regarding a specific file system it contains information such as the size of the file the block size used by its number of free data blocks and the list of free innards and data blocks question 7 what is GUI scripting now when we talk about Linux mostly what we talk about is the CLI or command-line interface scripting but there's also something known as graphical user interface which is used for controlling a computer and it's applications now the GUI scripting suppose different applications which mostly depend on the OS next what are the various stages of a Linux process that it passes through now basically there are four stages namely there waiting running stopped and the zombie mode now waiting is basically when the Linux process waits for resource then it starts running which means the process is being executed then after successful execution the Linux process is set to have stopped now the fourth one is an interesting one which is called a zombie stage now it is known as zombie because the process has already stopped but in the process stable it is still active question 9 what is the difference between the break and the continued statements now a lot of the times when you do basic scripting you might use break and continue statements for pretty much similar things but they are actually a little different the break command is a way to escape out of the entire loop in progress whereas the continue command causes you to escape only the current iteration you do not skip the whole entire loop so when you want to test whether each iteration of your loop is executable you use the continue statement in which case it just jumps out of every Ayten that has an error and goes on executing those which don't next question what is the significance of the shebang line in shell scripting now you might have seen the shebang line at the top of each script basically what it does is that it determines the location of the engine which is to be used to execute the script it simply provides information regarding the location where the engine is placed it is also neglected by some of the users if they want the same now with that let's move on to some questions which we can execute in our terminal so how do you pass an argument to a script for this let me just go ahead increase the size of this so you can see better I think this is OK so basically this is how you pass an argument to a script you use the cat command and the dollar one sign which basically is used to take an argument just going to exit from here we are going to change its permissions and then we are going to execute it so basically this is how you take in an argument the simple script is basically used to show the file name this dollar one is a special variable which displays the file name the file name here is question 11 or q 11 not Sh so what I'm gonna do is I'm gonna change permissions and then dot slash so here I have a simple script which basically water does is it will show the filename so here I have a concatenate dollar one dollar one means it's going to take in your first argument and that is basically what the question is so I'm just going to exit this and I'm going to make sure that it is executable so basically what I'm going to do is run that file and then pass a file name as an argument so I have already created something known as a test file and that is how it works okay with that let's go on to our next question so question 12 how to use arguments in a script so basically what I'm going to do is copy the file argument one to the destination which is my argument to it's basically I'm going to take in two different arguments and copy the first one the first one is going to be a file - the second one which is going to be a pot all right pretty simple same thing I'm gonna do again which is make sure it's executable so I'm gonna give a filename let's do the test file that I had created earlier and then copy it to public and if I put in the same part you can see that our test file is right here all right pretty simple now let's clean this next these are going to be pretty simple scripts as this is just the first part of the interview questions these are for beginners so next is how to calculate the number of passed arguments so again you can just go ahead and do a dollar hash but if you want to do it properly like in a script form we have the script for you this is just a fancy way of doing what I just did some time ago all right so number of parameters passed it's going to print and then it's going to take in the number of parameters that you have passed dollar hash all right so I'm going to pass two parameters here I'm going to write at Eureka Lerner how's that and as you see I've gotten okay next is how to get the script name inside a script now again there's a very simple way to do it all you have to do is dollar 0 which will basically print out the script name but since we're doing it in a script which is going to print it out same thing doll zero and it prints out the name of the script which is q14 all right next how do you check if a previous command has been run successfully now you might have noticed that I haven't really cleared my screen art this is so that you can see my previous command has been run successfully right so to check if it has been run successfully what I can do is simply go Dollar and question mark oh and put in print which is the echo and if the answer is zero basically your script has run successfully if it is one it has not now there may be other numbers shown to you depending on the kind of error that is there in your script now such a thing can also be done in a script form so if you want to do it in a more organized manner you can do this so basically what I've done is I've created a variable and assigned the exit status which is what this dollar and question mark special variable is it basically gives you the exit status of your previous script now if that variable is zero which as we saw which means your script has been run successfully then you're supposed to print this message and if it's not you're supposed to be printing this message all right so let me exit this and because our previous script was run successfully it prints this message I can see my spelling mistake here but there's a scripting tutorial and not an English tutorial so we can just let that slide so let me clear that out next we have question number 16 how to get the last line from a file so basically it's a very simple way to get the last line from a file all you have to do is tale and - 1 and then you put in your file name and it gives you the last line of that particular file this is basically what's there in the file let me concatenate and short to you yeah so this is basically a test file it has three lines all having test one test one test one now you can also go ahead and change the number and it will print that many lines from the bottom so there you go next question how to get the first line from a file now this is pretty much like the previous question so instead of tail and a - a certain positive integer what you can do is you can go ahead and type head and - one and then put in your file name and it'll give you the first line from that particular file called as well one it's pretty self-explanatory it's a pretty simple question next question how do you the third element from each line from a file so this goes along the same lines of the previous two so basically what I'm doing here is that I'm using the aw K to print out the third column or the third element from any given file that I am taking in as argument nothing too complicated so let's just exit this right so dot slash okay so it only makes more sense if I show you the file that I am using this is the text file that's basically a test file tree and as you can see my third entire row is test test test test test this is basically what it's supposed to be printing a row full of tests right so if I run this and I put in test file 3 as my argument it prints out my third element from each given row all right next question number 19 so how to write a function in shell script so this is a very simple example of a function it's basically function example is the name of the function and it has a basic print statement called echo hello learner there are parentheses which begin at the end of you're naming the function and they close at the end of your function it's nothing too difficult as the function keeps on complicating this might get complex but the basic structure of this script will not change okay and finally question 20 this is the last question of this particular segment write down the syntax for all the loops in shell scripting okay let me open and edit for this so first of all let's start with the for-loop how the syntax goes is you go for and then you have a variable say ijk and then you put in your condition for I equal to anything and then this is the beginner of your loop says done then you have your statement that you're going to execute and then you're going to go ahead and go done which is the end of your loop so this is how your for loop works all right now let me go ahead and quickly put in a for loop a simple script that I can run and show you you and upon execution you will get this result with a very simple for loop printing out all the variables from one tonight now next comes the while command so again you have your white statement and then whatever condition it is that you want to imply and then as the loop begins you have your do and then you have your statement that you want to execute and then done all right this is how your while command should look like this is the basic structure of how it should work again I'll try to run the same command that I did for for except for this time I'll be using the while command you and when we try so this is basically the Y command that I've written I have taken a variable e and here the while command states that if your value of a is less than 10 then you're going to print a and then add one to that particular e and then this goes on and on and on till your E is no longer less than 10 all right so again it prints from 0 to 9 same as the for loop then you have the until loop which is kind of like the while loop but it is slightly different so I am going to clean this out so seem as while you have your until and then your condition and then do and your statement to be executed and then done with that you close the loop all right this is basically what your until condition should look like go ahead and clean this out again I'm gonna do the same thing with the until command so here instead of while I'm going to go and do while so until your ear means less than 10 and what stayed inside the loop remains the same you're going to print a as you can see as I done while before a is starting from zero that is the value that has been allotted and then again we are during one to a we are incrementing a by one and then we are closing the loop with our done so when we run it so when we run it we get the same exact thing now that was three different loops for the same exact result but see how differently those things are executed here I have put them in for achieving the same thing but there are different loops for different utilities in shell scripting with that we come to the end of the first segment of our shell scripting interview questions now let's move ahead to slightly tougher ones which are for the intermediate level of professionals our shell scripting interview questions now let's move ahead to slightly tougher ones which are for the intermediate level of professionals so the first question in this segment where exactly can you store the cell programs in the system the answer is pretty simple they are stored in a file which is tagged as SH or the Bond shell as you might have noticed there is the shebang line that you put right in the beginning that basically points to the fire where your program is going to be stored what makes the shell are more preferable option than the bond shell well that three points would speak more in favor of the C shell than in the Bourne shell firstly all the commands in the C shell can be aliased whereas in the Bourne shell all of them cannot be aliased secondly C shell is great for reusing your script lengthy commands can be used again and again in the C shell whereas in the Bourne shell it does not allow the same axis and the third and final thing is the access to command history which is there again in the case of C shell and not there for the Bourne shell okay so the next question is what is the difference between these two special variables one of them is a dollar and asterisk sign and the other one is a dollar followed by the add the red sign now the meaning of these two are pretty identical when not quoted or used as a parameter assignment value or as a file name however when you use it as a command argument what the dollar and add sign does that it treats each quoted argument as a separate argument but when it is the dollar sign followed by an asterisk it will consider the entire set of parameters as one single string so if you're passing a sentence as a number of parameters to a script what the dollar and add sign will do is that it will print each word on a different line because it treats them as different parameters but the asteroid which follows the dollar sign will not do the same it will take the entire sentence as one single string and print them on one line so now let's move on to the next question how would you compare the strings in a shell script now basically you use this command known as the test command which is used to compare the test strings now how this works is that this command will compare text strings by comparing each character in each string so suppose I have two strings both the same called word first the test command is going to check the first character of each which is W once that matches it's going to go to o and so on and so forth once all of these match it declares them to be the same string next how do you redirect both standard outputs and the standard error to the same location well there are two methods to redirect standard output and error to the same location and they are what you see on your screens right now these are the two commands which will redirect your standard output and standard error to the same location next is a differentiation between single and double quotes which I shall demonstrate further in this segment so your single quote you use it when you do not want to evaluate the variables into values so if you see echo and put in a single quote where you are calling a variable it is going to print that variable as is whereas in a double quote you use it when all the variables that are inside the codes need to be evaluated so in a double quote where there is a variable that is being called it is going to replace that particular variable with the value stored in it this I am going to demonstrate in a little while in this very segment so the question is when should shell programming or shell scripting not be used so when the task is very complex like writing an entire payroll processing system or there's a high degree of productivity required and it involves various different software tools it is not the most ideal to use shell scripting next question is what is the lifespan of a variable inside a shell script so the lifespan of a variable inside of shell script only lasts until it is executed that is until the end of execution and that is it any further than that the variable inside the shell script is not of any use so what is a file system now basically it is a collection of files which contain information that is related to the files in computing a file system controls how the data is stored and retrieved without a file system information placed in a storage medium would be one last body of data with no way to tell whether one piece of information stops and where the next one begins question 30 what are the default permissions of a file when it is created so the default permissions of a file is 666 which is the user is allowed to read and write the file the user group is allowed to read and write the file and so is everybody else as you can see there is a table on your screen which has numbers as 0 to 7 now each of these numbers have a default permission which is associated to it so when we type in 0 it means there are no permissions 1 is execute 2 is right and 4 is read the remaining you can split as a sum of integers and find out what is the permission associated with it 6 as you can see is 2 plus 4 so it means read and write and finally when you type in 7 it is equal to 1 plus 2 plus 4 which means you can have all permissions to execute read and write we type the same thing tries the first one will stand for the user permission the second is the group permission and the third one is permissions for everybody else and that was all about the default permissions let's move on to the next question so what does it mean by the shebang line SH or the bash at the beginning of every script I'm here at the terminal and I am going to open up a simple script to demonstrate now as you can see there is this shebang line a shebang slash bin slash SH which is there which you will find in the beginning of every shell script now what this line does is it's nothing but the absolute path to the shell interpreter it consists of the shebang which is the hash and bang followed by the full path of the interpreter such as bin slash SH or bin slash bash all the scripts under Linux using this particular interpreter are specified on the first line the shebang must be the first line because it is interpreted by the kernel which looks at the two bytes at the start of the executable file since the kernel will only look at the first two characters and has no notion of further lines you must place the hash and bang in the first line so the program loader mechanism parses the rest of the files initial line as an interpreter directive that was all about the significance of the shebang line let's go back to the presentation and look at the next question now this is determine the output of the following command kind of a question so obviously we are going to go to our terminal so in bash or the bourne shell or any similar shell this is basically a shorthand way of writing an if statement so basically if this is true if this non-zero bracket is true then echo 0 else echo 1 for a simple test like this if you do not want to waste a lot of time or waste five lines of valuable screen editor vertical space then you can take advantage of the logical and which is these two Ambersons and the logical or which is these two pipes and how they work in a shell so echo zero is executed if the statement is true and echo one is executed if this statement is true and this one is false so basically it returns a non zero exit code and this is why it should not be used if there is any chance of that occurring this will give you a result of zero and that is why it should not be used if there is any chance of the latter statement to come true which is the output of one even though this condition is true in other words it's almost equivalent to these five lines of code which I'm gonna put it right here it's basically equivalent to these five lines of code you and you have the same answer now this full form does not have the same floor that the shorthand form does this construct is correct and always works while the shorthand is a quick hack and it is useful in only limited circumstances this particular set flag is only a test of whether a string is empty or not and it will always display a 0 next question is again determine the output sort of a question so this is your command and you have to determine the output of this command now this is the same command in the terminal it's a very simple command it's basically assigning the value John to a variable called name and here in this string it is calling that name in single quotes pay attention this is a single quote and we had discussed this earlier in this segment the difference between single quotes and double quotes later I am going to do the same using double quotes and you will see the difference for yourself here as you can see because it is inside single quotes it does not call the value of name if I try to do the same thing using double quotes it has called the variable which is John and that is the difference between single quotes and other quotes question number 34 again is a determine the output question okay so let's go to our terminal so basically what is happening is the variable is being assigned using this particular notation or some people even use this to expand their own variable now the answer to this is simple the answer is going to be just variable so the value variable has been assigned to the variable new but why are people doing this why is the variable being assigned like this now this technique has its own advantages it allows for a variable to be assigned to a value if another variable is either empty or undefined okay moving on so question number 35 how to get a part of a string variable with an echo command only so for that we have a script here all right so basically we start with taking in a parameter so a variable x and y where X is your start position or from where you want to pick from the string and your Y being length for the part of the string that you're trying to pick out from this particular sentence so this is the string that is allotted to the variable variable and finally we are printing out seven characters from position 11 which is this position we are picking out seven characters from position 11 and it should basically print out my name all right you and it prints out seven characters from the 11th position which is basically a Pastner that's my name so question number 36 sorry write the command that is given so here it is I like and whatever variable we are calling so rewrite the command to print the settings and convert the variable to plural so let's see how we can rewrite and convert this okay so this is what's been given to us I like the certain variable so firstly I am going to allot something to that particular variable so you're gonna go ahead and so basically allotted a string value called plane to variable and here I'm putting variable in curly braces and adding an S so it will take whatever is there in the curly braces and switch it with the value of the variable and that's it a change to our like planes and it's as simple as that question 3,700 all the arguments provided to the script the answer is pretty simple actually you can just go ahead and do a dollar star or you could do a dollar Act if you wanna see how it is executed you can just do cool Dola star and then happy learning and it prints happy learning it's as simple as that next question is how to print the PID of a current shell it's simple you just go ahead and use two dollar signs so if I do echo and two dollar signs you get 6 0 2 3 which is the PID or process ID of this particular shell okay next how to print the first array element you so this is a simple script with an area which is hi my name as a partner and basically what you're doing is printing out the zeroth element it's really not that tough you might have done the same while using any programming language that is you and it printed high which was the first element and this is the final question of the segment how to print all the array elements and their respective indexes so again at the same area that I've taken here I'm printing out all the areas I'm using the @ sign which is used for all the areas and here if I just use the bank before your variable I can print the indexes and by bang I mean the exclamatory sign here in Linux it is called the bank you and there you go hi my name is Posner and the index number zero one two three four with that we've come to the end of the intermediate section next we have some questions for the experienced people with three to five years of experience in shell scripting Linux or UNIX this section will also have some scenario based questions obviously in the practical form so the last few questions are for professionals with three to five years of experience at least here we have a few scenario based questions which may help you build on that resume that you have created with so much care so let's go ahead so if you're experienced you might know there's something known as cron the first question is what is the crontab now the crontab is a list of commands that you want to run on a regular schedule and also the name of the command that is used to manage that list so the schedule is called the crontab would stands for a cron table because it uses the job scheduler cron to execute tasks this is also the name of the program that is used to edit that jewel this software utility has a time-based job scheduler now people who set up and maintain software environments use cron to schedule these jobs to run periodically at fixed inés dates or intervals so next logical question obviously is that what does each field do in a cron tab file now the cron tap file has six fields the first five which tell the Quran when to execute the command they specify the minute our day month and day of the week and the sixth field contains information on which command is to be executed I'm sure a lot of you might be finding a lot of these questions pretty simple but this is mostly because you must be working on Linux on a day-to-day schedule even if all these questions are pretty small they are mostly knowledge-based and there's a high possibility that people who are in touch with shell scripting and Linux because of their daily jobs or because of their university courses will do better in interviews because of practice with that let's move on to question number 43 what are the to files of the crontab command now the to files of the crontab command artron allow which decides which users need to be permitted from using the crontab command and next is the cron dot deny it decides whatever users need to be prevented from using the crontab command in Red Hat based systems such as sent OS these crontab files are stored in the VAR spool cron directory while on Debian and Ubuntu files they are stored in the cron slash crontab directory if you look for it you may just find it in your particular OS next question which come on do you need to use to take backup now to take backup you need the tar which basically stands for tape archive now you can go ahead and add the extension of CBF to zip the files and xvf to unzip the file now tar ball files are also used when you download a number of files from the internet or your emails so tar is basically your most efficient command which you need to take a backup question 44 what are the different commands available to check the disk usage now there are three different commands which you can check the disk usage width you have DF D U and DF space now the DF command which is short for the disk file system is used to show the disk utilization for a Linux system you can display information of the device name total blocks total disk space used space and available disk space and mount points on a filesystem through this command you can also get the information and a human readable format or at a particular partition then there is the D U command which is short for disk usage now this is a useful command because you can find disk usage for files and directories the du command when used with various options provides the results in base formats for example you can find out the summary of disk usage for a directory with all its sub directories using this the output of the kebab shows all the files and directories in /home with block sides and finally you have the DF space which is the command used to check the free disk space same as DF but in terms of MB so that's all about checking the skew Serge let's move on to the next question what are the different communication commands available in shell so there are four different communication commands in the shell you have mail news wall and motd now whenever a mail is sent initially the mail command calls the standard sent mail binary which is located in the user file it allows you to read or send a meal the news command rights system news items to standard output this command keeps you informed of news concerning the system each news item is contained in a separate file in the news directory then you have the wall command the wall or the Linux wall is an abbreviation of write to all so basically this is a utility that displays the content of a computer file or the standard input to all the logged in users it is typically used by route to send out shutting down message to all users just before power off and finally you have M ot d so MOTD basically stands for a message of the day and it is used to send a common message to all the users in a more efficient manner than sending them all an email message or the system's might also have an MOT d feature such as the info segment on multics this feature is a file on all unix-like systems and can be changed at upgrade or during installation time next how would you find out the total disk space used by a specific user suppose the user name is Ed Eureka so the given command here is used to find out the total this space as we had discussed earlier there is the D you command with the path which is home slash at Eureka with the S flag which will show how much the space is used by each subdirectory next how to debug the problems encountered in each script or program so there are two ways to do it one is by inserting debug statements in the shell script to output or display the information which helps identify the problem and second by using set - X we can enable debugging in the script next the difference between single equal sign and the double equal sign now the first one is for assigning value to a variable and the second one is used for string comparison the comparison operator or the double equal to sign behaves differently within a double brackets test as the single equal to sign is an arithmetic operator and the double equal to sign is a string operator as well as an arithmetic operator which is meant not for assigning but for comparison next how to open a read-only file in a shell now if you work continuously with Linux and shell scripting you might know the answer to this basically you use the VI command with the are flag and the file name that you want read to open a read-only file in the shell and this is the last theory question of the module how can the context of a file inside jar be read without extracting in a shell script again this is a command called tar which we had discussed earlier the tar which stands for tape archive and it's flag tvf which can basically be expanded to T meaning the table of contents V means verbose and f means file followed by the filename and the tar extension next question 51 where can you specify two methods of passing parameters to your script okay let's see two methods is it so the first way is going to be how we've done it many times earlier and you might have seen it many times here in this video you basically write your script name and then you pass your parameters one way is this and next is you can use your read with the PFLAG and you can pass your parameter inside with your destination hostname so those are the two ways in which one can read parameters next write a shell script to get the current date time username and current working directory so here I have written alright so here is our script it's pretty simple to follow actually your hello whatever your log name is then there is date which is going to include time and this is the user and this particular statement is going to print out your current directory which is the PWD command so we are gonna change the permissions associated to it and then we were on this so here I have my log name which is at Eureka the date command printed out the date and time this is the username and the current directory slash home slash at Eureka which you can see even here it's pretty simple each command is very self-explanatory let's move on to our next question how do you find all the files modified in less than three days and print the record count of each let's go ahead okay so basically you're going to use the find command and the EM time flag then here is the number of days and we're going to execute it and then whatever it is that we are finding out from our list that we are going to put into a text file called the last three days all right so it's going to take some time and it's done now we go ahead and do an LS you can see the last three days text file is created and if we open the last three days text file you see all the files that have been modified in the last three days right next question right a shell script that adds two numbers if provided as the command-line argument and if the two numbers are not entered it throws an error message the scenes like an interesting one let's go ahead and so let me explain to you a little bit about what's happening in the shell script so firstly we are starting out with an if statement which is like a preventive step that if two inputs are not received from the standard output then you are going to execute the below statement and then you're gonna explain to whoever the user is that you have to put in two integers for which I will print the sum and that is what is written here and then you have your exit statement and then you exit the if statement and at the end of it if they do put in two positive integers the statement basically prints the some of those integers it's pretty simple so let me exit this you so first I'm going to run the script without entering two numbers as command-line argument so let's see what happens if I do that here prints out this statement it asks you for two numbers x and y where x and y are two numbers for which the script is going to print the sum next again let's try to enter x and y when the numbers are entered as we are instructed it says sum of five and three is eight and that's how simple it is hence the script fulfills the condition as suggested in the question next it asks print a given number in reverse order using a shell script such that the input is provided using the command-line argument only so let me open all right basically this is what the algorithm is let your input number be n which is going to be taken in by the command line and then we are setting the variable reverse as zero and SD or single digits is set up to zero as well then your single digit is going to be achieved by taking n modulo ten this will find and give the single leftmost digit then you're going to reverse the number that is generated as your reverse into ten plus your single digit and then decrease the input number by one now then if your n is greater than zero then you go back to step number three and then print reverse and hence finally your reverse number is printed you so now I'm going to run the script and I'm going to pass in a number say 1 2 3 4 5 now here I haven't put in a number so again like the previous question it is going to instruct me on how to make use of this for example this so let me go ahead and give them what they want I put in a space and go for one two three four five and it gives me back the reverse number which is five four three two one next question calculate a real number calculation directly from the terminal and without a shell script now I know that this is a shell script tutorial but there are a few things which are related to the shell script but you have to run them directly from your terminal so this is important so let's go ahead and do that so we need to use the BC command in a special way for this answer so what were you gonna do is we're gonna print it out and then we're gonna put it we're gonna put two real numbers let's say four point four three two just and then pipe in and it gives us the answer so basically what you're gonna do is you need to use the pipe space BC command where the answer of these two real numbers is going to be piped into the BC command for that obviously you need your BC maths package installed with that let's move on to our next question how can you get the value of pi' till a hundred decimal places so basically what we're gonna do is we are going to use the BC math package again here I'm going to directly print this out I'm scaling this up 200 I'm putting a scale as 100 and Here I am putting in as a pie and here you have it this is the value of pi to 100 places now again I'd like to remind you that for this to work you need to have installed your BC math package since my system has sent OS running I use yum install for other red hat enterprise limited os's you can use apt-get and for debian based systems you can use DNF next question how to check if a directory exists this is a pretty simple script here you are taking in as a parameter or an argument your name of the directory which I'm allotting into my dir and this is your directory flag so basically if the answer to this is nonzero then it's going to print your directory exists and then you close your if statement with an F I all right let's go ahead and try and run this you but in the name of a directory say public and then it says directory exists so if I go ahead and type this is your question fifty-eight and says let's see public and you know the directory exists because if I open my list of directories here you can see in blue you have public okay so let's clear this out so question number 59 can you write a script to portray how set - X works now as we had discussed earlier this is a way of debugging a script so let's go and see how it works so here I have written a script which obviously is going to throw an error at me so if I hash out comment out this set - X line let's see the answer that we get it basically shows this as I said it is a script that are supposed to throw an error at me now let me go back and uncomment this particular line and there you have it now in addition to displaying the intended output in the loop iteration lines it enables debugging with the set - X and also shows each line of execution preceded by a plus sign although this can become impractical when it comes to larger shell scripts it should be obvious how useful this can be when debugging a shell script to identify the root cause of the problem let me clear this out and finally this is your last question how will you find the total disk space used by a specific user so as I had mentioned before we are going to be using the D U command and then go with my user the current user that is Ed Eureka and here you have each directory each file how much space they have taken this seems like a pretty long list to me all right would that we've come to the end of all these interview questions of course we do not have the time or reach to give all the interview questions that has ever been asked in each and every interview so feel free to comment down below if you have been asked any good questions in any shell scripting interviews that you have appeared to before with that I'm closing this session thank you and have a great day I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 90,276
Rating: 4.8902678 out of 5
Keywords: yt:cc=on, shell scripting interview questions, shell scripting interview questions and answers, shell interview questions and answers, shell scripting interview questions for experienced, shell scripting interview questions and answers unix, bash shell scripting interview questions, linux shell scripting interview questions, interview questions on shell scripting, linux training, edureka, shell scripting, linux interview questions, unix interview questions and answers
Id: 6ue2luv2I-Y
Channel Id: undefined
Length: 59min 19sec (3559 seconds)
Published: Tue Apr 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.