您的位置:首页 > 运维架构 > Shell

linux shell编程五步拳(张迅雷闪击shell系列) 第二集 shell编程基本语法快速入门

2012-08-03 14:57 676 查看
变量测试语句:用于测试变量是否相等、是否为空、文件类型等

格式 test 测试条件

test str1=str2

test str1!=str2

test str1 是否为空

test -d file 指定文件是否目录

test -f file 指定文件是否常规文件

test -x file 指定文件是否可执行

test -r file 指定文件是否可读

test -w file 指定文件是否可写

test -s file 指定文件大小是否非0

test -a file 指定文件是否存在

整数的测试:

test int1 -eq int2 测试整数是否相等

test int1 -ge int2 测试int1是否>=int2

test int1 -gt int2 测试int1是否>int2

test int1 -le int2 测试int1是否<=int2

test int1 -lt int2 测试int1是否<int2

test int1 -ne int2 测试整数是否不相等

if test -d $1 then

fi

变量测试语句可用[ ] 进行简化

if[ "$web"="" ]

then

else

fi

if语句的流程图

if

then

fi

______________________________________

if

then

else

fi

______________________________________

if

then

eif

then

eif

then

else

fi

______________________________________

多个条件的联合

-a 逻辑与

-o 逻辑或

循环———————————————————————

for 变量 in 名字表

do

命令列表

done

for DAY in sunday Monday Tuesday

do

echo "THE day is:$DAY"

done

awk -F 域分符‘命令’ 不指定就是空格为风格符号,制定冒号做分割符就直接后面加冒号。

1 检测系统中UID为0的用户

awk -F:'$3==0{print $1}'/etc/passwd

2 检测系统中密码为空的用户

awk -F:'length($2)==0 {print $1}'/etc/shadow

exit 1 错误退出

grep ^root:x /etc/passwd 精确查找

list /etc/passwd info

who

sh killluser.sh test

cat killuser.sh

#!/bin/sh

#

username="$1" 这个地方是位置变量

/bin/ps aux | /bin/grep $username | /bin/awk '{print $2}' >/tmp/temp.pid $2是pid

killid='cat /tmp/temp.pid'

do

/bin/kill -9 $PID 2> /dev/null

done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: