• shell编程中用户输入处理(shell 04)


    shell编程中用户输入处理
    1.命令行参数
    2.脚本运行时获取输入

    命令行参数 通过空格来进行分割的
    位置参数 :$+position $0,$1,$2 ....
    $0 :程序名
    $1,$2,$3 ... $9
    10及其以上的
    ${10}

    add.sh

    #/bin/bash
    echo "file is $0"
    echo "1->$1"
    echo "2->$2"
    echo "10->${10}"
    echo "11->${11}"

     ./add.sh 1 2 3 4 5 6 7 8 9 10 11

    file is ./add.sh
    1->1
    2->2
    10->10
    11->11

    $0表示 命令行输入的

    /root/sh/f.sh

    #! /bin/bash
    echo `basename $0`
    echo `dirname $0`
    [root@localhost110 sh]# /root/sh/f.sh
    f.sh
    /root/sh

    calc.sh

    #! /bin/bash
    
    name=`basename $0`
    
    if [ $name = "add" ]
    then
            result=$[$1+$2]
    elif [ $name="minus" ]
    then
            result=$[$1-$2]
    fi
    echo "the $name result is $result"

    注意if 后的[]与变量之间必须有空格

    chmod u+x calc.sh

    ln -s calc.sh add
    ln -s calc.sh minus

    执行命令

     ./add 1 2
    the add result is 3
     ./minus 5 1
    the minus result is 4

    命令行参数-特殊变量
    1.参数计数(参数个数):$#
    2.所有参数: $*
    3.参数列表: $@

    test.sh

    #! /bin/bash
    
    echo $#
    echo $*
    echo $@
    echo "#######################"
    for var in "$*"
    do
            echo "$* param=$var"
    done
    
    echo "########################"
    
    for var in "$@"
    do
            echo "$@ param=$var"
    done

    执行结果

    [root@localhost110 sh]# ./test.sh 1 2 js php
    4
    1 2 js php
    1 2 js php
    #######################
    $* param=1 2 js php
    ########################
    $@ param=1
    $@ param=2
    $@ param=js
    $@ param=php

    $$

    Shell本身的PID(ProcessID)

    $!

    Shell最后运行的后台Process的PID

    $?

    最后运行的命令的结束代码(返回值)

    $-

    使用Set命令设定的Flag一览

    $*

    所有参数列表 如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。

    $@

    所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数

    $#

    添加到Shell的参数个数

     

    $0

    Shell本身的文件名

     

    $1~$n

    Shell文件第1到第n个参数,大于9的用${n}

    $?
    成功返回0 否者返回大于0 的

    [root@web ~]# ll
    总用量 24
    -rw-------. 1 root root  1331 7月   2 2016 anaconda-ks.cfg
    drwxr-xr-x. 2 root root    50 3月  14 20:32 c
    ..............................................
    [root@web ~]# echo $?
    0
    [root@web ~]# l
    -bash: l: 未找到命令
    [root@web ~]# echo $?
    127
    [root@web ~]# echo 'php'|grep a
    [root@web ~]# echo $?
    1
    [root@web ~]# echo 'php'|grep h
    php
    [root@web ~]# echo $?
    0
    View Code

    在/etc/profile里有这段代码

    for i in /etc/profile.d/*.sh ; do
        if [ -r "$i" ]; then
            if [ "${-#*i}" != "$-" ]; then 
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done
    [root@web sh]# echo $-
    himBH

    #表示从左到右匹配
    ## 表示从左到右匹配 贪婪匹配

    [root@web sh]# a=1.tar.gz
    [root@web sh]# echo ${a#*.}
    tar.gz
    [root@web sh]# echo ${a##*.}
    gz

    对于一个普通的文件

    echo ${-#*/root/sh/until.sh}

    himBH
    表示从左到右去掉**/root/sh/until.sh 匹配到的内容
    $-记录着当前设置的shell选项,himBH是默认值,可以通过 set 命令来设置或者取消一个选项配置

    [root@web sh]# set -x
    [root@web sh]# echo $-
    + echo himxBH
    himxBH

    x表示shell的调试开关 ,和sh -x 方式执行shell脚本等同
    多了x选项
    set -o 或者set +o 查看所有选项

    [root@web sh]# 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@web sh]# set +o
    set +o allexport
    set -o braceexpand
    set -o emacs
    set +o errexit
    set +o errtrace
    set +o functrace
    set -o hashall
    set -o histexpand
    set -o history
    set +o ignoreeof
    set -o interactive-comments
    set +o keyword
    set -o monitor
    set +o noclobber
    set +o noexec
    set +o noglob
    set +o nolog
    set +o notify
    set +o nounset
    set +o onecmd
    set +o physical
    set +o pipefail
    set +o posix
    set +o privileged
    set +o verbose
    set +o vi
    set +o xtrace
    View Code

    -表示打开的选项
    查看打开的选项

    [root@web sh]# set -o|awk '$2=="on" {print $1}'
    braceexpand
    emacs
    hashall
    histexpand
    history
    interactive-comments
    monitor
    或者
    [root@web sh]# set +o|awk '$2=="-o" {print $3}'
    braceexpand
    emacs
    hashall
    histexpand
    history
    interactive-comments
    monitor
    View Code

    i - interactive
    包含这个选项说明当前的 shell 是一个交互式的 shell,何为交互式
    你输入命令,shell 解释执行后给你返回结果,我们在 Terminal 下使用的 shell 就是交互式的
    所以 $- 会包含 i 字符
    如果我们在一个脚本里面 echo $-,结果是不会包含 i 的

    [root@web sh]# cat tmp.sh 
    #! /bin/bash
    echo $-;
    [root@web sh]# sh tmp.sh 
    hB
    [root@web sh]# echo $-
    himBH
    View Code

    H - history

    这个很多人都基本上不用, Shell 会把我们执行的命令记录下来,可以通过 history 命令查看,每一行是序号 + 执行的命令。
    在 shell 退出时,会将这些信息保存到~/.bash_history 文件中,当然在启动时也会从该文件中加载,不信删除这个文件再打开一个终端试试。
    history 就是展开历史列表中的命令,可以通过!感叹号来完成,例如"!!"返回上最近的一个历史命令,"!n"返回第 n 个历史命令
    set +o history
    关闭了history后history里不会再有新内容了

    B - braceexpansion
    Braceexpansion 是一个很有用的技巧 cp或者mv时
    [root@web sh]# cp add.sh{,.20170316}

    会自动备份 add.sh.20170316
    可以通过set +B来关闭这个功能,与前面几个选项不同的是,这个选项在脚本里面也是默认打开的

    m - monitor
    字面意思是说打开监控模式,Bash 手册上后面还有一句话"Job control is enabled",Job control 是什么?就是说可以控制进程的停止、继续,后台或者前台执行等。
    正常情况下,在交互式模式下,该选项默认是打开的,所以再执行一个比较耗时的命令时,
    你可以按下CTRL+Z 让它在后台运行,然后可以用 fg 命令将后台运行的任务恢复到前台执行

    [root@web sh]# sleep 10
    ^Z
    [1]+  已停止               sleep 10
    [root@web sh]# jobs
    [1]+  已停止               sleep 10
    [root@web sh]# fg 1
    sleep 10
    h - hashall
    打开这个选项后,Shell 会将命令所在的路径记录下来,避免每次都要查询
    在/usr/bin 下创建任意一个可执行的文件:
    [root@web sh]# cat /usr/bin/hk
    #! /bin/bash
    echo $0
    [root@web sh]# chmod u+x /usr/bin/hk
    [root@web sh]# hk
    /usr/bin/hk
    [root@web sh]# mv /usr/bin/hk  ~/sh
    [root@web sh]# hk
    -bash: /usr/bin/hk: 没有那个文件或目录
    View Code
  • 相关阅读:
    51nod-1462: 树据结构
    51nod-1363: 最小公倍数之和
    jar包反编译
    js表格某列多行值相同进行行合并
    JS对象转URL参数
    json数组转字符串 前端与后端交互
    element-ui 表格数据根据某一列值相同进行行合并(包括序号列)并同列相同值进行合计
    ssh框架中联合查询所取结果不在单一实体,sql写法
    ajax异步获取数据后动态向构建表格并添加数据
    关于ajax中async: false的作用
  • 原文地址:https://www.cnblogs.com/HKUI/p/6011931.html
Copyright © 2020-2023  润新知