5 Practical shell script face questions and answers _linux shell

Source: Internet
Author: User
Tags chmod echo 7 value of pi

Here are 5 interview questions to continue the previous questions and answers about Linux interviews. If you are a tecmint reader, I am very grateful for your support.

1. Write a shell script to get the current date, time, username and current working directory.

Answer: The output username, current date and time, and the current working directory command are logname,date,who I am and pwd.

Now, create a file called userstats.sh, and add the following code to it.

Copy Code code as follows:

#!/bin/bash
echo "Hello, $LOGNAME"
echo "Current date is ' Date '"
echo "The User is ' The Who I am '"
echo "current directory ' pwd '"

Add execute permissions to it and execute him.
Copy Code code as follows:

# chmod 755 userstats.sh
#./userstats.sh

Sample output

Copy Code code as follows:
Hello, avi
Current date is Sat June 7 13:05:29 IST 2014
User is avi pts/0 2014-06-07 11:59 (: 0)
Current Directory/home/avi/desktop

2. Write a shell script, add two numbers, and output an error message and a line of instructions if there are no input parameters

Answer: Here is a simple shell script and description, and if there are no command-line arguments, it throws errors and instructions on how to use the script.

Create another file named Twonumbers.sh and the following to add to the file.

Copy Code code as follows:

#!/bin/bash
# The Shebang

If [$#-ne 2]
# If Two inputs are not received from Standard Input

Then
# then execute the below statements

echo "Usage-$ x y"
# Print on standard output, how-to the script (Usage-./1.sh x Y)

echo "Where x and Y are two nos for which I'll print sum"
# Print on standard output, ' Where x and Y are two nos for which I'll print sum '

Exit 1
# Leave Shell in Error Stage and before the task is successfully carried out.

Fi
# End of the If Statement.

echo "Sum of" and $ is ' expr $ + $ '
# If The above condition is false and user entered two numbers as a command line Argument,
It'll show the sum of the entered numbers.

Add executable permissions to him, and execute.

Copy Code code as follows:
# chmod 755 two-numbers.sh

Case one: No two digits are entered as command line parameters to run the script, you will get the following output.

Sample output

Copy Code code as follows:
#./two-numbers.sh

Usage-/two-numbers.sh x y
Where x and Y are two nos for which I'll print sum

Scenario Two: When a number exists, you get the result as shown in the figure.
Copy Code code as follows:

$./two-numbers.sh 4 5

Sum of 4 and 5 is 9

As a result, the above shell script meets the requirements of the problem.

3. You need to print a reverse sequence of a given number, such as input 10572, output 27501, and if you do not enter data, you should throw an error and use a script description. Before that, tell me the algorithm you need to use here.

Algorithm

1. The number entered is n
2. Assign value rev=0, sd=0 (reverse and single digit set to 0)
3.N% 10, will get the leftmost number
4. Reverse digits can be generated in this way rev * + SD
5. The right displacement operation of the input number (divided by 10)
6. If n > 0, enter the third step, otherwise proceed to step seventh
7. Output Rev

Now, create a file called ' numbers.sh ' and add the following code.

Copy Code code as follows:

#!/bin/bash
If [$#-ne 1]
Then
echo "Usage: $ number"
echo "I'll find reverse of given number"
echo "for eg. $0123, I'll print 3210 "
Exit 1
Fi

N=$1
Rev=0
Sd=0

While [$n-GT 0]
Todo
sd= ' expr $n% 10 '
rev= ' expr $rev \* + $sd '
n= ' expr $n/10 '
Done
echo "Reverse number is $rev"

Grant execute permissions on the file, and run the script shown below.

Copy Code code as follows:
# chmod 755 Numbers.h

Case one: When the input does not contain command-line arguments, you will get the following output.

Sample output

Copy Code code as follows:
./numbers.sh

Usage:./numbers.sh number
I'll find reverse of given number
For eg. ./2.sh 123, I'll print 321

Scenario Two: normal input

Copy Code code as follows:

$/numbers.sh 10572

Reverse number is 27501

The script above is perfect and the output is exactly what we need.

4. You should use the terminal directly, instead of relying on any shell script to do real computation. What would you do (like real 7.56+2.453)?
Answer: We need to use the BC command in the special way described below. Enter the 7.56+2.453 as input through the pipeline into the BC.

Copy Code code as follows:

$ echo 7.56 + 2.453 | Bc

10.013

5. You need to give the value of pi, precision of 100 digits after the decimal point, what is the simplest method.

Answer: Find the value of pi the simplest way, we just need to issue the following command.

Copy Code code as follows:

# PI 100

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067

It's obvious! Install we must have bag pi. With just one apt or yum command, you can get the package you need and use the simplest way to achieve that requirement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.