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 ...
随机推荐
- 使用Eclipse可以方便的统计工程或文件的代码行数,
使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File... 2.选中正则表达式(Regular expressi ...
- 剑指offer:树的子结构
题目描述: 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 解题思路: 同样考虑用递归来做. 利用两个递归函数,一个用于判断两棵树树否相等,另一个递归取A的 ...
- 软件工程-pair work[附加题]
首先,在分组之前,我和室友周敏轩已经详细阅读了往届学长的博客,认为电梯调度这个项目应该先做UI会比较好一点,于是动手展开了UI的编写;但分组结果并没有如我们所愿,但我们依然共同进行了UI的编写,希望在 ...
- mybatis 框架网站
http://www.mybatis.org/mybatis-3/zh/index.html
- Server Tomcat v7.0 Server at libra failed to start
https://stackoverflow.com/questions/13244233/server-tomcat-v7-0-server-at-localhost-failed-to-start- ...
- back to top 回到顶部按钮 css+js
效果 html <p id="back-to-top"><a href="#top"><span></span> ...
- Angular 简单的Get
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...
- loadrunner基础学习笔记七-面向目标场景
部署应用程序之前,要执行验收测试以确保系统能够承担预期的实际工作量. 可以为想要生成的每秒点击次数,每秒事务数或事务响应时间设置目标 loadrunner将使用面向目标的场景自动生成所需的目标,当应用 ...
- inf
下确界 inf,表示下确界,英文名infimum. 对于函数y=f(x),在使f(x)大于等于M成立的所有常数M中,我们把M的最大值max(M)(即函数y=f(x)的最小值)叫做函数y=f(x)的下确 ...
- arctan
ArcTanWhen the ArcTan functional configuration is selected, the input vector (X_IN,Y_IN) is rotated( ...