美文网首页
shell入门17-shell解释器Bash属性设置

shell入门17-shell解释器Bash属性设置

作者: 万州客 | 来源:发表于2023-01-02 09:42 被阅读0次

    这次这本书的体系性,确实比较全面,以后应该经常翻翻的。嗯,应该买一本收藏,变成枕边书之一吧。

    一,set命令及属性设置

    image.png
    image.png
    image.png

    练习

    [root@127 tmp]# set -o
    allexport       off
    braceexpand     on
    emacs           on
    errexit         off
    errtrace        off
    functrace       off
    hashall         on
    histexpand      on
    history         on
    ignoreeof       off
    interactive-comments    on
    keyword         off
    monitor         on
    noclobber       off
    noexec          off
    noglob          off
    nolog           off
    notify          off
    nounset         off
    onecmd          off
    physical        off
    pipefail        off
    posix           off
    privileged      off
    verbose         off
    vi              off
    xtrace          off
    [root@127 tmp]# shopt
    autocd          off
    cdable_vars     off
    cdspell         off
    checkhash       off
    checkjobs       off
    checkwinsize    on
    cmdhist         on
    compat31        off
    compat32        off
    compat40        off
    compat41        off
    direxpand       off
    dirspell        off
    dotglob         off
    execfail        off
    expand_aliases  on
    extdebug        off
    extglob         on
    extquote        on
    failglob        off
    force_fignore   on
    globstar        off
    gnu_errfmt      off
    histappend      on
    histreedit      off
    histverify      off
    hostcomplete    on
    huponexit       off
    interactive_comments    on
    lastpipe        off
    lithist         off
    login_shell     on
    mailwarn        off
    no_empty_cmd_completion off
    nocaseglob      off
    nocasematch     off
    nullglob        off
    progcomp        on
    promptvars      on
    restricted_shell        off
    shift_verbose   off
    sourcepath      on
    syslog_history  off
    xpg_echo        off
    [root@127 tmp]# test=123
    [root@127 tmp]# bash
    [root@127 tmp]# echo $test
    
    [root@127 tmp]# exit
    exit
    [root@127 tmp]# set -a
    [root@127 tmp]# test=123
    [root@127 tmp]# bash
    [root@127 tmp]# echo $test
    123
    [root@127 tmp]# exit
    exit
    [root@127 tmp]# set +a
    [root@127 tmp]# echo {a..c}
    a b c
    [root@127 tmp]# set +B
    [root@127 tmp]# echo {a..c}
    {a..c}
    [root@127 tmp]# set -B
    [root@127 tmp]# vim errexit.sh
    [root@127 tmp]# bash errexit.sh 
    useradd: user 'root' already exists
    [root@127 tmp]# cat errexit.sh 
    #!/bin/bash
    # 功能描述:通过set -e设置命令返回非0状态时,脚本直接退出
    
    set -e
    useradd root
    echo "123456" |passwd --stdin root
    echo "已经将root密码修改为:123456."
    
    [root@127 tmp]# ls
    11.conf  22.conf  33.conf  a.sh  a.txt  A.txt  b.sh  b.txt  B.txt  conf.log  c.sh  errexit.sh  filebak.sh  passwd  sh.log
    [root@127 tmp]# ip link show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
        link/ether 08:00:27:cd:f9:49 brd ff:ff:ff:ff:ff:ff
    3: br-b237cf9cdae2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:9b:b4:8a:d4 brd ff:ff:ff:ff:ff:ff
    4: br-cdb6d62bd945: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:9f:0b:6c:48 brd ff:ff:ff:ff:ff:ff
    5: br-00b2bd0ce722: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:6c:11:9c:e5 brd ff:ff:ff:ff:ff:ff
    6: br-4ad1628686bb: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:b3:53:7f:f6 brd ff:ff:ff:ff:ff:ff
    7: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:27:f9:76:c4 brd ff:ff:ff:ff:ff:ff
    8: br-6195062f933e: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:63:f3:04:ea brd ff:ff:ff:ff:ff:ff
    9: br-6ef894cc54be: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:fb:56:76:6a brd ff:ff:ff:ff:ff:ff
    10: br-975568a34e5f: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:33:ef:8a:ff brd ff:ff:ff:ff:ff:ff
    [root@127 tmp]# yum repolist
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    repo id                                            repo name                                                        status
    base/7/x86_64                                      CentOS-7 - Base - mirrors.aliyun.com                             10,072
    docker-ce-stable/7/x86_64                          Docker CE Stable - x86_64                                           193
    epel/x86_64                                        Extra Packages for Enterprise Linux 7 - x86_64                   13,730
    extras/7/x86_64                                    CentOS-7 - Extras - mirrors.aliyun.com                              515
    mysql-connectors-community/x86_64                  MySQL Connectors Community                                          206
    mysql-tools-community/x86_64                       MySQL Tools Community                                                94
    mysql56-community/x86_64                           MySQL 5.6 Community Server                                          581
    updates/7/x86_64                                   CentOS-7 - Updates - mirrors.aliyun.com                           4,538
    repolist: 29,929
    [root@127 tmp]# hash
    hits    command
       2    /usr/bin/basename
       3    /usr/bin/bash
       1    /usr/bin/du
       1    /usr/bin/rm
       2    /usr/bin/vim
       2    /usr/bin/dirname
       5    /usr/bin/cat
       7    /usr/bin/touch
       2    /usr/bin/yum
       1    /usr/bin/paste
       1    /usr/bin/mkdir
       1    /usr/bin/wc
      12    /usr/bin/ls
       1    /usr/bin/tree
       1    /usr/sbin/ip
    [root@127 tmp]# ls
    11.conf  22.conf  33.conf  a.sh  a.txt  A.txt  b.sh  b.txt  B.txt  conf.log  c.sh  errexit.sh  filebak.sh  passwd  sh.log
    [root@127 tmp]# ls
    11.conf  22.conf  33.conf  a.sh  a.txt  A.txt  b.sh  b.txt  B.txt  conf.log  c.sh  errexit.sh  filebak.sh  passwd  sh.log
    [root@127 tmp]# ls
    11.conf  22.conf  33.conf  a.sh  a.txt  A.txt  b.sh  b.txt  B.txt  conf.log  c.sh  errexit.sh  filebak.sh  passwd  sh.log
    [root@127 tmp]# hash
    hits    command
       2    /usr/bin/basename
       3    /usr/bin/bash
       1    /usr/bin/du
       1    /usr/bin/rm
       2    /usr/bin/vim
       2    /usr/bin/dirname
       5    /usr/bin/cat
       7    /usr/bin/touch
       2    /usr/bin/yum
       1    /usr/bin/paste
       1    /usr/bin/mkdir
       1    /usr/bin/wc
      15    /usr/bin/ls
       1    /usr/bin/tree
       1    /usr/sbin/ip
    [root@127 tmp]# set +h
    [root@127 tmp]# set -h
    [root@127 tmp]# !yum
    yum repolist
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    repo id                                            repo name                                                        status
    base/7/x86_64                                      CentOS-7 - Base - mirrors.aliyun.com                             10,072
    docker-ce-stable/7/x86_64                          Docker CE Stable - x86_64                                           193
    epel/x86_64                                        Extra Packages for Enterprise Linux 7 - x86_64                   13,730
    extras/7/x86_64                                    CentOS-7 - Extras - mirrors.aliyun.com                              515
    mysql-connectors-community/x86_64                  MySQL Connectors Community                                          206
    mysql-tools-community/x86_64                       MySQL Tools Community                                                94
    mysql56-community/x86_64                           MySQL 5.6 Community Server                                          581
    updates/7/x86_64                                   CentOS-7 - Updates - mirrors.aliyun.com                           4,538
    repolist: 29,929
    [root@127 tmp]# set +H
    [root@127 tmp]# !yum
    -bash: !yum: command not found
    [root@127 tmp]# !yum -H
    -bash: !yum: command not found
    [root@127 tmp]# set -H
    [root@127 tmp]# !yum
    yum repolist
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    repo id                                            repo name                                                        status
    base/7/x86_64                                      CentOS-7 - Base - mirrors.aliyun.com                             10,072
    docker-ce-stable/7/x86_64                          Docker CE Stable - x86_64                                           193
    epel/x86_64                                        Extra Packages for Enterprise Linux 7 - x86_64                   13,730
    extras/7/x86_64                                    CentOS-7 - Extras - mirrors.aliyun.com                              515
    mysql-connectors-community/x86_64                  MySQL Connectors Community                                          206
    mysql-tools-community/x86_64                       MySQL Tools Community                                                94
    mysql56-community/x86_64                           MySQL 5.6 Community Server                                          581
    updates/7/x86_64                                   CentOS-7 - Updates - mirrors.aliyun.com                           4,538
    repolist: 29,929
    [root@127 tmp]# vim nounset.sh
    [root@127 tmp]# bash nounset.sh 
    nounset.sh: line 5: $1: unbound variable
    [root@127 tmp]# cat nounset.sh 
    #!/bin/bash
    # 功能描述:通过设置nounset属性,防止变量未定义导致的意外错误出现
    
    set -u
    useradd $1
    echo "$2" |passwd --stdin $1
    
    

    二,shopt属性及设置

    image.png

    练习

    [root@127 tmp]# hello=/etc
    [root@127 tmp]# cd hello
    -bash: cd: hello: No such file or directory
    [root@127 tmp]# shopt -s cdable_vars
    [root@127 tmp]# cd hello
    /etc
    [root@127 etc]# shopt -u cdable_vars
    [root@127 etc]# cd /ect
    -bash: cd: /ect: No such file or directory
    [root@127 etc]# shopt -s cdspell
    [root@127 etc]# cd /ect
    /etc
    [root@127 etc]# cd /etcc
    /etc
    [root@127 etc]# cd /ettc
    /etc
    [root@127 etc]# cd /ettcc
    -bash: cd: /ettcc: No such file or directory
    [root@127 etc]# shopt -u cdspell
    [root@127 etc]# cd /ettc
    -bash: cd: /ettc: No such file or directory
    [root@127 etc]# for i in {1..3}
    > do
    >   echo $i
    > done
    1
    2
    3
    [root@127 etc]# history |tail -2
     1240  for i in {1..3}; do   echo $i; done
     1241  history |tail -2
    [root@127 etc]# shopt -u cmdhist
    [root@127 etc]# for i in 8 9
    > do
    >   echo $i
    > done
    8
    9
    [root@127 etc]# history |tail -10
     1238  shopt -u cdspell
     1239  cd /ettc
     1240  for i in {1..3}; do   echo $i; done
     1241  history |tail -2
     1242  shopt -u cmdhist
     1243  for i in 8 9
     1244  do
     1245    echo $i
     1246  done
     1247  history |tail -10
    [root@127 etc]# shopt -s cmdlist
    -bash: shopt: cmdlist: invalid shell option name
    [root@127 etc]# shopt -s cmdhist
    

    三,使用tput命令查看和设置命令行终端属性

    练习

    [root@127 etc]# tput cols
    122
    [root@127 etc]# tput lines
    15
    [root@127 etc]# tput clear
    [root@127 etc]# tput cup 10 20
    
    
    
    
    
    
    
    
    
                        [root@127 etc]# 
    [root@127 etc]# 
    [root@127 etc]# tput cup 20 10
    
              [root@127 etc]# 
    [root@127 etc]# tput sc
    [root@127 etc]# ls
    adjtime                  exports        libnl                     popt.d               skel
    aliases                  favicon.png    libuser.conf              postfix              snmp
    aliases.db               filesystems    locale.conf               ppp                  ssh
    alternatives             firewalld      localtime                 prelink.conf.d       ssl
    anacrontab               fonts          login.defs                printcap             statetab
    asound.conf              fstab          logrotate.conf            profile              statetab.d
    audisp                   gcrypt         logrotate.d               profile.d            subgid
    audit                    gdbinit        lvm                       protocols            subgid-
    bash_completion.d        gdbinit.d      machine-id                python               subuid
    bashrc                   glvnd          magic                     rc0.d                subuid-
    binfmt.d                 gnupg          mailcap                   rc1.d                subversion
    cacti                    GREP_COLORS    makedumpfile.conf.sample  rc2.d                sudo.conf
    centos-release           groff          man_db.conf               rc3.d                sudoers
    centos-release-upstream  group          mime.types                rc4.d                sudoers.d
    chkconfig.d              group-         mke2fs.conf               rc5.d                sudo-ldap.conf
    containerd               grub2.cfg      modprobe.d                rc6.d                sysconfig
    cron.d                   grub.d         modules-load.d            rc.d                 sysctl.conf
    cron.daily               gshadow        motd                      rc.local             sysctl.d
    cron.deny                gshadow-       mtab                      redhat-release       systemd
    cron.hourly              gss            my.cnf                    redis.conf           system-release
    cron.monthly             host.conf      my.cnf.d                  redis-sentinel.conf  system-release-cpe
    crontab                  hostname       NetworkManager            resolv.conf          tcsd.conf
    cron.weekly              hosts          networks                  resolv.conf.IOQFK1   telegraf
    crypttab                 hosts.allow    nsswitch.conf             resolv.conf.save     terminfo
    csh.cshrc                hosts.deny     nsswitch.conf.bak         rpc                  timezone
    csh.login                httpd          ntp                       rpm                  tmpfiles.d
    dbus-1                   init.d         ntp.conf                  rsyncd.conf          tuned
    default                  inittab        openldap                  rsyslog.conf         udev
    depmod.d                 inputrc        opt                       rsyslog.d            vconsole.conf
    dhcp                     iproute2       os-release                rwtab                vimrc
    DIR_COLORS               issue          pam.d                     rwtab.d              virc
    DIR_COLORS.256color      issue.net      passwd                    sasl2                wgetrc
    DIR_COLORS.lightbgcolor  kdump.conf     passwd-                   securetty            wpa_supplicant
    docker                   kernel         php.d                     security             X11
    dracut.conf              krb5.conf      php.ini                   selinux              xdg
    dracut.conf.d            krb5.conf.d    pkcs11                    services             xinetd.d
    e2fsck.conf              ld.so.cache    pki                       sestatus.conf        yum
    egl       welcome        ld.so.conf     plymouth                  shadow               yum.conf
    environment              ld.so.conf.d   pm                        shadow-              yum.repos.d
    ethertypeswelcome        libaudit.conf  polkit-1                  shells
    [root@127 etc]# tput sc; tput cup 10 10;echo "welcome"; tput rc
    [root@127 etc]# tput rc
    [root@127 etc]# tput sc; tput cup 10 10;echo "welcome"; tput rc
    [root@127 etc]# tput civis
    [root@127 etc]# tput cnorm
    [root@127 etc]# tput civis
    [root@127 etc]# tput civis
    [root@127 etc]# tput cnorm
    [root@127 etc]# tput blink
    [root@127 etc]# tput bold
    [root@127 etc]# tput rev
    [root@127 etc]# tput -blink
    tput: invalid option -- 'b'
    usage: tput [-V] [-S] [-T term] capname
    [root@127 etc]# tput sgr0
    [root@127 etc]# echo "hello"
    hello
    [root@127 etc]# echo "save screen"
    save screen
    [root@127 etc]# tput smcup
    [root@127 etc]# tput sgr0
    [root@127 etc]# reset
                                                      
    

    相关文章

      网友评论

          本文标题:shell入门17-shell解释器Bash属性设置

          本文链接:https://www.haomeiwen.com/subject/iebqcdtx.html