Linux复习(四)Shell编程2
来源:互联网 发布:手机网络制式有几种 编辑:程序博客网 时间:2024/06/07 03:35
shell的语法
一、test命令
test expression 或 [ expression ]
1. 关于某个文件名的『类型』侦测(存在与否),如 test -e filename
-e file 如果文件存在,则结果为真,历史上-e选项是不可移植的,所以通常使用的是 -f 选项
-f file 如果是文件是一个普通文件(file),则结果为真。
-d file 如果文件是一个目录(directory),则结果为真
-b 该『文件名』是否为一个 block device 装置?
-c 该『文件名』是否为一个 character device 装置?
-S 该『文件名』是否为一个 Socket 文件?
-p 该『文件名』是否为一个 FIFO (pipe) 文件?
-r 侦测该文件名是否具有『可读』的属性?
-w 侦测该文件名是否具有『可写』的属性?
-x 侦测该文件名是否具有『可执行』的属性?
-u 侦测该文件名是否具有『SUID』的属性?
-g 侦测该文件名是否具有『SGID』的属性?
-k 侦测该文件名是否具有『Sticky bit』的属性?
-s 侦测该文件名是否为『非空白文件』?
-w 侦测该文件名是否具有『可写』的属性?
-x 侦测该文件名是否具有『可执行』的属性?
-u 侦测该文件名是否具有『SUID』的属性?
-g 侦测该文件名是否具有『SGID』的属性?
-k 侦测该文件名是否具有『Sticky bit』的属性?
-s 侦测该文件名是否为『非空白文件』?
2. 两个文件之间的比较,如: test file1 -nt file2
-nt (newer than)判断 file1 是否比 file2 新
-ot (older than)判断 file1 是否比 file2 旧
-ef 判断 file2 与 file2 是否为同一文件,可用在判断 hard link 的判定上。
-nt (newer than)判断 file1 是否比 file2 新
-ot (older than)判断 file1 是否比 file2 旧
-ef 判断 file2 与 file2 是否为同一文件,可用在判断 hard link 的判定上。
3. 关于两个整数之间的判定,例如 test n1 -eq n2
-eq 两数值相等 (equal)
-ne 两数值不等 (not equal)
-gt n1 大于 n2 (greater than)
-lt n1 小于 n2 (less than)
-ge n1 大于等于 n2 (greater than or equal)
-le n1 小于等于 n2 (less than or equal)
-eq 两数值相等 (equal)
-ne 两数值不等 (not equal)
-gt n1 大于 n2 (greater than)
-lt n1 小于 n2 (less than)
-ge n1 大于等于 n2 (greater than or equal)
-le n1 小于等于 n2 (less than or equal)
4. 判定字符串的数据
test -z string 判定字符串是否为空 ?若 string 为空字符串,则为 true
test -n string 判定字符串是否非空?若 string 为空字符串,则为 false。
注: -n 亦可省略
test str1 = str2 判定 str1 是否等于 str2 ,若相等,则回传 true
test str1 != str2 判定 str1 是否不等于 str2 ,若相等,则回传 false
test -z string 判定字符串是否为空 ?若 string 为空字符串,则为 true
test -n string 判定字符串是否非空?若 string 为空字符串,则为 false。
注: -n 亦可省略
test str1 = str2 判定 str1 是否等于 str2 ,若相等,则回传 true
test str1 != str2 判定 str1 是否不等于 str2 ,若相等,则回传 false
5. 多重条件判定,例如: test -r filename -a -x filename
-a (and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才回传 true。
-o (or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或 x 权限时,就可回传 true。
! 反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true
-a (and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才回传 true。
-o (or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或 x 权限时,就可回传 true。
! 反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true
二、各种语法
1.条件语句
1)if/else
2)case
2.重复语句
1)for
形式:for var in list
do
statements
done
适用于对一系列字符串循环处理
Example :
#!/bin/sh
for file in $(ls f*.sh); do
lpr $file
done
exit 0
#!/bin/sh
for file in $(ls f*.sh); do
lpr $file
done
exit 0
2)while
形式
while condition
do
statements
done
while condition
do
statements
done
Example :
quit=n
while [ “$quit”!= “y”]; do
read menu_choice
case “$menu_choice”in
a) do_something;;
b) do_anotherthing;;
…
q|Q) quit=y;;
*) echo “Sorry, choice not recognized.”;;
esac
done
quit=n
while [ “$quit”!= “y”]; do
read menu_choice
case “$menu_choice”in
a) do_something;;
b) do_anotherthing;;
…
q|Q) quit=y;;
*) echo “Sorry, choice not recognized.”;;
esac
done
Example 2:
a=0
while [ "$a" -le "$LIMIT" ]
do
a=$(($a+1))
if [ "$a" -gt2 ]
then
break # Skip entire rest of loop.
fi
echo -n "$a ”
done
while [ "$a" -le "$LIMIT" ]
do
a=$(($a+1))
if [ "$a" -gt2 ]
then
break # Skip entire rest of loop.
fi
echo -n "$a ”
done
3)until
形式
until condition
do
statements
done
until condition
do
statements
done
Not recommended (while statement is
preferred)
preferred)
4)select
形式
select item in itemlist
do
statements
done
作用: 生成菜单列表
select item in itemlist
do
statements
done
作用: 生成菜单列表
例(一个简单的菜单选择程序)
#!/bin/sh
clear
select item in Continue Finish
do
case “$item”in
Continue) ;;
Finish) break ;;
*) echo “Wrong choice! Please select again!”;;
esac
done
#!/bin/sh
clear
select item in Continue Finish
do
case “$item”in
Continue) ;;
Finish) break ;;
*) echo “Wrong choice! Please select again!”;;
esac
done
Example :
0 0
- Linux复习(四)Shell编程2
- Linux复习(三)Shell编程1
- linux shell编程(四)
- linux shell 编程(四)
- LINUX期末复习---SHELL编程
- Linux程序设计(Linux shell编程四)
- Linux系统shell脚本编程(四)
- Linux Shell编程(四)--Shell控制结构
- Linux的shell编程(四)
- Linux Shell编程四:高级shell脚本
- linux shell 复习(一)
- linux高级编程四shell编程/指针
- Linux脚本(shell)编程(四) 判断条件
- Linux脚本(shell)编程(四) 判断条件
- 【Linux 系统编程】shell 输入/输出重定向(四)
- Linux shell编程学习笔记(四) 位置变量
- shell编程复习 第一次
- shell编程复习 第二次
- C#连接数据库
- json_xml
- C++:STL标准入门汇总(http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html)
- Video Segmentation by Non-Local Consensus Voting 读书笔记
- 使用Grid++report开发报表功能
- Linux复习(四)Shell编程2
- HDU 2579 Dating with girls(2)
- 测试了std::vector的交集求解,并对比里std
- cocos2d-x 命令摘要
- Factorial Trailing Zeroes N!末尾0的个数
- C++ 动态存储空间的分配和释放 new与malloc的区别
- Android 5.0学习<一>
- 特殊的树---二叉树
- 解题报告 之 UVA11400 Lighting System Design