shell处理用户输入

墨蓝 2022-03-10 14:12 326阅读 0赞

文章目录

  • 命令行参数
    • 读取参数
    • 读取脚本名
    • 确定参数是否存在
  • 特殊参数变量
    • 参数统计: $#
    • 抓取所有参数: $ *和$@
  • 移动变量:shift
  • 处理选项
    • 查找选项
      • 处理简单选项
      • 分离参数和选项
      • 处理带值的选项
    • getopt命令
  • 用户输入
    • read

命令行参数

读取参数

bash shell会将一些称为位置参数的特殊变量分配给输入到命令行中的所有参数。这也包括shell所执行的脚本名称。位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2是第二个参数,依次类推,直到第九个参数$9

  1. #!/bin/bash
  2. echo "The zero parameter is $0"
  3. echo "The first parameter is $1"
  4. echo "The second parameter is $2"
  5. echo "The third parameter is $3"

执行脚本

  1. $ bash scipth.sh 1 2 3
  2. The zero parameter is scipth.sh
  3. The first parameter is 1
  4. The second parameter is 2
  5. The third parameter is 3
  6. $ bash scipth.sh "1 2 3" "11 22 33" '111 222 333'
  7. The zero parameter is scipth.sh
  8. The first parameter is 1 2 3
  9. The second parameter is 11 22 33
  10. The third parameter is 111 222 333

如果脚本需要的命令行参数不止9个,你仍然可以处理,但是需要稍微修改一下变量名。在
第9个变量之后,你必须在变量数字周围加上花括号,比如${10}。

  1. #!/bin/bash
  2. echo "The 10 parameter is ${10}"
  3. echo "The 11 parameter is ${11}"

执行脚本

  1. $ bash scipth.sh 1 2 3 4 5 6 7 8 9 10 11
  2. The 10 parameter is 10
  3. The 11 parameter is 11

读取脚本名

$0参数获取shell在命令行启动的脚本名,basename命令会返回不包含路径的脚本名。

  1. #!/bin/bash
  2. echo "The 0 parameter is ${0}"
  3. name=$(basename $0)
  4. echo "the shell name is " $name

执行脚本

  1. $ bash /home/ocean/workspace/scipth.sh
  2. The 0 parameter is /home/ocean/workspace/scipth.sh
  3. the shell name is scipth.sh

确定参数是否存在

在使用参数之前一定要检查参数是否存在

  1. #!/bin/bash
  2. if [ -n "$1" ]
  3. then
  4. echo "Hello $1"
  5. else
  6. echo "please input ont paremeter"
  7. fi

特殊参数变量

参数统计: $#

  1. #!/bin/bash
  2. if [ $# -ne 2 ]
  3. then
  4. echo
  5. echo Usage: test9.sh a b
  6. echo
  7. else
  8. total=$[$1 + $2 ]
  9. echo $total
  10. fi
  • 找出最后一个参数

    !/bin/bash

    params=$#
    echo
    echo “The last parameter is $params”
    echo “The last parameter is ${!#}”

执行脚本

  1. $ bash scipth.sh
  2. The last parameter is 0
  3. The last parameter is scipth.sh
  4. $ bash scipth.sh 1
  5. The last parameter is 1
  6. The last parameter is 1

抓取所有参数: $ *和$@

  1. $ bash scipth.sh 1 2 3 4 5
  2. Using the $* method: 1 2 3 4 5
  3. Using the $@ method: 1 2 3 4 5
  4. $* param #1 = 1 2 3 4 5
  5. $@ param #1 = 1
  6. $@ param #2 = 2
  7. $@ param #3 = 3
  8. $@ param #4 = 4
  9. $@ param #5 = 5

移动变量:shift

在使用shift命令时,默认情况下它会将每个参数变量向左移动一个位置。所以,变量$3
的值会移到$2中,变量$2的值会移到$1中,而变量$1的值则会被删除(注意,变量$0的值,也就是程序名,不会改变)。

  1. #!/bin/bash
  2. echo
  3. count=1
  4. while [ -n "$1" ]
  5. do
  6. echo "Parameter #$count=$1"
  7. count=$[ $count + 1 ]
  8. shift
  9. done

备注:shift n 指定跳过n个参数。

处理选项

查找选项

处理简单选项

  1. #!/bin/bash
  2. echo
  3. count=1
  4. while [ -n "$1" ]
  5. do
  6. case "$1" in
  7. -a) echo "-a option";;
  8. -b) echo "-b option";;
  9. -c) echo "-c option";;
  10. *) echo "no $1 option"
  11. esac
  12. shift
  13. done

分离参数和选项

shell会用双破折线来表明选项列表结束。

  1. #!/bin/bash
  2. echo
  3. count=1
  4. while [ -n "$1" ]
  5. do
  6. case "$1" in
  7. -a) echo "-a option";;
  8. -b) echo "-b option";;
  9. -c) echo "-c option";;
  10. --) shift
  11. break;;
  12. *) echo "$1 is not a option";;
  13. esac
  14. shift
  15. done
  16. count=1
  17. for param in $@
  18. do
  19. echo "Parameter #$count:$param"
  20. count=$[ $count+1 ]
  21. done

处理带值的选项

  1. #!/bin/bash
  2. count=1
  3. while [ -n "$1" ]
  4. do
  5. case "$1" in
  6. -a) echo "-a option";;
  7. -b) echo "-b option with value $2"
  8. shift;;
  9. -c) echo "-c option";;
  10. --) shift
  11. break;;
  12. *) echo "$1 is not a option";;
  13. esac
  14. shift
  15. done

执行脚本

  1. $ bash scipth.sh -a -a -a -a -b 222 -a
  2. -a option
  3. -a option
  4. -a option
  5. -a option
  6. -b option with value 222
  7. -a option

getopt命令

  • 如何使用

getopt将参数格式化成固定的格式,其使用方法是

  1. getopt optstring parameters

例子:

  1. $ getopt ab:cd -a -b test1 -cd test2 test3
  2. -a -b test1 -c -d -- test2 test3

optstring定义了四个有效选项字母:a、b、c和d。冒号(:)被放在了字母b后面,因为b选项需要一个参数值。当getopt命令运行时,它会检查提供的参数列表(-a -b test1 -cd test2 test3),并基于提供的optstring进行解析。注意,它会自动将-cd选项分成两个单独的选项,并插入双破折线来分隔行中的额外参数。

用户输入

read

  1. #!/bin/bash
  2. echo -n "Enter your name:" #echo -n不会在字符串末尾输出换行符
  3. read name
  4. echo "Hello $name"
  • read 指定变量

    !/bin/bash

    read -p “Enter your name:” name
    echo “Hello $name”

    !/bin/bash

    read -p “Enter your name:” first last
    echo “Hello $first $last”

  • read不指定变量时,会存储到特殊环境变量REPLY中

    !/bin/bash

    read -p “Enter your name:”
    echo “Hello $REPLY”

  • read -t 等待时间。如果计时器过期,会返回一个非0退出码

    in/bash
    if read -t 5 -p “Please enter your name: “ name
    then

    1. echo "hello $name"

    else

    1. echo "too late"

    fi

  • read -n 输入字符个数。当输入的字符达到预设的字符数时,就自动退出,将输入的数据赋给变量

    !/bin/bash

    read -n1 -p “Do you want to continue [Y/N]?” answer
    case $answer in
    Y | y) echo

    1. echo "contine on ..";;

    N | n) echo

    1. echo "byebye!"
    2. exit;;

    esac
    echo “End”

  • read -s输入密码(实际上,数据会被显示,只是read命令会将文本颜色设成跟背景色一样)。

    !/bin/bash

    read -s -p “Enter your password: “ pas
    echo “password:$pas”

  • 通过管道 从文件中读取

每次调用read命令,它都
会从文件中读取一行文本。当文件中再没有内容时,read命令会退出并返回非零退出状态码。

  1. #!/bin/bash
  2. count=1
  3. cat out.txt | while read line
  4. do
  5. echo "Line $count:$line"
  6. count=$[ $count + 1]
  7. done

读书笔记

发表评论

表情:
评论列表 (有 0 条评论,326人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue处理用户输入

    处理用户输入   为了让用户和你的应用进行交互,我们可以用 v-on 指令添加一个事件监听器,通过它调用在 Vue 实例中定义的方法: <div id="app