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

Shell 基本语法

2013-06-04 15:38 267 查看
开机自启动的方法:

1:/etc/rc.d/rc.local 加入项目
2:chkconfig item on

$# 脚本的参数个数

$* 以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过9个

$0 脚本名称

$1..$9 第N个参数

1、算术运算比较运算符(bash自身不能比较浮点数)

-eq 等于 [ $num1 -eq $num2 ]

-ne 不等于 [ 100 -ne $num1 ]

-lt 小于 [ 100 -lt `expr $num1 + $num2` ]

-le 小于或等于 [ 100 -le `expr $num1 \* $num2` ]

-gt 大于 [ 100 -gt `expr $num1 / $num2` ]

-ge 大于或等于 [ 100 -ge `expr $num1 % $num2` ]

2、字符串比较运算符

-z string 如果 string 长度为零,则为真 [ -z "`ps aux | grep mysql`" ]

-n string 如果 string 长度非零,则为真 [ -n "$string" ]

【注意】 $string 一定要放在双引号里面 "$string",否则使用 -n -z 的结果都为真!

string1 != string2 如果 string1 与 string2 不同,则为真 [ "$str1" != "Snail" ]

string1 == string2 如果 string1 与 string2 相同,则为真 [ "$str1" == "$str2" ]

(上面用一个 = 也可以,在严格的 POSIX 兼容下使用)

string1 string2 如果 string1 按字典顺序比较大于 string2,则为真

3、文件比较运算符

-a filename 如果 filename 存在,则为真 [ -e $HOME/.bashrc ]

-e filename (同上)

shell 与或

我想在[]里做一个判断,如满足a和b才执行,那么中间的符号是什么?

-------------------------------------------------------

if [ $xxx=a -a $xx=b ]

a means and

我想在[]里做一个判断,如满足a或b才执行,那么中间的符号是什么?

-------------------------------------------------------

if [ $xxx=a -o $xx=b ]

o means or

或者 ---

我想在[]里做一个判断,如满足a和b才执行,那么中间的符号是什么?

-------------------------------------------------------

if [ $xxx=a ] && [ $xx=b ]

&& means and

我想在[]里做一个判断,如满足a或b才执行,那么中间的符号是什么?

-------------------------------------------------------

if [ $xxx=a ] || [ $xx=b ]

|| means or

read 作用

的最简单形式::

#!/bin/bash

echo -n "Enter your name:" //参数-n的作用是不换行,echo默认是换行

read name //从键盘输入

echo "hello $name,welcome to my program" //显示信息

exit 0 //退出shell程序。

//********************************

由于read命令提供了-p参数,允许在read命令行中直接指定一个提示。

所以上面的脚本可以简写成下面的脚本::

#!/bin/bash

read -p "Enter your name:" name

echo "hello $name, welcome to my program"

exit 0

1)判断表达式

if test (表达式为真)

if test !表达式为假

test 表达式1 –a 表达式2 两个表达式都为真

test 表达式1 –o 表达式2 两个表达式有一个为真

2)判断字符串

test –n 字符串 字符串的长度非零

test –z 字符串 字符串的长度为零

test 字符串1=字符串2 字符串相等

test 字符串1!=字符串2 字符串不等

3)判断整数

test 整数1 –eq 整数2 整数相等

test 整数1 –ge 整数2 整数1大于等于整数2

test 整数1 –gt 整数2 整数1大于整数2

test 整数1 –le 整数2 整数1小于等于整数2

test 整数1 –lt 整数2 整数1小于整数2

test 整数1 –ne 整数2 整数1不等于整数2

4)判断文件

test File1 –ef File2 两个文件具有同样的设备号和i结点号

test File1 –nt File2 文件1比文件2 新

test File1 –ot File2 文件1比文件2 旧

test –b File 文件存在并且是块设备文件

test –c File 文件存在并且是字符设备文件

test –d File 文件存在并且是目录

test –e File 文件存在

test –f File 文件存在并且是正规文件

test –g File 文件存在并且是设置了组ID

test –G File 文件存在并且属于有效组ID

test –h File 文件存在并且是一个符号链接(同-L)

test –k File 文件存在并且设置了sticky位

test –b File 文件存在并且是块设备文件

test –L File 文件存在并且是一个符号链接(同-h)

test –o File 文件存在并且属于有效用户ID

test –p File 文件存在并且是一个命名管道

test –r File 文件存在并且可读

test –s File 文件存在并且是一个套接字

test –t FD 文件描述符是在一个终端打开的

test –u File 文件存在并且设置了它的set-user-id位

test –w File 文件存在并且可写

test –x File 文件存在并且可执行

for filename in `ls`
就是获取当前目录下的文件

ls -i

查看目录及文件的详细信息

awk:用于一行中分成数个“字段”来处理。适合处理 	小型数据。
运行模式:awk '条件类型1{动作1} 条件类型2{动作2} ...' filename

# last | awk '{print $1 "\t" $3}' <== 查看登录者的数据,只显示登录名和ip地址,并以[tab]隔开

awk 的内置变量
变量名称	代表的含义

NF	每一行($0)拥有的字段总数

NR	当前 awk 所处理的是 “第几行” 数据

FS	当前分隔符,默认空格键

awk 的逻辑运算符
运算单元	代表含义
>	大于
<	小于
>=	大于或等于
<=	小于或等于
==	等于
!=	不等于

范例:
cat /etc/passwd | awk '{FS=":"} $3 < 10 {print $1 "\t" $3}' <== 文件/etc/passwd是以":"分隔的,查看第三栏小于10的数据,并且只显示帐号与第三栏


6.删除当前目录下大小为0的文件

#/bin/bash

for filename in `ls` //符号是1边上哪个

do

if test -d $filename

then b=0

else

a=$(ls -l $filename | awk '{ print $5 }') //a边上不能有空格

if test $a -eq 0

then rm $filename

fi

fi

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