Shell编程入门(第二版)(中)

简介: 变量测试语句-test作用:用来测试变量是否相等,是否为空,文件类型等。格式: test 测试条件 或 [] #范围:整数,字符串,文件   1)整数测试:  test int...

变量测试语句-test

作用:用来测试变量是否相等,是否为空,文件类型等。

格式:

test 测试条件 或 [] #范围:整数,字符串,文件  

 

1)整数测试

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  测试整数是否不相等

 

2)字符串测试

test str1=str2  测试字符串是否相等 

test str1!=str2  测试字符串是否不相等 

test str1  测试字符串是否不为空 

test -n str1  测试字符串是否不为空 

test -z str1  测试字符串是否为空

 

3)文件测试

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

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

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

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

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

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

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

 

注:test测试语句一般不单独使用,一般作为if语句的测试条件,;

if test -d file
then
	....
fi


 

test的变量的简写形式”[]”

 

示例-apachtest.sh

#!/bin/bash
# A test shell script for test Apache is running or not

web=$(/usr/bin/pgrep httpd)

echo "Now let's test the Apache..."
echo

#if [ "$web" != "" ]
if [ -n "$web" ]
then
    echo "Apache is running..."
else
    echo "Apache is NOT running..."
    /etc/rc.d/init.d/httpd start
fi

流程控制语句

流控制语句:用于控制shell程序的流程 

exit语句:退出程序执行,并返回一个返回码,返回码为0表示正常退出,0表示非正常退出。 

例如:exit 0 

 

一、if

if/then格式

if test -d $1 
then 
		... 
fi 

示例-if_then.sh

#!/bin/bash
# A test shell script for if/then

if [ -x /etc/rc.d/init.d/httpd ]
then
    echo "Script: /etc/rc.d/init.d/httdp have x power!"
    /etc/rc.d/init.d/httpd restart
fi

if/else格式

	if 条件1 
	then 
		命令1 
	elif 条件2
	then 
		命令2 
	else 
		命令3 
	fi 

多个条件的联合

-a: 逻辑与,仅当两个条件都成立时,结果为真。 

-o: 逻辑或,两个条件只要有一个成立,结果为真。

 

示例-if_else.sh

#!/bin/bash
# A test shell script for if/elif/else

echo -n "Please input a filename: "
read filename

if [ -d $filename ]
then
    echo "$filename is a directory"
elif [ -f $filename ]
then
    echo "$filename is a commen file"
elif [ -c $filename -o -b $filename ]
then
    echo "$filename is a device file"
else
    echo "$filename is a unkown file"
fi

 

示例-if_elif_exit.sh

#!/bin/bash
# A test shell script for if/elif

if [ $# -ne 2 ] 
then
echo "Not enough parameters"
exit 1
fi

if [ $1 -gt $2 ]
then
    echo "$1 is great then $2"
elif [ $1 -lt $2 ]
then
    echo "$1 is little then $2"
else
    echo "$1 is equal as $2"
fi

二、for/in

for 变量 in 名字表 
do 
	命令列表 
done 

示例-for.sh

#!/bin/bash
# A test shell script for "for"

for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday
do
    echo "The day is $DAY"
done


awk命令[分段提取]

awk -F域分隔符 命令[单引号] #如果不用-F指定分割符,默认为空格

 

1、检测系统中UID0的用户 

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

#awk -F: '{print $1}' /etc/passwd

-F 指定分割附为:

$3 表示以:为分割附的第三位

 

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

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

#ps aux | grep -v root | awk '{print $2}'

示例-awk.sh

#!/bin/bash
# A test script for desplay users infomation

/bin/echo -n "Please input a username: "
read username

/bin/grep $username /etc/passwd > /dev/null 2> /dev/null

if [ $? -eq 0 ]
then
    /bin/echo "username is: $username"
else
    /bin/echo "user: $username is not exits."
    exit 1
fi
/bin/echo

# list /etc/passwd info
userinfo=`/bin/grep ^$username:x /etc/passwd`
uid=`echo $userinfo | awk -F: '{print $3}'`
gid=`echo $userinfo | awk -F: '{print $4'}`
dir=`echo $userinfo | awk -F: '{print $6}'`
shell=`echo $userinfo | awk -F: '{print $7}'`

# get /etc/group info
groupinfo=`/bin/grep x:$gid /etc/group`
gname=`/bin/echo $groupinfo | awk -F: '{print $1}'`

/bin/echo "user id is: $uid"
/bin/echo "default group is: $gname"
/bin/echo "home directory is: $dir"
/bin/echo "shell is: $shell"
/bin/echo "group member info:"

# get group members
groups=`/usr/bin/groups $username`
/bin/echo $groups
/bin/echo

# get online info
online=`/usr/bin/who | grep $username`
if [ -z "$online" ]
then
    echo "$username is not online"
else
    echo "$username is online..."
fi

 

实例-killuser.sh

#思路:将一个用户所有的进程包括shell都关闭,则相当于将该用户踢出了系统
#!/bin/bash
# A shell sript to kill a user in Linux

username=$1

killpid=`/bin/ps aux | grep $username | awk '{print $2}'`

for PID in $killpid
do
    /bin/kill -9 $PID 2> /dev/null
done

目录
相关文章
|
16天前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
32 12
|
16天前
|
Shell Linux
Shell 编程 编写hello word
Shell 编写hello word
38 5
|
30天前
|
Shell KVM 虚拟化
Shell 数组编程
【8月更文挑战第22天】 Shell 数组编程
39 10
|
26天前
|
分布式计算 资源调度 Hadoop
Hadoop入门基础(五):Hadoop 常用 Shell 命令一网打尽,提升你的大数据技能!
Hadoop入门基础(五):Hadoop 常用 Shell 命令一网打尽,提升你的大数据技能!
|
30天前
|
Java Shell Linux
【Linux入门技巧】新员工必看:用Shell脚本轻松解析应用服务日志
关于如何使用Shell脚本来解析Linux系统中的应用服务日志,提供了脚本实现的详细步骤和技巧,以及一些Shell编程的技能扩展。
24 0
【Linux入门技巧】新员工必看:用Shell脚本轻松解析应用服务日志
|
1月前
|
Shell 数据处理 C++
【震撼揭秘】Python正则VS Shell正则:一场跨越编程边界的史诗级对决!你绝不能错过的精彩较量,带你领略文本处理的极致魅力!
【8月更文挑战第19天】正则表达式是文本处理的强大工具,在Python与Shell中有广泛应用。两者虽语法各异,但仍共享许多基本元素,如`.`、`*`及`[]`等。Python通过`re`模块支持丰富的功能,如非捕获组及命名捕获组;而Shell则依赖`grep`、`sed`和`awk`等命令实现类似效果。尽管Python提供了更高级的特性和函数,Shell在处理文本文件方面仍有其独特优势。选择合适工具需根据具体需求和个人偏好决定。
26 1
|
17天前
|
存储 Ubuntu Shell
shell 用法入门
本文档详细介绍了Shell脚本的基础知识,包括基本写法、变量定义与使用、命令置换、环境变量、数组操作、算术运算、输入输出处理、控制语句及循环结构等内容。文档还提供了丰富的示例代码,帮助读者更好地理解和掌握Shell编程技巧。此外,还介绍了如何使用`if`语句进行条件判断、`case`语句进行模式匹配以及`while`、`for`循环等控制结构。最后,文档还涵盖了函数定义与调用的方法。适合初学者和有一定基础的开发者参考学习。
|
1月前
|
监控 Shell Linux
探索Linux操作系统下的Shell编程之魅力
【8月更文挑战第4天】本文旨在通过一系列精心设计的示例和分析,揭示在Linux环境下进行Shell编程的独特之处及其强大功能。我们将从基础语法入手,逐步深入到脚本的编写与执行,最终通过实际代码案例展现Shell编程在日常系统管理和自动化任务中的应用价值。文章不仅适合初学者构建扎实的基础,同时也为有一定经验的开发者提供进阶技巧。
38 11
|
2月前
|
JavaScript 前端开发 Shell
Shell 脚本编程保姆级教程(上)
Shell 脚本编程保姆级教程(上)
|
2月前
|
Shell Linux Perl
shell 编程中 awk ,wc ,$0,$1 等 命令的使用总结
shell 编程中 awk ,wc ,$0,$1 等 命令的使用总结
66 0