Linux之shell脚本
Shell就是一个命令行解释器,它的作用就是遵循一定的语法将输入的命令加以解释并传给系统。打开文本编辑器(可以使用vi/vim命令来创建文件),新建一个文件test.sh,扩展名为sh(sh代表shell)。
一、算术运算符
+ 加法
- 减法
* 乘法
/ 除法
% 取余
= 赋值
== 相等
!= 不相等
二、关系运算符
-eq 检测两个数是否相等,相等返回 true。
-ne 检测两个数是否不相等,不相等返回 true。
-gt 检测左边的数是否大于右边的,如果是,则返回 true。
-lt 检测左边的数是否小于右边的,如果是,则返回 true。
-ge 检测左边的数是否大于等于右边的,如果是,则返回 true。
-le 检测左边的数是否小于等于右边的,如果是,则返回 true。
三、字符串运算符
= 检测两个字符串是否相等,相等返回 true。
!= 检测两个字符串是否相等,不相等返回 true。
-z 检测字符串长度是否为0,为0返回 true。
-n 检测字符串长度是否为0,不为0返回 true。
str 检测字符串是否为空,不为空返回 true。
四、文件测试运算符
-b file 检测文件是否是块设备文件,如果是,则返回 true。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。
-d file 检测文件是否是目录,如果是,则返回 true。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。
-p file 检测文件是否是有名管道,如果是,则返回 true。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。
-r file 检测文件是否可读,如果是,则返回 true。
-w file 检测文件是否可写,如果是,则返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。
五、实例
1.模拟linnux登录shell
/bin/bash
echo -n “login:” #-n 字符串长度不为零
read name
echo -n “password:”
read passwd
if [
name="cht"−a
n
a
m
e
="
c
h
t
"
−
a
passwd = “abc” ];then #-a 两个表达式都为true,返回true
echo “the host and password is right!”
else echo “input is error!”
fi
2.比较两个数大小
/bin/bash
echo “please enter two number”
read a #输入的参数
read b
if test
a−eq
a
−
e
q
b #eq表示a和b数相同就返回true
then echo “NO.1 = NO.2”
elif test
a−gt
a
−
g
t
b #gt表示a和b大小相同返回true
then echo “NO.1 > NO.2”
else echo “NO.1 < NO.2”
fi
3.查找/root/目录下是否存在该文件
/bin/bash
echo “enter a file name:”
read a
if test -e /root/$a #e表示文件或目录存在,返回true
then echo “the file is exist!”
else echo “the file is not exist!”
fi
4.for循环的使用
/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo “$num”
done
5.查看用户是否运行
/bin/bash
echo “Please enter a user:”
read a
b=
(whoami)iftest
(
w
h
o
a
m
i
)
i
f
t
e
s
t
a = $b
then echo “the user is running.”
else echo “the user is not running.”
fi
6.删除当前目录下大小为0的文件
/bin/bash
for filename in ls
do
if test -d filename #d检测是否是块设备文件
then b=0
else
a=
filename #d检测是否是块设备文件 then b=0 else a=
(ls -l
filename|awk‘print$5′)iftest
f
i
l
e
n
a
m
e
|
a
w
k
‘
p
r
i
n
t
$
5
′
)
i
f
t
e
s
t
a -eq 0
then rm $filename
fi
fi
done
7.如果/export/um_lpp_source下有文件,那么将其文件系统大小改为3G
/bin/bash
while line=ls /export/um_lpp_source
do
if test
line=””thenecho“NULL”sleep1elseecho
l
i
n
e
=
”
”
t
h
e
n
e
c
h
o
“
N
U
L
L
”
s
l
e
e
p
1
e
l
s
e
e
c
h
o
line
chfs -a size=3G /export/um_lpp_source #a与运算
exit 0
fi
done
8.测试IP地址
/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
echo “the number of
icomputeris”ping−c1192.168.0.
i
c
o
m
p
u
t
e
r
i
s
”
p
i
n
g
−
c
1
192.168.0.
i #c字符设备文件
done
9.如果test.log的大小大于0,那么将/opt目录下的*.tar.gz文件
/bin/sh
a=2
while name=”test.log”
do
sleep 1
b=
(ls−l
(
l
s
−
l
name | awk ‘{print 5}’) #awk是一种编程语言,用于在linux/unix下对文本和数据进行处理它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。
if test
5}’) #awk是一种编程语言,用于在linux/unix下对文本和数据进行处理它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。 if test
b -ge $a
#then echo “OK”
then cp /opt/*.tar.gz .
exit 0
fi
done
10.打印读取的内容,为下面的例子做准备
/bin/bash
while read name
do
echo $name
done
11.从0.sh中读取内容并打印
/bin/bash
while read line
do
echo $line
done < 0.sh
12.读取a.c中的内容并做加1运算
/bin/bash
test -e a.c
while read line
do
a=
((
(
(
line+1))
done < a.c
echo $a
13.普通无参数函数
/bin/bash
p ()
{
echo “hello”
}
p
14.给函数传递参数
/bin/bash
p_num ()
{
num=
1echo
1
e
c
h
o
num
}
for n in
@dopnum
@
d
o
p
n
u
m
n
done
15.创建文件夹
/bin/bash
while :
do
echo “please input file’s name:”
read a
if test -e /root/
athenecho“thefileisexistingPleaseinputnewfilename:”elsemkdir
a
t
h
e
n
e
c
h
o
“
t
h
e
f
i
l
e
i
s
e
x
i
s
t
i
n
g
P
l
e
a
s
e
i
n
p
u
t
n
e
w
f
i
l
e
n
a
m
e
:
”
e
l
s
e
m
k
d
i
r
a
echo “you aye sussesful!”
break
fi
done
16.获取本机IP地址
/bin/bash
ifconfig | grep “inet addr:” | awk ‘{ print $2 }’| sed ‘s/addr://g’ #sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用。
17.查找最大文件
/bin/bash
a=0
for name in .
do
b=
(ls−l
(
l
s
−
l
name | awk ‘{print 5}’)
if test
5}’) if test
b -ge
athena=
a
t
h
e
n
a
=
b
namemax=
namefidoneecho“themaxfileis
n
a
m
e
f
i
d
o
n
e
e
c
h
o
“
t
h
e
m
a
x
f
i
l
e
i
s
namemax”
18.查找当前网段内IP用户,重定向到ip.txt文件中
/bin/bash
a=1
while :
do
a=
((
(
(
a+1))
if test
a−gt255thenbreakelseecho
a
−
g
t
255
t
h
e
n
b
r
e
a
k
e
l
s
e
e
c
h
o
(ping -c 1 192.168.0.
a|grep“ttl”|awk‘print$4′|sed‘s/://g′)ip=
a
|
g
r
e
p
“
t
t
l
”
|
a
w
k
‘
p
r
i
n
t
$
4
′
|
s
e
d
‘
s
/
:
/
/
g
′
)
i
p
=
(ping -c 1 192.168.0.
a|grep“ttl”|awk‘print$4′|sed‘s/://g′)echo
a
|
g
r
e
p
“
t
t
l
”
|
a
w
k
‘
p
r
i
n
t
$
4
′
|
s
e
d
‘
s
/
:
/
/
g
′
)
e
c
h
o
ip >> ip.txt
fi
done
19.打印当前用户
/bin/bash
echo “Current User is :”
echo
(ps|grep"
(
p
s
|
g
r
e
p
"
" | awk '{print
" | awk '{print
2}’)
20.case语句练习
!/bin/bash
clear
echo “enter a number from 1 to 5:”
read num
case $num in
1) echo “you enter 1”
;;
2) echo “you enter 2”
;;
3) echo “you enter 3”
;;
4) echo “you enter 4”
;;
5) echo “you enter 5”
;;
*) echo “error”
;;
esac
21.yes/no返回不同的结构
!/bin/bash
clear
echo “enter [y/n]:”
read a
case
ainy|Y|Yes|YES)echo“youenter
a
i
n
y
|
Y
|
Y
e
s
|
Y
E
S
)
e
c
h
o
“
y
o
u
e
n
t
e
r
a”
;;
n|N|NO|no) echo “you enter $a”
;;
*) echo “error”
;;
esac
22.内置命令的使用
/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date id `date`"
echo
echo "the user is :"
who
echo
echo "this is `uname -s`"
echo
echo "that's all folks! "
23.打印无密码用户
/bin/bash
echo “No Password User are :”
echo (cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print
(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print
1}’)
24.查看用户之间的关系
/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date id `date`"
echo
echo "the user is :"
who
echo
echo "this is `uname -s`"
echo
echo "that's all folks! "
25.检查端口号是否已启动
!/bin/bash
n=1
echo “检查xxx服务…”
while true
do
if test $n -gt 20
then
echo “xxx服务启动失败”
break
fi
sleep 5
n=$(($n+1))
port=`netstat -antp | grep "0.0.0.0:8080"`
if [ ${#port} -gt 3 ]; then
echo "xxx服务已经启动"
break;
fi
done
参考文献:
http://www.runoob.com/linux/linux-shell-basic-operators.html