shell中的循环语法
SHELL脚本编程配置环境
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
把命令行分成单个命令词
展开别名
展开大括号的声明({})
展开波浪符声明(~)
命令替换$() 和 ``)
再次把命令行分成命令词
展开文件通配(*、?、[abc]等等)
准备I/0重导向(<、>)
运行命令
[root@node101.yinzhengjie.org.cn ~]# echo Your cost:\$15.00
Your cost:$15.00
[root@node101.yinzhengjie.org.cn ~]#
单引号('')防止所有扩展 双引号("")也可防止扩展,但是以下情况例外:
$(美元符号):
变量扩展
` `(反引号):
命令替换
\(反斜线):
禁止单个字符扩展
!(叹号):
历史命令替换
按生效范围划分,存在两类:
全局配置:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
个人配置:
~/.bash_profile
~/.bashrc
交互式登录:
()直接通过终端输入账号密码登录
()使用"su -l UserName"切换的用户
执行顺序:/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc 非交互式登录:
(1)使用"su UserName"切换的用户
(2)图形界面下打开的终端
(3)执行脚本
(4)任何其它的bash实例
执行顺序: /etc/profile.d/*.sh --> /etc/bashrc -->~/.bashrc
按功能划分,存在两类:
即profile类和bashrc类 profile类:为交互式登录的shell提供配置
全局:
/etc/profile, /etc/profile.d/*.sh
个人:
~/.bash_profile
作用:
(1)用于定义环境变量
(2)运行命令或脚本 bashrc类:为非交互式和交互式登录的shell提供配置
全局:
/etc/bashrc
个人:
~/.bashrc
作用:
(1)定义命令别名和函数
(2)定义本地变量 修改profile和bashrc文件后需生效
两种方法:
(1)重新启动shell进程
(2)使用"."或"source"
例:
[root@node101.yinzhengjie.org.cn ~]# . ~/.bashrc
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# source ~/.bashrc
保存在~/.bash_logout文件中(用户) 在退出登录shell时运行 用于"创建自动备份"或者"清除临时文件"。
h:
hashall,打开这个选项后,Shell 会将命令所在的路径hash下来,避免每次都要查询。通过set +h将h选项关闭 i:
interactive-comments,包含这个选项说明当前的 shell 是一个交互式的 shell。所谓的交互式shell,在脚本中,i选项是关闭的。 m:
monitor,打开监控模式,就可以通过Job control来控制进程的停止、继续,后台或者前台执行等。 B:
braceexpand,大括号扩展 H:
history,H选项打开,可以展开历史列表中的命令,可以通过!感叹号来完成,例如“!!”返回上最近的一个历史命令,“!n”返回第 n 个历史命令
[root@node101.yinzhengjie.org.cn ~]# echo $-
himBH
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hash
hits command
/usr/bin/ls
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# set +h
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $-
imBH
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hash
-bash: hash: hashing disabled
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# set -h
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $-
himBH
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hash
hits command
/usr/bin/ls
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
"h"使用案例
[root@node101.yinzhengjie.org.cn ~]# echo $-
himBH
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} #支持大括号扩展功能
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# set +B #禁用大括号功能
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $-
himH
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} #发现禁用大括号功能后,只能原样输出啦~
{a..z}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
"B"使用案例
[root@node101.yinzhengjie.org.cn ~]# history | tail
echo {a..z}
set +B
echo $-
echo {a..z}
history
echo $-
set -B
echo $-
history | tail -
history | tail
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# !- #可以正常使用histrory相关命令
echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $-
himBH
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# set +H #禁用history
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $-
himB
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# history | tail
echo $-
history | tail -
history | tail
echo {a..z}
echo $-
set -H
echo $-
set +H
echo $-
history | tail
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# !- #发现history相关语法命令不可用啦~
-bash: !-: command not found
[root@node101.yinzhengjie.org.cn ~]#
"H"使用案例
[root@node101.yinzhengjie.org.cn ~]# vim shell/test.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/test.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: test.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** echo $-
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/test.sh #我们发现脚本中是没有"-i,-m和-h"选项的
hB
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo $- #查看当前shell支持的功能
himBH
[root@node101.yinzhengjie.org.cn ~]#
查看脚本中的变量默认的"$-"的值
[root@node101.yinzhengjie.org.cn ~]# cat -n /etc/profile #注意观察第67行就用到咱们这里说的"$-"变量哟~
# /etc/profile # System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates. pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$
else
PATH=$:$PATH
fi
esac
} if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`/usr/bin/id -u`
UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi # Path manipulation
if [ "$EUID" = "" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi HOSTNAME=`/usr/bin/hostname >/dev/null`
HISTSIZE=
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL # By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask
else
umask
fi for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
66 if [ -r "$i" ]; then
67 if [ "${-#*i}" != "$-" ]; then
68 . "$i"
69 else
70 . "$i" >/dev/null
71 fi
72 fi
73 done
74
75 unset i
76 unset -f pathmunge
77
78 #Add by yinzhengjie
79 JAVA_HOME=/home/softwares/jdk1.8.0_201
80 PATH=$PATH:$JAVA_HOME/bin
81
82
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat -n /etc/profile #注意观察第67行就用到咱们这里说的"$-"变量哟~
2>.脚本安全
-u:
在扩展一个没有设置的变量时,显示错误信息
等同set -o nounset -e:
如果一个命令返回一个非0退出状态值(失败)就退出
等同set -o errexit
[root@node101.yinzhengjie.org.cn ~]# vim shell/test.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat -n shell/test.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: test.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** set -u #在扩展一个没有设置的变量时,显示错误信息
DIR=/tmp
unset DIR #此处为我们故意将变量名称删除掉,让下一行指令引用时找不到该变量
rm -rf $DIR/test/*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/test.sh
shell/test.sh: line 16: DIR: unbound variable
[root@node101.yinzhengjie.org.cn ~]#
"-u"使用案例
[root@node101.yinzhengjie.org.cn ~]# vim shell/test.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat shell/test.sh
#!/bin/bash
#
#********************************************************************
#Author: yinzhengjie
#QQ:
#Date: --
#FileName: test.sh
#URL: http://www.cnblogs.com/yinzhengjie
#Description: The test script
#Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated.
#******************************************************************** set -ue #"-e"表示如果一个命令返回一个非0退出状态值(失败)就退出当前脚本,即不会继续执行脚本.
xxx
ls
cat /etc/profile
DIR=/tmp
rm -rf $DIR/test/*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bash shell/test.sh
shell/test.sh: line 14: xxx: command not found
[root@node101.yinzhengjie.org.cn ~]#
"-e"选项使用案例
()让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin
()用户 root 登录时,将命令指示符变成红色,并自动启用如下别名:
rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或 ifcfg-ens33 ’ (如果系统是CentOS7)
()任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
()编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
()编写用户的环境初始化脚本reset.sh,包括别名,登录提示符,vim的设置,环境变量等
shell中的循环语法的更多相关文章
- shell 脚本中所有循环语法
写出 shell 脚本中所有循环语法 for 循环 : for i in $(ls);do echo item:$i done while 循环 : #!/bin/bash COUNTER=0 whi ...
- shell中for循环总结
关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...
- shell中的循环
shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...
- shell中for循环
shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...
- Shell中的循环语句实例
1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do echo number $x done 注:" ...
- Linux shell编程 4 ---- shell中的循环
1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...
- shell中while循环的陷阱
在写while循环的时候,发现了一个问题,在while循环内部对变量赋值.定义变量.数组定义等等环境,在循环外面失效. 一个简单的测试脚本如下: #!/bin/bash echo "abc ...
- (八)shell中的循环结构
1.for循环(1)要求:能看懂.能改即可.不要求能够完全不参考写出来.因为毕竟嵌入式并不需要完全重新手写shell,系统管理员(服务器运维人员,应用层系统级管理开发的才需要完全掌握shell) 这里 ...
- shell中的循环语句
for语法格式 for var in list;do commands done 其中list可以包含: 1) 直接写 for alpha in a b c d;do echo $alpha done ...
随机推荐
- 五子棋游戏SRS文档
SRS技术文档,包括对SRS的解释说明.SRS描述规范.软件需求规格说明书(SRS,Software Requirement Specification)是为了软件开发系统而编写的,主要用来描 ...
- SQL之SELECT语句执行顺序及子句功能
1.select 语句的执行顺序 SELECT a.id,a.`product_name`,a.`agreement_copies` i,b.id as statusId from `opmp_pro ...
- SVN入门教程
1. 什么是SVN SVN全名Subversion,即版本控制系统.SVN与CVS一样,是一个跨平台的软件,支持大多数常见的操作系统. 作为一个开源的版本控制系统,Subversion管理者随时间改变 ...
- PHP加密与编码技术
md5加密: string md5( string $str [,bool $raw output=false]) md5加密方法用的挺多,有两个参数,第一个参数是要加密的字符串,第二个参数默认为f ...
- 面象对象设计原则之四:接口隔离原则(The Interface Segregation Principle,ISP)
接口隔离原则定义如下: 接口隔离原则(Interface Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 根 ...
- 转《trackingjs+websocket+百度人脸识别API,实现人脸签到》流程
先用websocket与后台建立通讯:用trackingjs在页面调用电脑摄像头,监听人脸,发现有人脸进入屏幕了,就把图片转成base64字符串,通过websocket发送到后端:后端拿到图片,调用百 ...
- 在使用ADOQuery删除数据时的处理 [问题点数:100分,结帖人isdxsc]
在使用ADOQuery删除数据时的,希望在他的事件BeforeDelete进行一些判断,符合要求的进行删除,不符合要求的终止这个删除行为,请问应该用什么语句呢?还有个比较奇怪的现象也一起请教:DBGr ...
- 【gridview增删改查】数据库查询后lodop打印
ASP.NET中使用gridview可以很容易的把需要的数据动态显示在前台,还可以在表格里加入列进行增删改查,每次点击的时候重新加载数据,gridview也提供了分页等功能,还有一些模版让显示在前台的 ...
- BZOJ2124 等差子序列(树状数组+哈希)
容易想到一种暴力的做法:枚举中间的位置,设该位置权值为x,如果其两边存在权值关于x对称即合法. 问题是如何快速寻找这个东西是否存在.考虑仅将该位置左边出现的权值标1.那么若在值域上若关于x对称的两权值 ...
- VMware下Centos7快速搭建vsftpd
最简单快捷的实现ftp的功能,不考虑安全问题. 1.配置防火墙和selinux vi /etc/selinux/config # This file controls the state of SEL ...