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 ...
随机推荐
- android开发之图表
在这里使用的插件为Mpchart,只以折线图为例.首先需要导入
- 四则运算APP
1) N (Need 需求) 用户基本需求:随机生成四则运算,能自动判定对错,答错时能提示正确答案! 在这个基础上,我的创意: 多用户模式,能记录用户的答题情况(登陆功能) 分级挑战,按照不同的水 ...
- enumerate()函数用法
enumerate 函数用于遍历序列中的元素以及它们的下标:
- C 实现选择排序
一.选择排序的思想 假设有一个7元素的数组 [11, 24, 5, 17, 2, 8, 20],我们通过选择排序来从小到大排序. 思想是进行7次外循环从0-->6,每一次又是一个内循环,从i+1 ...
- JSON数据格式解析
JSON数据的语法规则 1.数据以键值对的形式 2.数据由逗号分隔 3.花括号保存对象 4.方括号保存数组 以PHP的数组为例: <?php $arr = array( "aaaa&q ...
- 线性代数的本质与几何意义 02. 线性组合、张成的空间、基(3blue1brown 咪博士 图文注解版)
1. 线性组合 接下来我们要换一个角度来看向量.以二维平面直角坐标系为例,i, j 分别是沿 2 个坐标轴方向的单位向量.那么坐标平面上的其他向量,例如 [ 3 -2 ] [3−与 i, j 是什么 ...
- maven手动添加jar包到本地仓库
推荐几个好的 Maven 常用仓库网址:http://mvnrepository.com/http://search.maven.org/ Maven 安装 JAR 包的命令是: mvn instal ...
- BZOJ1916[USACO 2010 Open Gold 2.Water Slides]——DP+记忆化搜索
题目描述 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园.当然,它最大的亮点就是新奇巨大的水上冲浪.超级轨道包含 E (1 <= E <=15 ...
- BZOJ5311 贞鱼(动态规划+wqs二分+决策单调性)
大胆猜想答案随k变化是凸函数,且有决策单调性即可.去粘了份fread快读板子才过. #include<iostream> #include<cstdio> #include&l ...
- SQL partition (小组排序)
很多时候,我们在SQL中进行数据去重(distinct) 结果发现有2条一样ID,或者name的数据,我们想要最接近的那条数据. 直接看看题目: 原表 select ID,Title,PRICE fr ...