In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the practical shell script interview questions". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Write a shell script to get the current date, time, user name and current working directory.
Answer: the commands that output the user name, current date and time, and current working directory are logname,date,who I am and pwd.
Now, create a file called userstats.sh and add the following code to it.
The code is as follows:
#! / bin/bash
Echo "Hello, $LOGNAME"
Echo "Current date is `date`"
Echo "User is `who I am`"
Echo "Current directory `pwd`"
Add execute permission to it, and execute it.
The code is as follows:
# chmod 755 userstats.sh
#. / userstats.sh
Sample output
The code is as follows:
Hello, avi
Current date is Sat Jun 7 13:05:29 IST 2014
User is avi pts/0 2014-06-07 11:59 (: 0)
Current directory / home/avi/Desktop
two。 Write a shell script that adds two numbers and outputs an error message and a line of instructions if there are no input parameters.
Answer: here is a simple shell script and description that, if there are no command line arguments, throws an error and instructions on how to use the script.
Create a file called twonumbers.sh and add the following to the file.
The code is 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-$0 x y"
# print on standard output, how-to use the script (Usage -. / 1.sh x y)
Echo "Where x and y are two nos for which I will print sum"
# print on standard output, "Where x and y are two nos for which I will print sum"
Exit 1
# Leave shell in Error Stage and before the task was successfully carried out.
Fi
# End of the if Statement.
Echo "Sum of $1 and $2 is `expr $1 + $2`"
# If the above condition was false and user Entered two numbers as a command Line Argument
It will show the sum of the entered numbers.
Add executable permissions to him and execute them.
The code is as follows:
# chmod 755 two-numbers.sh
Scenario 1: run the script without entering two numbers as command line arguments, and you will get the following output.
Sample output
The code is as follows:
#. / two-numbers.sh
Usage -. / two-numbers.sh x y
Where x and y are two nos for which I will print sum
Case 2: when the numbers exist, you will get the results shown in the picture.
The code is as follows:
$. / two-numbers.sh 4 5
Sum of 4 and 5 is 9
Therefore, the above shell script meets the requirements of the problem.
3. You need to print the reverse order of a given number, such as input 10572 and output 27501. If there is no input data, you should throw an error and use a script description. Before that, tell me the algorithm you need to use here.
Arithmetic
1. The number entered is n
two。 Assign rev=0, sd=0 (reverse and single number set to 0)
3.n% 10, you will get the leftmost number
4. Reverse numbers can be used to generate rev * 10 + sd in this way.
5. Right shift of the input number (divided by 10)
6. If n > 0, proceed to step 3, otherwise proceed to step 7
7. Output rev
Now, create a file called `numbers.sh` and add the following code.
The code is as follows:
#! / bin/bash
If [$#-ne 1]
Then
Echo "Usage: $0 number"
Echo "I will find reverse of given number"
Echo "For eg. $0 0123, I will print 3210"
Exit 1
Fi
Nasty 1
Rev=0
Sd=0
While [$n-gt 0]
Do
Sd= `expr $n 10`
Rev= `expr $rev\ * 10 + $SD`
N = `expr $n / 10`
Done
Echo "Reverse number is $rev"
Grant execution permissions to the file and run the script as shown below.
The code is as follows:
# chmod 755 numbers.h
Case 1: when the input does not contain command line arguments, you will get the following output.
Sample output
The code is as follows:
. / numbers.sh
Usage:. / numbers.sh number
I will find reverse of given number
For eg. . / 2.sh 123, I will print 321
Case 2: normal input
The code is 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 for real calculations. What would you do (for example, the real number 7.56 to 2.453)?
Answer: we need to use the bc command in the special way described below. Enter 7.56 # 2.453 as input into the bc through the pipeline.
The code is as follows:
$echo 7.56 + 2.453 | bc
10.013
5. You need to give the value of pi with an accuracy of 100 decimal places, what is the easiest way.
Answer: the easiest way to find the value of pi, we just need to issue the following command.
The code is as follows:
# pi 100
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
It's obvious! To install we must have the package pi. You can get the software package you need with just an apt or yum command, and implement this requirement in the simplest way.
This is the end of the content of "what are the practical shell script interview questions"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.