SHELL脚本编程配置环境

                                                     作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 
 
 
一.bash如何展开命令行
1>.bash的优先级
  把命令行分成单个命令词
  展开别名

  展开大括号的声明({})

  展开波浪符声明(~)

  命令替换$() 和 ``)

  再次把命令行分成命令词

  展开文件通配(*、?、[abc]等等)

  准备I/0重导向(<、>)

  运行命令
2>.反斜线(\)会使随后的字符按原意解释
[root@node101.yinzhengjie.org.cn ~]# echo Your cost:\$15.00
Your cost:$15.00
[root@node101.yinzhengjie.org.cn ~]#
3>.加引号来防止扩展
    单引号('')防止所有扩展

    双引号("")也可防止扩展,但是以下情况例外:
    $(美元符号):
      变量扩展
    ` `(反引号):
      命令替换
    \(反斜线):
      禁止单个字符扩展
    !(叹号):
      历史命令替换
二.bash的配置文件
按生效范围划分,存在两类:
  全局配置:
    /etc/profile
    /etc/profile.d/*.sh
    /etc/bashrc
  个人配置:
    ~/.bash_profile
    ~/.bashrc
 
三.shell登录两种方式
交互式登录:
  ()直接通过终端输入账号密码登录
  ()使用"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类和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退出任务
  保存在~/.bash_logout文件中(用户)

  在退出登录shell时运行

  用于"创建自动备份"或者"清除临时文件"。
 
六.set 命令
1>."$-"变量
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中的循环语法的更多相关文章

  1. shell 脚本中所有循环语法

    写出 shell 脚本中所有循环语法 for 循环 : for i in $(ls);do echo item:$i done while 循环 : #!/bin/bash COUNTER=0 whi ...

  2. shell中for循环总结

    关于shell中的for循环用法很多,一直想总结一下,今天网上看到上一篇关于for循环用法的总结,感觉很全面,所以就转过来研究研究,嘿嘿... 1. for((i=1;i<=10;i++));d ...

  3. shell中的循环

    shell中的循环 for循环 类似于C语言的步长控制 例如: ;i<=;i++)); ); done 将1到10,依次乘以4,然后打印出来. 这里顺便提一下,shell里面表达式的计算,可以有 ...

  4. shell中for循环

    shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...

  5. Shell中的循环语句实例

    1.for循环语句实例1.1 最基本的for循环 #!/bin/bash for x in one two three four do     echo number $x done 注:" ...

  6. Linux shell编程 4 ---- shell中的循环

    1 for循环 1 for语句的结构 for variable in values; do statement done 2 for循环通常是用来处理一组值,这组值可以是任意的字符串的集合 3 for ...

  7. shell中while循环的陷阱

    在写while循环的时候,发现了一个问题,在while循环内部对变量赋值.定义变量.数组定义等等环境,在循环外面失效. 一个简单的测试脚本如下: #!/bin/bash echo "abc ...

  8. (八)shell中的循环结构

    1.for循环(1)要求:能看懂.能改即可.不要求能够完全不参考写出来.因为毕竟嵌入式并不需要完全重新手写shell,系统管理员(服务器运维人员,应用层系统级管理开发的才需要完全掌握shell) 这里 ...

  9. shell中的循环语句

    for语法格式 for var in list;do commands done 其中list可以包含: 1) 直接写 for alpha in a b c d;do echo $alpha done ...

随机推荐

  1. PHP常用类------生成验证码类Code

    直接附上代码吧!很简单的代码,写一遍基本就会了,主要明白用GD库画图的几个步骤: 1.先创建画布,不然画在哪儿?(imagecreatetruecolor)2.根据要画的数据,选定颜色 (imagec ...

  2. PHP文件系统操作常用函数

    虽然PHP提供很多内置的文件处理函数,但是分得特别细,有一些操作需要多个函数一起使用才能达到目标,比如删除非空文件夹的所有内容,遍历文件夹等功能,下面各个函数是学习的时候整理的,有的是教程里的,有的是 ...

  3. FreeMarker example all in one

    Pick up from http://demojava.iteye.com/blog/800204

  4. Facebook 50%用户是虚假账号?我觉得可以更高!

    0x00 背景 今天下午看新闻时,无意看到一条关于facebook虚假帐号(fake account)消息: 一下子就被这标题吸引了眼球,因为作为一个第三方机构,能够对facebook这样如此海量的帐 ...

  5. [转帖]TMD为你揭秘中国互联网下半场所有秘密

    https://www.iyiou.com/p/35099.html 李安说,<比利.林恩的中场战事>是“一个成长的故事”.中国互联网也行至中场,下半场如何走,成长的方向在哪里,成当下关键 ...

  6. 设计模式之抽象工厂模式(附带类似反射功能的实现/c++)

    问题描述 假设我们要开发一款游戏, 当然为了吸引更多的人玩, 游戏难度不能太大(让大家都没有信心了,估计游戏也就没有前途了),但是也不能太简单(没有挑战性也不符合玩家的心理).于是我们就可以采用这样一 ...

  7. scipy优化器optimizer

    #optimazer优化器 from scipy.optimize import minimize def rosem(x): return sum(100.0*(x[1:]-x[:-1])**2.0 ...

  8. logstash 使用kafka范例

    写入到kafka input { stdin { } } output { kafka { bootstrap_servers => "10.0.0.200:9092" to ...

  9. 配合es5.8的使用,升级sb版本到2.X,遇到一个问题

    问题:Failed to bind properties under 'spring.redis.jedis.pool.max-wait' to java.time.Duration: Propert ...

  10. BZOJ3653谈笑风生——可持久化线段树+dfs序

    题目描述 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ? 设a 和 b 为 T 中的两个不同节点.如果 a ...