Linux Shell 脚本面试实用问题及解答
我们在采访系列文章中得到了压倒性的反响,这是任何 Linux How-to 网站上的第一篇以点赞、评论反馈以及个人电子邮件地址形式出现的此类文章,使我们能够从一篇文章转到下一篇文章文章。
以下是已在 linux-console.net 上发布的访谈系列文章的链接,其中我们涵盖了许多主题,即 FTP、MySQL、Apache、脚本、Linux 命令等。
继续上面的系列,我们将带来另外 5 个精彩的 Linux 面试问题及其答案。为了使其成功,始终需要您(linux-console.net 的读者和常客)的支持。
1. 编写一个 shell 脚本来获取当前日期、时间、用户名和当前工作目录。
现在创建一个名为“userstats.sh”的文件,并向其中添加以下代码。
#!/bin/bash
echo "Hello, $LOGNAME"
echo "Current date is `date`"
echo "User is `who i am`"
echo "Current directory `pwd`"
设置执行权限并运行脚本,如下所示。
chmod 755 userstats.sh
./userstats.sh
样本输出
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
2. 编写一个 Shell 脚本,如果作为命令行参数提供,则将两个数字相加;如果未输入这两个数字,则会输出一条错误消息以及一行如何使用说明。
再次创建一个名为“two-numbers.sh”的文件,并向其中添加以下内容。
#!/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.
设置文件的执行者权限并运行脚本,如下所示。
chmod 755 two-numbers.sh
条件 1:运行脚本而不输入两个数字作为命令行参数,您将得到以下输出。
样本输出
./two-numbers.sh
Usage - ./two-numbers.sh x y
Where x and y are two nos for which I will print sum
条件 2:当数字作为命令行参数输入时,您将得到如图所示的结果。
./two-numbers.sh 4 5
Sum of 4 and 5 is 9
因此,上述 shell 脚本满足问题中建议的条件。
3. 您需要使用 Shell 脚本以相反的顺序打印给定的数字(例如 10572),以便仅使用命令行参数提供输入。如果输入数据没有作为命令行参数提供,它应该抛出错误并应该建议如何使用脚本。编写脚本,但在此之前请告诉我这里需要实现的算法。
算法
- 1. 令输入数=n
- 2.设置rev=0,sd=0(反转和个位数设置为0)
- 3. n % 10,将找到并给出最左边的一位数字
- 4. 反向数生成为 rev * 10 + sd
- 5. 将输入编号 (n) 减 1。
- 6. 如果 n > 0,则转到步骤 3,否则转到 setp 7
- 7. 打印版本
现在再次创建一个名为“numbers.sh”的文件并添加以下给定代码。
#!/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
n=$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"
授予该文件的执行权限并运行脚本,如下所示。
chmod 755 numbers.h
条件 1:当未提供输入作为命令行参数时,您将得到以下输出。
样本输出
./numbers.sh
Usage: ./numbers.sh number
I will find reverse of given number
For eg. ./2.sh 123, I will print 321
条件 2:当输入作为命令行参数提供时。
./numbers.sh 10572
Reverse number is 27501
上面的脚本完美运行,输出正是我们所需要的。
4. 您应该直接从终端而不是任何 shell 脚本进行实数计算。你会怎么做(假设实数是 7.56 和 2.453)?
例如,运行以下命令以使用 bc 命令实时计算数字,如图所示。
echo 7.56 + 2.453 | bc
10.013
5. 你需要找到小数点后 100 位的 pi 值,这是获得结果的最简单方法。
pi 100
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
明显地!我们必须安装包“pi”。只需执行 apt 或 yum 即可获取将 ‘pi’ 安装到您正在使用的发行版上所需的软件包。
目前为止就这样了。我很快就会再次在这里发表另一篇有趣的文章。在此之前,请继续关注并连接到 Howtoing.com。不要忘记在下面的评论部分向我们提供宝贵的反馈。