Continuing to the above series here we are coming with another 5 wonderful Linux Interview Questions and their answer. Your (The Tecmint.com Readers and frequent Visitors) support is always needed to make it successful.
1. Write a shell script to get current date, time, user name and current working directory.
Now create a file called ‘userstats.sh‘ and add the following code to it.
Place execute permission and run the script as shown below.
Sample Output
2. Write a Shell Script that adds two numbers if provided as the command Line Argument and if the two numbers are not entered it outputs an Error Message along with a one-Line of how-to use description.
Again create a file called ‘two-numbers.sh‘ and add the following content to it.
Set executer permission on the file and run the script as shown below.
Condition 1: Running the script without entering two numbers as command line argument, you will get the following output.
Sample Output
Condition 2: When Numbers are entered as command line argument you will get the result as shown.
Hence the above shell script fulfills the condition as suggested in the question.
3. You needs to print a given Number say 10572, in reverse order using a Shell script such that the input is provided using command Line Argument only. If the input data is not provided as Command Line Argument, it should throw and error and should suggest, how to use the script. Write the script but before that tell me the algorithm that needs to be implemented here.
Algorithm
- 1. Let the Input Number = n
- 2. Set rev=0, sd=0 (Reverse and single digitis set to 0)
- 3. n % 10, will find and give single left most digit
- 4. reverse number is generated as rev * 10 + sd
- 5. Decrease Input Number (n) by 1.
- 6. if n > 0, then goto step 3 else goto setp 7
- 7. Print rev
Now again, create a file called ‘numbers.sh‘ and add the following given code it.
Grant a execute permission on the file and run the script as shown below.
Condition 1: When Input is not provided as command line argument, you will get the following output.
Sample Output
Condition 2: When Input was provided as command line Argument.
The above script worked perfectly and the output was just what we needed.
4. You are supposed to calculate a real number calculation directly from terminal and not any shell script. What will you do (say the real numbers are 7.56 and 2.453)?
For example, run the following command to calculate numbers in real time using bc command as shown.
5. You are supposed to find the value of pi upto 100 places of decimal, what is the easiest way to get the result done.
Obviously! We must have package ‘pi‘ installed. Just do a apt or yum to get a required package to install ‘pi‘ onto the distribution you are using.
That’s all for now. I’ll be here again with another interesting article very soon. Till then stay tuned and connected to Tecmint.com. Don’t forget to provide us with you valuable feedback in the comment section below.
No comments:
Post a Comment