首页 编程技术从新手到系统管理员(三):深入探索Linux Bash脚本编程世界

从新手到系统管理员(三):深入探索Linux Bash脚本编程世界

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

本文由 [茶话汇] – [Qing] 编译自 [Avishek Kumar] 转载请注明出处

Shell-Scripting-Part-3

bash关键字

[code language=”bash”]
! esac select { }
c fi then [[
do for until ]]
done function while elif
if time else in
[/code]

与其它语言不同,bash允许将关键字用于变量的命名,虽然这样会降低脚本的可读性。为了保持脚本的可读性,程序员们应该避免这样子做。

命令在脚本中的执行方式是$(命令)。为了保证命令可以正确被执行,你或许应该包含命令的绝对路径,如$(/bin/date)

你可以通过“whereis”来查询一个命令的绝对路径,如 whereis date

[code language=”bash”]
[root@tecmint /]# whereis date
date: /bin/date /usr/share/man/man1/date.1.gz
[/code]

切换目录

这个脚本的作用是从当前目录向上切换N层。

[code language=”bash”]
#! /bin/bash
LEVEL=$1
for ((i = 1; i <= LEVEL; i++))
do
CDIR=../$CDIR
done
cd $CDIR
echo "You are in: "$PWD
exec /bin/bash
[/code]

将上面的代码保存成“up.sh”,并赋予可执行的权限(chmod 755 up.sh)。

./up.sh 2 从当前目录向上切换2层。
./up.sh 4 从当前目录向上切换4层。

输出结果:

[code language=”bash”]
[root@tecmint /]# chmod 755 up
[root@tecmint /]# ./up.sh 2
You are in: /

[root@tecmint /]# ./up.sh 4
You are in: /

[root@tecmint /]#
[/code]

你可以从这里下载这个例子的代码。

随机生成目录或文件

[code language=”bash”]
#! /bin/bash

echo "Hello $USER";
echo "$(uptime)" >> "$(date)".txt
echo "Your File is being saved to $(pwd)"
[/code]

这个脚本看起来简单,但其背后包含的东西并不简单。
echo:打印包含在其后的引号中的任何东西。
$:引导一个变量。
>>:重定向符号。

上面的脚本将update的输出结果保存到一个文件中,这个文件的名称是以当前的系统时间命名的。

输出结果:

[code language=”bash”]
[root@tecmint /]# ./randomfile.sh
Hello server
Your File is being saved to /home/server/Desktop
[/code]

你可以从这里下载这个例子的代码。

收集网络信息

这个脚本可以收集网络服务器的信息。因为这个脚本的行数比较多,这里就不列出来了。你可以从后面的链接中下载整个脚本。

注意:或许在运行这个脚本的时候,你需要安装lsb-core包。你可以使用apt(Ubuntu或者Debian)或者yum(Fedora)来安装。

输出结果:

[code language=”bash”]
[root@tecmint /]# ./collectnetworkinfo.sh

The Network Configuration Info Written To network.20-07-13.info.txt.
Please email this file to your_name@service_provider.com. ktop
[/code]

你可以更改脚本中的邮件,以便将结果发送到你的邮箱。

你可以从这里下载这个例子的代码。

将大写字母转换成小写字母

这个脚本可以将一个文件中的大写字母全部转换成小写,并保存到文件“small.txt”中。

[code language=”bash”]
#!/bin/bash

echo -n "Enter File Name : "
read fileName

if [ ! -f $fileName ]; then
echo "Filename $fileName does not exists"
exit 1
fi

tr ‘[A-Z]’ ‘[a-z]’ < $fileName >> small.txt
[/code]

输出结果:

[code language=”bash”]
[root@tecmint /]# ./convertlowercase.sh
Enter File Name : a.txt

Initial File:
A
B
C
D
E
F
G
H
I
J
K

[/code]

文件“small.txt”中包含的输出结果:

[code language=”bash”]
a
b
c
d
e
f
g
h
i
j
k

[/code]

你可以从这里下载这个例子的代码。

简易计算器

[code language=”bash”]
#! /bin/bash
clear
sum=0
i="y"

echo " Enter one no."
read n1
echo "Enter second no."
read n2
while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sum=`expr $n1 – $n2`
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue (y/n)) ?"
read i
if [ $i != "y" ]
then
exit
fi
done
[/code]

输出结果:

[code language=”bash”]
[root@tecmint /]# ./simplecalc.sh

Enter one no.
12
Enter second no.
14
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
1
Sum =26
Do u want to continue (y/n)) ?
y
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
3
mul = 14812
Do u want to continue (y/n)) ?
n
[/code]

你可以从这里下载这个例子的代码。

本文链接:https://www.yunweipai.com/3617.html

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部