您的位置:首页 > 运维架构 > Linux

Linux学习第十三天--配置用户环境

2017-08-08 14:54 281 查看

read命令

使用read来把输入值分配给一个 或多个shell变量

-p 指定要显示提示

-s 静默输入,一般用于密码

-n N指定输入的字符长度N

-d ‘字符’输入结束符

-t N TIMEOUT为N秒

从标准输入中读取值,给每个单词分配一个变量,所有剩余单词都分配给最后一个变量。

eg:read -n “Enter a filename” File

read命令的使用可以使我们的脚本交互性更加人性化,更加方便用户的使用。

防止扩展

反斜线(\)会使随后的字符按愿意解释

[root@localhost ~]# echo Your cost: $5.00
Your cost: .00
[root@localhost ~]# echo Your cost: \$5.00
Your cost: $5.00


加引号防止扩展

单引号(')防止所有扩展
双引号(")也防止所有扩展,但是一下情况例外:
$(美元符号) - 变量扩展
`(反引号) - 命令替换
\(反斜杠) - 禁止单个字符扩展
!(叹号) - 历史命令替换


[root@localhost ~]# echo "hello!"
-bash: !": event not found
[root@localhost ~]# echo 'hello!'
hello!


bash的配置文件

按生效范围划分,存在两类:

全局配置:

/etc/profile
/etc/profile.d/*.sh
/etc/basgrc


个人配置:

~/.bash_profile
~/.bashrc


shell的两种登陆方式

交互式登陆:

1. 直接使用终端输入账号密码登陆
2. 使用"su - username"切换的用户
执行顺序:/etc/profile --> /etc/profile.d/*.sh --> .bash_profile --> ~/.bashrc --> /etc/bashrc


非交互式登陆:

1. su username
2. 图形界面下打开的终端
3. 执行脚本
4. 任何其他的bash示例
执行顺序:~/.bashrc --> /etc/bashrc --> /etc/prifile.d/*.sh


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
eg1: . ~/.bashrc
eg2: source ~/.bashr
9d2c
c


bash 退出任务

当我们需要exit时,需要执行一些命令或脚本,可以将其放入~/.bash_logout文件中,在退出当前shell时执行。

用于:

创建自动备份

清除临时文件

eg:
[root@localhost ~]# vim .bash_logout
1 # ~/.bash_logout
2 clear #清屏
3 cat /dev/null &> /var/log/messages #清空日志
4 cp /etc/passwd /app #备份文件


例题

让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin。

解题思路:
First:为了让所有用户的PATH环境变量都生效,我们应该将执行命令放在全局配置文件中。
Second:避免对配置文件误操作,这里我们选择在/etc/profile.d/文件夹下创建以.sh结尾的脚本。
eg:
[root@localhost ~]# vim /etc/profile.d/path.sh
1 echo $PATH | egrep '/usr/local/apache/bin' &> /dev/null || PATH=$PATH:/usr/local/apache/bin #做个判断,避免交互式登陆,造成对
'/usr/local/apache/bin'的叠加。


用户root登录时,将命令指示符变成红色,并自动启用如下别名:

rm=‘rm –i’cdnet=‘cd /etc/sysconfig/network-scripts/’

editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-ens33 ’

解题思路:
切入root用户下,进行操作。
eg:
[root@localhost ~]# vim .bashrc
1 # .bashrc
2
3
4 # User specific aliases and functions
5
6 alias rm='rm -i'
7 alias cp='cp -i'
8 alias mv='mv -i'
#上面是系统自己配置的
9 PS1="\[\e[31m\][\u@\h \W]\\$\[\e[0m\] "
10 alias cdnet='cd /etc/sysconfig/network-scripts/'
11 alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-ens33'


任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”

解题思路:
目的对任意用户生效,则需要对全局配置文件进行操作,这里我们选择在/etc/profile.d/目录下新建一个warning.sh文件。
eg:
[root@localhost ~]# vim /etc/profile.d/warning.sh
1 echo -e "\e[31m "Hi,dangerous!"\e[0m"


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: