[root@localhost mnt]# exit 退出
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 03:44:50 2018 from 172.25.254.21
/usr/bin/xauth: file /root/.Xauthority does not exist
[root@localhost ~]# /mnt/file.sh 临时修改退出失效运行脚本没有值
[root@localhost ~]# vim .bash_profile 修改用户环境变量
[root@localhost ~]# source .bash_profile 刷新
[root@localhost ~]# echo $a a有值
1
[root@localhost ~]# logout再次退出
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 04:49:40 2018 from 172.25.254.21
[root@localhost ~]# echo $a 进入a的值依旧存在
1
[root@localhost ~]# su - student
Last login: Thu May 11 20:23:54 EDT 2017 on pts/0
[student@localhost ~]$ echo $a 进去学生用户没有因为加载的时候已经将原来的文件覆盖,所以root用户设置的不会生效
[student@localhost ~]$ exit 再次退出
logout
[root@localhost ~]# echo $a root用户生效
1
[root@localhost ~]# bash 重新打开一个shell
[root@localhost ~]# echo $a 依旧生效
1
[root@localhost ~]# exit
exit
[root@localhost ~]# vim /etc/profile 编辑系统变量文件
[root@localhost ~]# cat /etc/profile | tail -n 1 将系统变量文件更改为a=2
export a=2
[root@localhost ~]# source /etc/profile 刷新
[root@localhost ~]# echo $a a的值已经改变但是是由于刷新临时的
2
[root@localhost ~]# exit 退出
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 04:52:14 2018 from 172.25.254.21
[root@localhost ~]# echo $a 系统开启先加载系统进程之后加载用户进程,默认读用户进程,所以依旧是a=1
1
[root@localhost ~]# echo $PS1 shell的变量
[\u@\h \W]\$
[root@localhost ~]# PS1='westos>' 更改shell变量
westos>date
Sun Jun 17 05:11:52 EDT 2018
westos>PS1='westos\u>'
westosroot>PS1='westos \u>'
westos root>PS1='westos \W>'
westos ~>PS1='westos \W>'
westos ~>PS1='westos \w>' w为绝对路径
westos ~>cd /mnt/
westos /mnt>cd /etc/sysconfig/
westos /etc/sysconfig>PS1='westos \W>'
westos sysconfig>cd /etc/sysconfig/ W为相对路径
westos sysconfig>PS1='westos @\W>'
westos @sysconfig>exit
logout
Connection to 172.25.254.121 closed.
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
root@172.25.254.121's password:
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Sun Jun 17 04:55:02 2018 from 172.25.254.21
[root@localhost mnt]# sh test.sh 依次测试,$0后面接脚本名字
$0 is test.sh
$1 is
$2 is
$3 is
$# is 0
$* is
$@ is
[root@localhost mnt]# sh test.sh westos $1后面所接第一串字符
$0 is test.sh
$1 is westos
$2 is
$3 is
$# is 1
$* is westos
$@ is westos
[root@localhost mnt]# sh test.sh westos linux $2后面接第二串字符
$0 is test.sh
$1 is westos
$2 is linux
$3 is
$# is 2
$* is westos linux 依次显示1,2,3字符串
$@ is westos linux
[root@localhost mnt]# sh test.sh westos linux redhat $3后面接第三串字符
$0 is test.sh
$1 is westos
$2 is linux
$3 is redhat
$# is 3
$* is westos linux redhat
$@ is westos linux redhat
[root@localhost mnt]# ls
passfile test.sh user_create.sh userfile
[root@localhost mnt]# vim for.sh 编辑脚本检测$*和$@区别
[root@localhost mnt]# sh for.sh
[root@localhost mnt]# sh -x for.sh westos linux redhat $*脚本后跟的所有字符串,模式为"1 2 3"一串字符
+ for name in '"$*"'
+ echo westos linux redhat
westos linux redhat
[root@localhost mnt]# vim for.sh 更改为$@
[root@localhost mnt]# sh -x for.sh westos linux redhat $@ 脚本后跟的所有字符串,模式为"1""2""3"三串字符
+ for name in '"$@"'
+ echo westos
westos
+ for name in '"$@"'
+ echo linux
linux
+ for name in '"$@"'
+ echo redhat
redhat
编写脚本当输入的文件少于两个时提示错误,用脚本建立文件中用户及密码
[root@localhost mnt]# vim user_create.sh 编写脚本
[root@localhost mnt]# sh user_create.sh 调用
ERROR:please give me userfile or passfile!!
[root@localhost mnt]# sh user_create.sh userfile 文件少于两个
ERROR:please give me userfile or passfile!!
[root@localhost mnt]# sh user_create.sh userfile passfile 正确建立文件并显示密码
[root@localhost mnt]# vim bb.sh
[root@localhost mnt]# sh bb.sh 非交互式没有提示
123
123
[root@localhost mnt]# vim bb.sh 加入提示
[root@localhost mnt]# sh bb.sh
please give me a filewestos
westos
[root@localhost mnt]# vim cc.sh 交互式提示并检测IP是否在运行
[root@localhost mnt]# sh cc.sh
please input a number: 172.25.254.121
172.25.254.121 is up
[root@localhost mnt]# vim cc.sh 输入时候加入加密
[root@localhost mnt]# sh cc.sh
please input a number: 172.25.254.121 is up
[root@localhost mnt]# vim cc.sh 加入空行不然显示看起来不好看
[root@localhost mnt]# sh cc.sh
please input a number:
172.25.254.121 is up
操作:
用脚本实现read交互式提示输入建立用户文件,密码文件并检测是否存在:
[root@localhost mnt]# vim user_create.sh 编写脚本
[root@localhost mnt]# sh user_create.sh 调用脚本
please input userfile: lee 文件错误
ERROR: lee is not exist!!
[root@localhost mnt]# sh user_create.sh
please input userfile: userfile
please input passfile: lee 文件名不匹配无法运行脚本
ERROR: lee is not exist!!
[root@localhost mnt]# sh user_create.sh 调用脚本
please input userfile: userfile 有提示输入文件
please input passfile: passfile
Changing password for user user1.
passwd: all authentication tokens updated successfully. 建立成功
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
检测建立用户密码:
[root@localhost mnt]# su - user1 成功切换建立用户
[user1@localhost ~]$ su - user2 验证密码
Password:
Last failed login: Sun Jun 17 01:01:47 EDT 2018 on pts/1
There was 1 failed login attempt since the last successful login.
[user2@localhost ~]$ exit 退出
logout
[user1@localhost ~]$ exit
logout
linux系统中命令别名的设定:
[root@localhost mnt]# alias 查看系统中的别名
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost mnt]# alias xfl='vim' 设置别名
[root@localhost mnt]# xfl调用直接打开vim按:q退出
[root@localhost mnt]# alias 查看别名已经加入
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias xfl='vim'
[root@localhost mnt]# exit 退出失效
[kiosk@foundation21 Desktop]$ ssh root@172.25.254.121 -X
[root@localhost ~]# alias 查看别名失效
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# vim .bashrc 编辑用户家目录文件
[root@localhost ~]# source .bashrc 刷新
[root@localhost ~]# alias 查看别名永久生效
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias xfl='vim'
[root@localhost ~]# su -student 切换到学生用户
su: failed to execute tudent: No such file or directory
[root@localhost ~]# su - student
Last login: Sun Jun 17 04:52:35 EDT 2018 on pts/1
[student@localhost ~]$ xfl 不生效
bash: xfl: command not found...
[student@localhost ~]$ exit
logout
[root@localhost ~]# vim /etc/bashrc 编辑系统配置文件
[root@localhost ~]# source /etc/bashrc 刷新
[root@localhost ~]# su - student 切换用户
Last login: Sun Jun 17 22:44:46 EDT 2018 on pts/0
[student@localhost ~]$ alias 永久有效
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias xfl='vim'
[student@localhost ~]$ xfl 调用
[student@localhost ~]$ exit
logout
[root@localhost ~]# vim /etc/bashrc 删除添加
[root@localhost ~]# vim .bashrc 删除添加
[root@localhost ~]# source .bashrc 刷新
[root@localhost ~]# source /etc/bashrc 刷新
[root@localhost ~]# alias 查看还在
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
alias xfl='vim'
[root@localhost ~]# unalias xfl unalias 撤销命令的别名
[root@localhost ~]# alias 查看已经撤销
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
查询 exit退出值:
exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行。
退出码(exit status,或exit code)的约定:
0表示成功(Zero - Success)
非0表示失败(Non-Zero - Failure)
2表示用法不当(Incorrect Usage)
127表示命令没有找到(Command Not Found)
126表示不是可执行的(Not an executable)
>=128 信号产生
操作演示:
[root@localhost ~]# echo $?
0
[root@localhost ~]# ls
anaconda-ks.cfg Documents Music Public Videos
Desktop Downloads Pictures Templates
[root@localhost ~]# echo $? 查看退出值
0
[root@localhost ~]# vim file.sh 编写脚本
[root@localhost ~]# sh file.sh 调用
hello world
[root@localhost ~]# echo $? 查看退出值
1
[root@localhost ~]# vim file.sh 编写脚本退出值自己设定
[root@localhost ~]# sh file.sh 调用
hello world
[root@localhost ~]# echo $? 查看退出值
66
[root@localhost mnt]# vim cc.sh 编写脚本检测$?退出值是否等于0来返回值
[root@localhost mnt]# sh cc.sh
please input a number:
172.25.254.111 is up
脚本中的函数:
函数可以确保命令循环执行,可以解决命令多重复的情况,
函数用来简化脚本,使脚本可以循环执行。
编写脚本利用函数循环检测IP是否运行并在输入exit退出:
[root@localhost mnt]# vim ip_check.sh 编写脚本
[root@localhost mnt]# sh ip_check.sh 检测脚本功能
please input a ipaddr: 172.25.254.111
172.25.254.111 is up
please input a ipaddr: 172.25.254.222
172.25.254.222 is down
please input a ipaddr: exit
byebye
编写脚本利用函数循环检测文件类型:
[root@localhost mnt]# vim file_check.sh 编写脚本
[root@localhost mnt]# sh file_check.sh /dev/vdb 检测文件类型
/dev/vdb is link
[root@localhost mnt]# sh file_check.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is not exist!!
[root@localhost mnt]# sh file_check.sh /dev/pts/1
/dev/pts/1 is link
利用脚本根据函数建立用户删除用户,并设置密码:
[root@localhost mnt]# vim user_ctl.sh 编写脚本
[root@localhost mnt]# sh user_ctl.sh 调用脚本演示功能
[root@localhost mnt]# sh user_ctl.sh
please input action: add
please input a username: lee 建立用户
please input a password:
Changing password for user lee.
passwd: all authentication tokens updated successfully.
please input action: del 删除用户
please input a username:lee
please input action: exit
bye