linux基础3.12shell脚本2
来源:互联网 发布:淘宝买家如何开店 编辑:程序博客网 时间:2024/05/22 04:24
1.shell脚本参数
(1)使用 read -p 提示用户输入并将其直接存储到一个或多个变量(交互式输入):
[root@localhost mnt]# vim test.sh
#!/bin/bash
read -p "give me a world: " NUM
echo $NUM
[root@localhost mnt]# sh 1.sh
give me a world: hello
hello
(2)使用位置参数来读取传递给脚本的命令行参数或选项输入:
$1 ##传递给脚本的第一个参数
$2 ##传递给脚本的第二个参数
$3 ##传递给脚本的第三个参数
$* ##传递给脚本的所有参数
$# ##传递给脚本的参数个数a
$@ ##传给脚本的所有参数的列表
$0 ##脚本本身的名字
$$ ##脚本运行的当前进程ID号
$? ##显示最后命令的退出状态,0表示没有错误,其它表示有错误
[root@localhost mnt]# vim test.sh
#!/bin/bash
echo $1
echo $2
echo $3
echo $*
echo $#
echo $@
echo $0
echo $$
echo $?
[root@localhost mnt]# sh test.sh 1 2 3
1
2
3
1 2 3
3
1 2 3
test.sh
7137
0
2.文件状态运算符 [ -b|c|e|f|d|r|w|x|s|L /directory/file]
参数
-b block块设备
-c 字符设备文件
-e 存在
-f file文件
-d directory目录
-r 文件可读
-w 文件可写
-x 文件执行
-s 文件长度不为0
-L link链接
二进制文件运算符:-ef、-nt、-ot
在脚本中 [] 等价与 test 命令
[root@localhost mnt]# a=1 b=1
[root@localhost mnt]# [ "$a" = "$b" ] && echoyes
yes
[root@localhost mnt]# test "$a"="$b" && echoyes
yes
3.if语句
命令检查if后面的命令或列表的退出值。如果第一个命令评估为true/零,则运行then之后的命令列表,直至任一else。如果第一个命令评估为false/非零,则运行else与fi之间的命令列表(反向平写if,标记if块的结束)。
格式:
if
判断条件
then
条件成立的结果
else
条件不成立的结果
fi
4.case语句
case语句:它把变量的内容与多个模板进行匹配,再根据成功匹配的模板去决定应该执行哪部分代码。
[root@foundation4 mnt]# vim ceshi.sh
#!/bin/bash
case "$1" in
start)
rht-vmctl start $2
;;
view)
rht-vmctl view $2
;;
poweroff)
rht-vmctl poweroff $2
;;
*)
echo "I don'tknow"
;;
esac
[root@mail-qq mnt]# vim hello.sh
#!/bin/bash
CHECK () {
if
[ $1 $2 ]
then
echo $2 is a $3
else
echo $2 is not a $3
fi
}
case "$1" in
-f)
CHECK -f $2 file
;;
-b)
CHECK -b $2 block
;;
-d)
CHECK -d $2 directory
;;
*)
echo "ERROE:please input aargment and file after command"
esac
测试脚本:
[root@mail-qq mnt]# sh hello.sh -d /etc/passwd
/etc/passwd is not a directory
[root@mail-qq mnt]# passwd student <<end ##修改student的密码
> lee ##新密码
> lee ##确认新密码
> end
5.expect自动应答脚本
[root@mail-qq mnt]# yum install expect -y ##安装ecpect服务
[root@mail-qq mnt]# expect
[root@mail-qq mnt]# which expect
/usr/bin/expect
[root@mail-qq mnt]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
#!/usr/bin/expect ##脚本使用那一个shell执行。
set timeout 10 ##设置后面所有的expect命令的等待响应的超时时间,单位为秒。
send "westos\n" ##执行交互动作,相当于手工输入"westos"。
expect eof ##作用是在输出中搜索文件结束符,如果没有这一行,脚本会立即退出,得不到正确结果。
interact ##执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了,否则退出登录。
$argv 参数数组 ##expect脚本可以接受从bash传递过来的参数.可以使用[lindex$argv n]获得,n从0开始,分别表示第一个,第二个,第三个....参数。
(1)自动建立新分区脚本:
[root@mail-qq mnt]# vim fdisk.sh
#!/bin/bash
fdisk $1 <<end
n
+100M
wq
end
[root@mail-qq mnt]# sh fdisk.sh /dev/vdb
(2)自动应答
[root@mail-qq mnt]# vim ask.sh
#!/bin/bash
read -p "input your name: "
read -p "input your age: "
read -p "input your class: "
[root@mail-qq mnt]# chmod +x ask.sh
[root@mail-qq mnt]# vim answer.exp
#!/usr/bin/expect
spawn /mnt/ask.sh
expect {
"name: " {send"lee\r";exp_continue};
"age: " {send"18\r";exp_continue};
"class: " {send"linux\r"};
}
expect eof
测试:
[root@mail-qq mnt]# chmod +x answer.exp
[root@mail-qq mnt]# /mnt/answer.exp
[root@mail-qq mnt]# vim ssh.exp
#!/usr/bin/expect
spawn ssh root@172.25.254.104
expect {
"(yes/no)? " {send"yes\r";exp_continue};
"password: " {send"redhat\r"};
}
interact
测试:
[root@mail-qq mnt]# chmod +x ssh.exp
[root@mail-qq mnt]# /mnt/ssh.exp
6.环境变量
shell和脚本使用变量来存储数据,有些变量可以连同它们的内容传递给子进程,这些变量我们称之为环境变量。
(1)环境级环境变量:只能在当前环境使用
[root@mail-qq mnt]# a=1
[root@mail-qq mnt]# echo $a
1
[root@mail-qq mnt]# bash
[root@mail-qq mnt]# echo $a
[root@mail-qq mnt]# exit
exit
[root@mail-qq mnt]# export a=1
[root@mail-qq mnt]# bash
[root@mail-qq mnt]# echo $a
1
[root@mail-qq mnt]# exit
exit
[root@mail-qq mnt]# b=1
[root@mail-qq mnt]# vim test.sh
vim:
echo $b
[root@mail-qq mnt]# sh test.sh
[root@mail-qq mnt]# echo $b
1
[root@mail-qq mnt]# cat test.sh
#!/bin/bash
echo $b
(2)用户级环境变量
[root@mail-qq mnt]# su - student
[student@mail-qq ~]# vim .bash_profile ##环境变量配置文件,永久配置
vim:
export a=2
:wq
[student@mail-qq ~]# source .bash_profile
[student@mail-qq ~]# echo $a
2
(3)全局变量
[root@mail-qq ~]# vim /etc/profile
vim:
exprot a=1
:wq
[root@mail-qq ~]# source /etc/profile
[root@mail-qq ~]# echo $a
1
[root@mail-qq ~]# su - student
[student@mail-qq ~]# echo $a
2
[student@mail-qq ~]# source /etc/profile
mkdir: cannot create directory ‘/home/student/.cache’:Permission denied
[student@mail-qq ~]# echo $a
1
7.别名
(1)使用别名:
alias命令可以用来自定义属于自己的系统命令,写入~/.bashrc文件永久生效。
(2)查看别名:
# alias
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
...
(3)设置别名:
# alias mycom='echo hello;hostname'
# mycomm
hello
server0.example.com
删除别名: unalias mycomm
- linux基础3.12shell脚本2
- linux shell脚本基础-----2
- Linux Shell脚本基础
- Linux Shell脚本基础
- Linux shell脚本基础
- Linux shell脚本基础
- Linux Shell 脚本基础
- 【Linux基础】shell脚本
- Linux Shell脚本编写基础(2)
- linux shell脚本编写基础
- Linux shell脚本编写基础
- Linux shell脚本编写基础
- Linux shell脚本编写基础
- Linux shell脚本编写基础
- Linux shell脚本编写基础
- Linux shell脚本编写基础
- Linux shell 脚本编程基础
- Linux shell脚本基础学习
- HDU 1159 Common Subsequence【LCS(dp)】
- restTemplate公用魔板使用及中文乱码问题
- 中序遍历树(不一定是二叉树)
- SMTP邮件传输协议发送邮件和附件
- POJ 2431 Expedition(贪心+优先队列)
- linux基础3.12shell脚本2
- NIO中通道(Channel)
- 【Summary】——>Web组.Week4
- Struts2框架的基本使用(三)
- Nginx隐藏响应头信息的Server信息和版本信息
- JMeter学习笔记10-Graph Results介绍
- 轮转调度算法(C语言描述)
- 第六讲、软件包管理
- 使用Angular CLI创建一个Angualr4项目