SHELL编程(一) 基础语法
一、基本语法1.1 入门例子新建一个example01.sh,写入以下内容: vim example01.sh
#!/bin/bash
#This is to show what a example looks like
echo "This is our first example"
echo #This inserts an empty line in output
echo "We are currently in the following directory"
pwd
echo
echo "This directory contains the following files"
ls -lh
第一行: 加权限: chmod +x example01.sh
执行方式一: ./example01.sh
执行方式二: /opt/bee/shtest/example01.sh
执行方式三: sh example01.sh
1.2 shell变量1.2.1 永久变量永久变量:永久变量是环境变量,其值不随shell脚本的执行结束而消失。 echo $JAVA_HOME
echo $PATH
1.2.2 临时变量临时变量:shell程序内定义,其范围仅限于定义它的程序,对其它程序不可见。 临时变量的注意事项: 1.2.3单引号和双引号单引号和双引号的区别: 举例 DAY=mon
A='Today is $DAY'
B="Today is $DAY"
echo $A
echo $B
输出结果: Today is $DAY
Today is mon
1.2.4 变量管理查看所有变量: SET
查看某一个变量 DAY="Mon"
set | grep DAY
删除变量: unset DAY
1.2.5 位置变量和特殊变量ls -lh
ls是命令名,-lh是位置参数。 ./example01.sh file1 file2 file3
n 是这个程序的第n个参数值 特殊变量是一开始执行Script脚本时就会的设定,不能被修改。
# 这个程序的参数个数 #!/bin/bash
echo "$* 表示这个程序的所有参数"
echo "$# 表示这个程序的参数个数"
touch /tmp/a.txt
echo "$$ 表示程序的进程ID"
touch /tmp/b.txt &
echo "$! 执行上一个后台指令的PID"
echo "$$ 表示程序的进程ID"
输出: [root@iz2zeisjfk2rw89yhp3g19z shtest]# sh example02.sh a b c
a b c 表示这个程序的所有参数
3 表示这个程序的参数个数
2755 表示程序的进程ID
2757 执行上一个后台指令的PID
2755 表示程序的进程ID
1.3 read命令作用:read命令可以读参数 #!/bin/bash
echo "input three parameters:"
read first second third
echo "the first parameter is $first"
echo "the second parameter is $second"
echo "the third parameter is $third"
1.4 expr命令作用:shell变量的算术运算 expr 3 + 5 #算术运算符中要有空格
var1=10
var2=6
expr $var1 - 3
expr $var1 + $var2
expr $var1 / 2
expr $var1 * $var2
expr `expr 5 + 15` * `expr 2 + 3`
expr `expr 5 + 15` / `expr 2 + 3`
example04.sh #!/bin/sh
A=10
B=20
C=30
value1=`expr $A + $B + $C`
echo "value1 : $value1"
value2=`expr $C / $B `
echo "value2 : $value2"
value3=`expr $A * $B `
echo "value3 : $value3"
1.5 变量测试语句测试整数: test int1 -eq int2
test int1 -ge int2
test int1 -gt int2
test int1 -le int2
test int1 -lt int2
test int1 -ne int2
文件测试: test -d file #测试是否为目录
test -f file #测试是否为文件
test -r file #测试文件是否可读
test -w file #测试文件是否可写
test -x file #测试文件是否可执行
test -e file #测试文件是否存在
test -s file #测试大小是否为空
1.6 if判断语句流程控制语句 if test[]
then
语句
fi
#;表示两个命令写在一行,互不影响
例子: #!/bin/bash
echo "input a file name: "
read file_name
if [ -d $file_name ]
then
echo "$file_name is a dir"
elif [ -f $file_name ] ;then
echo "$file_name is a file"
elif [ -c $file_name -o -b $file_name ]; then
echo "$file_name is d device file"
else
echo "$file_name is an unknow file"
fi
1.7 case流程控制#!/bin/bash
echo "*************************"
echo "Please select your operation:"
echo "1 Copy"
echo "2 Delete"
echo "3 Backup"
echo "Q Quit"
read op
case $op in
1)
echo "your selection is Copy"
;;
2)
echo "your selection is Delete"
;;
3)
echo "your selection is Backup"
;;
Q)
echo "Quit"
;;
*)
echo "invalid selection"
esac
1.8 双小括号的用法#!/bin/sh
var1=1
while((var1<100))
do
echo "$var1"
((var1=var1*2))
done
1.9 循环语句嵌套#!/bin/sh
echo "please input line number and shape: "
read N S
i=0
N=$N
for((i=1; i<=N; i++))
do
j=1
for((j=1; j<=$i; j++))
do
echo -n "$S" #-n 起到换行的作用
done
echo
done
打印倒三角 please input line number and shape:
5 &
&
&&
&&&
&&&&
&&&&&
1.10 break和continue#!/bin/bash
# break continue
# break 跳出整个循环
# continue 跳出本次循环,进行下次循环
#############################################
#
# 打印一个菜单功能,实现输错后,可以重新输入
# 只有输入Q,才可以退出菜单
#
#############################################
while true
do
echo "***************************************"
echo "Please select your operation: "
echo "1 Copy"
echo "2 Delete"
echo "3 Backup"
echo "4 Quit"
echo "***************************************"
read op
case $op in
C)
echo "your selecttion is Copy"
;;
D)
echo "your selecttion is Delete"
;;
B)
echo "your selecttion is Backup"
;;
Q)
echo "your selecttion is Quit"
break
;;
*)
echo "invalide selection,please try again"
continue
;;
esac
done
1.11 shift命令#!/bin/bash
echo "####################################"
echo "# #"
echo "#做一个加法计算器,求出所有参数的和#"
echo "# #"
echo "####################################"
echo "please input your nums:"
if [ $# -le 0 ]
then
echo "err!:Not enough parameters"
exit 124
fi
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo "sum:$sum"
二、shell函数2.1 函数定义语法: 函数名()
{ shell 命令序列 }
或者 function 函数名()
{ shell 命令序列 }
注: 函数调用时不需要带() 例子: #!/bin/bash
abc=123
echo $abc
function example()
{
abc=456
}
example
echo "$abc"
2.2 函数参数传递#!/bin/bash
function example()
{
echo $1
echo $2
}
example 3 4
2.3 正则表达式1.字符串查找 grep root passwd
2.字符串开头 grep ^root passwd
3.字符串结尾 grep bash$ passwd
4.特殊字符 grep -n --color ' passwd
5.*表示重复0到无穷个前一个字符 搜索包括sp,o重复2次以上的行。 grep spoo* passwd
6.[list]:字符集合。列出想要选择的字符。 grep g[ao] passwd -n
搜索不以#开头的行: grep ^[^#] passwd
搜索包含一定范围数字的行: grep [3-8] passwd
以小写字母开头的行: grep ^[a-z] passwd
搜索不以英文字母开头的行: grep ^[^a-zA-Z] passwd
显示空行及行号: grep ^$ passwd -n
.代表任意字符,*代表重复前一个字符一到无穷次。 例如,搜索包含以r开头、t结尾,长度为4个字符的行: grep r..t passwd -n
搜索包含2个o的行: grep ooo* passwd --color
搜索包含以g开头、g结尾,中间可有可无的字符串的行: grep g.*g passwd (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |