linux学习19 shell脚本基础-bash脚本编程基础及配置文件

一、shell脚本编程

  1、编程语言的分类,根据运行方式

    a、编译运行:源代码 --> 编译器(编译) --> 程序文件

      C语言:

    b、解释运行:源代码 --> 运行时启动解释器,由解释器边解释边运行;即源代码本身并不能运行,而是启动一个解释器的进程,把整个源代码的内容当做解释器的参数。因为其是边解释边运行因此运行过程中比起编译运行速度会差一些。

    c、无论是编译器还是解释器中间总需要另外一个程序,即在运行过程中全程参与,这就是我们说的翻译官,他需要将我们人能识别的代码转换成机器所能识别的机器码。

  2、根据其编程过程中功能的实现是调用库还是调用外部的程序文件:

    a、shell脚本编程:

      利用系统上的命令及编程组件进行编程

    b、完整编程:

      利用库或编程组件进行编程

  3、根据编程模型进行分类:过程式编程语言,面向对象的编程语言

    程序=指令+数据

      过程式:以指令为中心来组织代码,数据是服务于代码的。

        顺序执行。

        选择执行

        循环执行

        C,bash

      对象式:以数据为中心来组织代码,围绕数据来组织指令。

        类(class):实例化对象,method;

        代表:java,C++,Python

  4、shell脚本编程:过程式编程,解释运行,依赖于外部程序文件运行;

二、如何写shell脚本

  1、脚本文件的第一行,顶格:给出shebang,解释器途径,用于指明解释执行当前脚本的解释器程序文件。

    a、常见的解释器

      #!/bin/bash

      #!/usr/bin/python

      #!/usr/bin/perl

    b、常见的文本编辑器:nano

      行编辑器:sed

      全屏幕编辑器:nano,vi,vim

[root@node1 ~]# nano myfirst.sh
[root@node1 ~]# cat myfirst.sh 
#!/bin/bash
useradd user3
echo "user3"|passwd --stdin user3
mktemp -d /tmp/test.xxxx

[root@node1 ~]# chmod +x myfirst.sh 
[root@node1 ~]# ./myfirst.sh 
Changing password for user user3.
passwd: all authentication tokens updated successfully.
mktemp: too few X's in template ‘/tmp/test.xxxx’

  2、shell脚本是什么?

    命令的堆积

    但很多命令不具有幂等性,需要用程序逻辑来判断运行条件是否满足,以避免其运行中发生错误。

  3、运行脚本

    a、赋予执行权限,并直接运行此程序文件即可

      chmod +x /PATH/TO/SCRIPT_FILE

      /PATH/TO/SCRIPT_FILE

    b、直接运行解释器,将脚本以命令行参数传递给解释器程序

      bash /PATH/TO/SCRIPT_FILE

    注意:

      脚本中的空白行会被解释器忽略

      脚本中,除了shebang,余下所有以#开头的行,都会被视作注释行而被忽略;此即为注释行。

    c、练习1:写一个脚本,实现如下功能:

      (1)、显示/etc目录下所有以大写p或小写p开头的文件或目录本身

[root@node1 ~]# ls -d /etc/[pP]*
/etc/pam.d   /etc/passwd-  /etc/pkcs11  /etc/plymouth  /etc/polkit-1  /etc/postfix  /etc/prelink.conf.d  /etc/profile    /etc/protocols
/etc/passwd  /etc/pinforc  /etc/pki     /etc/pm        /etc/popt.d    /etc/ppp      /etc/printcap        /etc/profile.d  /etc/python

      (2)、显示/var目录下的所有文件或目录本身,并将显示结果中的小写字母转换为大写后显示;

[root@node1 var]# ls -d /var/* |tr 'a-z' "A-Z"
/VAR/ACCOUNT
/VAR/ADM
/VAR/CACHE
/VAR/CRASH
/VAR/DB
/VAR/EMPTY
/VAR/GAMES
/VAR/GOPHER
/VAR/KERBEROS
/VAR/LIB
/VAR/LOCAL
/VAR/LOCK
/VAR/LOG
/VAR/MAIL
/VAR/NIS
/VAR/OPT
/VAR/PRESERVE
/VAR/RUN
/VAR/SPOOL
/VAR/TMP
/VAR/WWW
/VAR/YP

      (3)、创建临时文件/tmp/myfile.XXXX

[root@node1 var]# mktemp -d /tmp/myfile.XXXX
/tmp/myfile.Z19s

三、bash的配置文件

  1、分为两类

    a、profile类:为交互式登陆的shell进程提供配置

    b、bashrc类:为非交互式登陆的shell进程提供配置

  2、登陆类型

    a、交互式登陆shell进程:

      直接通过某终端输入账号和密码后登陆打开的shell进程

      使用su 命令:su - USERNAME,或者使用su -l USERNAME执行的登陆切换

    b、非交互式登陆shell进程

      su USERNAME执行的登陆切换

      在图形界面下打开的终端

      运行脚本

  3、profile类

    a、全局:对所有用户都生效

      /etc/profile

      /etc/profil.d/*.sh

    b、用户个人:仅对当前用户有效

      ~/.bash_profile

    c、功用:

      (1)、用于定义环境变量

      (2)、运行命令或脚本

  4、bashrc类:

    a、全局:

      /etc/bashrc

    b、用户个人

      ~/.bashrc

    c、功用

      (1)、定义本地变量

      (2)、定义命令别名

    d、注意:仅管理员可修改全局配置文件

  5、配置文件的读取次序

    a、交互式登陆shell进程: /etc/profile --> /etc/profile.d --> ~/.bash_profile --> ~/.bashrc -->/etc/bashrc

    b、非交互式登陆shell进程:

      ~/.bashrc --> /etc/bashrc --> /etc/profile.d/* 

  6、配置文件生效特性

    a、命令行中定义的特性,例如变量和别名作用域为当前shell进程的生命周期

    b、配置文件定义的特性,只对随后新启动的shell进程有效

    c、让通过配置文件定义的特性立即生效

      (1)、通过命令行重复定义一次

      (2)、让shell进程重读配置文件

          source /PATH/FROM/CONF_FILE

          .(小点)  /PATH/FROM/CONF_FILE

[root@node1 ~]# 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@node1 ~]# nano /root/.bashrc 
[root@node1 ~]# cat /root/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias cls='clear'
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi
[root@node1 ~]# source /root/.bashrc 
[root@node1 ~]# alias 
alias cls='clear'
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'

四、问题

  1、定义对所有用户都生效的命令别名,例如:"lftps"="lftp 172.16.0.1/pub"

  2、让centos用户登录时,提供其已经登录,并显示当前系统时间

原文地址:https://www.cnblogs.com/Presley-lpc/p/12085132.html