zoukankan      html  css  js  c++  java
  • shell初级-----处理用户输入

    命令行参数

     读取参数

    位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2,是第二个参数,直到第九个参数$9。

    特殊的变量:$#表示参数个数,$?表示最后运行的命令的结束代码(返回值)

    每个参数必须用空格分开。当然如果要在参数中引用空格必须加引号。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    sum=$[ $1 + $2 ]
    echo "first num is $1"
    echo "second num is $2"
    echo "sum num is $sum"
    [root@node1 ljy]# sh ceshi.sh 2 3
    first num is 2
    second num is 3
    sum num is 5
    

    读取脚本名

    $0可以获取shell在命令行启动的脚本名

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    echo "this script name is $0"
    [root@node1 ljy]# sh ceshi.sh 
    this script name is ceshi.sh
    

    如果使用另一些命令执行脚本,可能命令会与脚本名混在一起。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    echo "this script name is $0"
    [root@node1 ljy]# sh /ljy/ceshi.sh 
    this script name is /ljy/ceshi.sh
    [root@node1 ljy]# ./ceshi.sh 
    this script name is ./ceshi.sh
    

    basename命令可以返回不包含路径的脚本名

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    name=$(basename $0)
    echo "this script name is $name"
    [root@node1 ljy]# sh /ljy/ceshi.sh 
    this script name is ceshi.sh
    [root@node1 ljy]# ./ceshi.sh 
    this script name is ceshi.sh
    

    测试参数

    如果你要使用命令行参数,而不小心漏了加,可能就要报错了,

    所以最好加一个测试

    -n测试来检查命令行参数是否有数据。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    if [ -n "$1" ] && [ -n "$2" ]
    then
       sum=$[ $1 + $2 ]
       echo "first num is $1"
       echo "second num is $2"
       echo "sum num is $sum"
    else
       echo "you should identify yourself!"
    fi
    [root@node1 ljy]# sh ceshi.sh 1 2
    first num is 1
    second num is 2
    sum num is 3
    [root@node1 ljy]# sh ceshi.sh 1  
    you should identify yourself!
    

    特殊参数变量

    参数统计

    $#含有脚本运行时携带的命令行参数的个数。可以在脚本中任何地方使用这个变量。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    echo there were $# parameters
    [root@node1 ljy]# sh ceshi.sh  1 2 2 3 
    there were 4 parameters
    

    if-then语句常用-ne来测试命令行参数数量。

    抓取所有数据

    $*和$@会将命令行提供的所有参数作为一个单词保存。

    $@变量会将所有参数当做一个字符串的多个独立的单词。

    $*变量会将所有参数当成单个参数。

    获得用户输入

    基本读取

    read命令从标准输入或者另一个文化描述符中接受输入,收到输入后,read命令会将数据放在一个变量里。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    echo -n "enter your name:"
    read name
    echo hello $name
    [root@node1 ljy]# sh ceshi.sh 
    enter your name:ljy
    hello ljy
    

    -n选项不会在字符末尾输出换行符,允许用户紧跟其后的输入数据。

    -p命令允许你输入提示符:

    超时

    使用read命令可能会导致程序一直等待中。

    你可以使用-t选项来指定一个定时器。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    if read -t 5 -p "enter your name:" name
    then 
       echo "your name is $name"
    else
       echo 
       echo "sorry,slow"
    fi
    [root@node1 ljy]# sh ceshi.sh 
    enter your name:
    sorry,slow
    

    隐秘读取

    -s选项可以避免输入的内容显示在屏幕上

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    read  -s -p "enter your name:" name
    echo
    echo "your name is $name"
    [root@node1 ljy]# sh ceshi.sh 
    enter your name:
    your name is ljy
    

    从文件中读取

    每次调用一次read命令,都会从文件中读取一行数据,一直到没有内容的时候,read命令会退出并返回非零退出码。

    [root@node1 ljy]# more ceshi.sh 
    #!/bin/bash
    count=1
    cat test.txt | while read line
    do
      echo line:$line
    done
    [root@node1 ljy]# sh ceshi.sh 
    line:1
    line:2
    line:3
    line:4
    line:5
    line:
    

    空格也作为一行显示出来了。

  • 相关阅读:
    每日一题力扣520 判断大小写
    每日一题力扣434
    每日一题力扣557
    每日一题力扣344 反转字符串
    每日一题力扣125 回文字符串
    每日一题力扣541
    每日一题力扣58
    【20211015】项目成本粗略分析
    【20211013】连岳摘抄
    【20211014】自己就是解决问题的起点
  • 原文地址:https://www.cnblogs.com/jinyuanliu/p/10897848.html
Copyright © 2011-2022 走看看