1.  命令行参数------类似javac  参数1 参数2

类似Java中编译的javac parm1....。在shell中,参数与参数之间用空格隔开。采用位置参数来识别对应的参数值:$0是程序名,$1是第一个参数,以此类推,知道第9个参数$9。对于大于9个参数的需要在变量数字周围添加花括号,比如${10}。

note:命令行上不仅可以处理数值,还可以处理字符串。

 1 [Hermioner@localhost Documents]$ cat test2.sh
 2 #!/bin/bash
 3 total=$[ $1*$2 ]
 4 echo The first parm is $1
 5 echo The second parm is $2
 6 a=$3
 7 echo the third parm is $3
 8 b=$4
 9 echo the forth parm is $4
10 [Hermioner@localhost Documents]$ bash test2.sh 2 3 "hello world" min
11 The first parm is 2
12 The second parm is 3
13 the third parm is hello world
14 the forth parm is min

View Code

1 [Hermioner@localhost Documents]$ cat test3
2 #!/bin/bash
3 echo the tenth parm is ${10}
4 echo the eleventh parm is ${11}
5 [Hermioner@localhost Documents]$  bash test3 1 2 3 4 5 6 7 8 9 10 11
6 the tenth parm is 10
7 the eleventh parm is 11
8 [Hermioner@localhost Documents]$ 

View Code

note: $0返回脚本名,如果用bash,就只返回脚本名;如果./脚本来运行,返回当前路径名;  因此,还可以尝试basename命令来返回不包含路径的脚本名。

2. $#,$*,$@,${!#}

s#      用来统计命令行的参数个数

s*       用来访问所有的命令行参数,并且构成一个字符串整体输出

s@     同s*,只是结果是分散成字符串数组,每个数组中的元素都是一个参数

${!#}   代表的最后一个参数,因为花括号中不可以用$,因此用!来代替它

 1 [Hermioner@localhost Documents]$ cat testfile
 2 #!/bin/bash
 3 echo the "\$*" is  $*
 4 echo the "\$@" is $@
 5 echo the "\$#" is $#
 6 echo the "\${!#}" is ${!#}
 7
 8 [Hermioner@localhost Documents]$ bash testfile a b c d
 9 the $* is a b c d
10 the $@ is a b c d
11 the $# is 4
12 the ${!#} is d

View Code

 1 [Hermioner@localhost Documents]$ cat testfile
 2 #!/bin/bash
 3 echo
 4 count=1
 5 for param in "$*"
 6 do
 7    echo "\$* Parameter #$count = $param"
 8    count=$[ $count+1 ]
 9 done
10
11 echo
12 count=1
13 for param in "$@"
14 do
15    echo "\$@ Parameter #$count = $param"
16    count=$[ $count+1 ]
17 done
18
19 [Hermioner@localhost Documents]$ bash testfile a b c d
20
21 $* Parameter #1 = a b c d
22
23 $@ Parameter #1 = a
24 $@ Parameter #2 = b
25 $@ Parameter #3 = c
26 $@ Parameter #4 = d
27 [Hermioner@localhost Documents]$ 

View Code

3. shift 移动变量  

shift可以用来在不知道有多少参数,以及每个参数的值的情况下进行遍历,因为它始终可以只打印第一个值。默认情况下它会将每个参数变量向左移动一个位置。所以变量$3的值会移动到$2中,$2的值会移动到$1中,而变量$1的值则会被删除(note:$0代表程序吗,不会改变)

也可以shift n 来指定左移动多少个,eg: shift 2   ,则$3的会移动到$1中,这样就可以跳过一些值不遍历了。

1 [Hermioner@localhost Documents]$ cat test3.sh
2 #!/bin/bash
3 echo "the original parameters is $*"
4 shift 2
5 echo "the new first parameter is $1"
6 [Hermioner@localhost Documents]$ bash test3.sh 1 2 3 4 5
7 the original parameters is 1 2 3 4 5
8 the new first parameter is 3
9 [Hermioner@localhost Documents]$

View Code

note:配合shift的使用,同样可以通过shell脚本中的逻辑来判断是选项还是参数,从而让参数得到应有的输出。并在在bash shell中还提供了getopt和getopts来判断是选项还是参数-------用时参考它们用法即可。

4. 获取用户输入-------交互性更强,类似java中的scanner+system.in用法

    采用read命令。read后面跟变量名,就可以将输入的值保存到变量中;如果不输入变量名,那么就自动保存在了特殊环境变量REPLY中。

1 [Hermioner@localhost Documents]$ cat test1
2 #!bin/bash
3 echo -n "Enter your name:"
4 read name
5 echo "hello $name"
6 [Hermioner@localhost Documents]$ bash test1
7 Enter your name:Tom
8 hello Tom
9 [Hermioner@localhost Documents]$ 

View Code

note1:如果用户一直不输入,read会一直等待,因此可以设置计时器,用-t选项。时间过了,就不等了。

eg:read -t 5 name

note2: 类似密码输入,隐藏方式读取,只需要添加 -s就可以

note3: 还可以从文件中读取,一行一行的读取

 1 [Hermioner@localhost Documents]$ cat test1
 2 #!bin/bash
 3 a
 4 b
 5 c
 6 [Hermioner@localhost Documents]$ cat test2
 7 #!/bin/bash
 8 cat test1 | while read line    #采用了管道
 9 do
10     echo "the line is $line"
11 done
12 echo "read is done"
13 [Hermioner@localhost Documents]$ bash test2
14 the line is #!bin/bash
15 the line is a
16 the line is b
17 the line is c
18 read is done
19 [Hermioner@localhost Documents]$ 

View Code

补充管道:

command1 | command2   就是将命令1的输出重定向到了command2中。 可以多级重定向,多添加|就好了。

参考文献

Linux命令行与shell脚本编程大全(第3版)[美] 布鲁姆(Richard Blum),布雷斯纳汉(Christine Bresnahan) 著,门佳,武海峰 译

转载于:https://www.cnblogs.com/Hermioner/p/9383629.html

shell基础05 处理用户输入相关推荐

  1. linux脚本用户输入,如何在Linux shell脚本中提示用户输入

    本篇文章给大家介绍关于如何在Linux shell脚本中提示用户输入?下面来看具体的内容. 我们首先来看一下命令# read var # read -s "Waiting for input ...

  2. linux脚本外输入参数,shell 脚本中关于用户输入参数的处理

    1.命令行参数 向shell脚本传递数据的最基本方式是使用命令行参数. (1) 读取参数 读取输入的参数的变量为位置参数,位置参数通过标准数字表示, 其中$0为程序名称,$1为第一个参数,$2为第二个 ...

  3. shell编程(八) : [shell基础] 处理用户输入

    接上一篇文章shell编程(七) : [shell基础] 使用结构化命令 目录 3.3 处理用户输入 3.3.1 命令行参数 1.位置参数 2.对参数进行测试 3.3.2 特殊参数变量 1.参数个数 ...

  4. Linux shell 学习笔记(10)— 处理用户输入(命令行读取参数、读取用户输入、超时处理)

    1. 命令行参数 向 shell 脚本传递数据的最基本方法是使用命令行参数.命令行参数允许在运行脚本时向命令行添加数据. $ ./addem 10 30 本例向脚本 addem 传递了两个命令行参数( ...

  5. shell脚本编程之处理用户输入

    技术交流QQ群:1027579432,欢迎你的加入! 本教程使用Linux发行版Centos7.0系统,请您注意~ 1.命令行参数 bash shell提供了一些不同的方法来从用户处获得数据,包括命令 ...

  6. 8、python基础知识-用户输入

    #!/usr/bin/env python # _*_ coding: utf-8 _*_ num = 100 name = input("your name:") age = i ...

  7. 【shell】Linux shell 之 判断用户输入的变量是否为数字

    本文内容:判断用户输入的参数是否为数字 在shell中如何进行计算? 方式一 [root@XiaoPeng scripts]# echo $((1+2)) 3 方式二 [root@XiaoPeng s ...

  8. Java黑皮书课后题第5章:*5.30(金融应用:复利值)假设你每月在储蓄账户上多存100美元,年利率为5%,那么每月利率是0.05 / 12 = 0.00417。编写程序提示用户输入数据显示定月钱数

    5.30(金融应用:复利值)假设你每月在储蓄账户上多存100美元,年利率为5%,那么每月利率是0.05 / 12 = 0.00417.编写程序提示用户输入数据显示定月钱数 题目 题目概述 破题 代码 ...

  9. linux 输入是否为数字,【shell】Linux shell 之 判断用户输入的变量是否为数字

    本文内容:判断用户输入的参数是否为数字 在shell中如何进行计算? 方式一 [root@XiaoPeng scripts]# echo $((1+2)) 3 方式二 [root@XiaoPeng s ...

最新文章

  1. 分析了 600 多种烘焙配方,机器学习开发出新品
  2. Java swing 如何将一个按钮放置到弹出框框的任意位置?(Absolute layout 布局的使用)...
  3. python3 读取.plist文件_Python学习笔记 -5 - 文件操作
  4. stm32 systick分析
  5. 如何保存Tensorflow中的Tensor参数,保存训练中的中间参数,存储卷积层的数据
  6. 【测试】ESP32天线信号强度比较,小龟小车A2天线esp32cam板载外置天线测试数据...
  7. 【opencv】17.提取RBG各种颜色c++代码
  8. 如何通过c语言获取ipv6邻居表,急求在vc++6.0中获取IPV6地址的方法,高手请进,谢谢!!...
  9. 《Qt on Android核心编程》夹
  10. [ORGINAL]OOP Panel control design(based on web )
  11. Face-landmarks-detection-benchmark 人脸特征定位网站汇总
  12. C++ passes by reference, Java and Ruby don’t
  13. 谈谈原子变量与锁的性能比较
  14. Maven搭建webService (一) 创建服务端---使用main函数发布服务
  15. xd使用技巧_真香!3个技巧,帮你获得面试机率提升300%
  16. PYTHON网络爬虫大数据朱炯明
  17. Android原生框架--Xui使用
  18. 我的世界1.14java原版命令_我的世界1.14.4第一个预览版发布 添加了debugreport命令...
  19. 判断机器大端还是小端
  20. tomcat监控脚本(监控进程,测试接口,告警动作为发送邮件)

热门文章

  1. 【NC140 排序】手写快速排序
  2. 电影与爆米花(模拟)
  3. PCA算法中样本方差和协方差的无偏估计与n-1的由来
  4. Mysql8秒级加字段_Mysql8.0秒级加字段
  5. android 退出函数,android – 关闭应用程序与退出按钮
  6. ios 代码设置控件宽高比_iOS--利用比例纯代码适配屏幕大小
  7. 创建或更改表 tablename 失败_mysql 创建用户
  8. 修改背景图片_我花了5小时,为网易修改了一份内容超多的PPT,效果超级赞!!...
  9. 终于,我读懂了所有Java集合——map篇(多线程)
  10. android7.0提示定位,解决android7.0上某些PopuWindow显示位置不正确的问题