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:
空格也作为一行显示出来了。
shell初级-----处理用户输入的更多相关文章
- shell编程中用户输入处理(shell 04)
shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...
- shell中处理用户输入
1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...
- Linux&shell之处理用户输入
写在前面:案例.常用.归类.解释说明.(By Jim) 命令行参数$1为第一个参数,$2为第二个参数,依次类推...示例: #!/bin/bash # using one command line p ...
- Linux - 简明Shell编程13 - 用户输入(UserInput)
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 1 - arguments #!/bin/bash i ...
- Shell - 简明Shell入门13 - 用户输入(UserInput)
示例脚本及注释 1 - arguments #!/bin/bash if [ -n "$1" ];then # 验证参数是否传入 echo "The first para ...
- Shell 脚本处理用户输入
传递参数 跟踪参数 移动变量 处理选项 将选项标准化 获得用户的输入 bash shell提供了一些不同的方法来从用户处获取数据,包括命令行参数(添加在命令后数据),命令行选项(可以修改命令行为的单个 ...
- Shell 语法之用户输入
bash shell 提供了一些不同的方法从用户处获取数据,这些方法包括命令行参数.命令行选项和直接读取键盘输入. 命令行参数 bash shell 将在命令行中输入的所有参数赋值给一些特殊变量,称为 ...
- Linux shell脚本读取用户输入的参数
新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...
- shell获取用户输入
主题: 再学shell之获取用户输入echo -n(不换行)和read命令-p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 和“读文件”深入学习 1.基本读取read命令接收标准输 ...
随机推荐
- mybatis原理解析
本文是结合spring-mybatis整合进行的分析 1.先看看依赖的jar包: <dependency> <groupId>org.mybatis</groupId&g ...
- 微信小程序多video播放暂停问题
<swiper class="swiper" indicator-dots="{{indicatorDots}}" autoplay="{{fl ...
- 让虚拟机中的MySQL可以被主机访问
1.VirtualBox对Ubuntu系统进行设置,“网络”选择“桥接网卡” 2.安装MYSQL服务器 sudo su; apt-get intall mysql-server 3.赋予远程访问的 ...
- cassandra查询效率探讨
cassandra目前提倡的建表与查询方式为CQL方式,传统的cassandra-cli相关的api由于性能问题将逐步淘汰,而cassandra-cli也将在2.2版本之后被淘汰. 在CQL中,可以利 ...
- logging:不喜欢写日志可不好哦
logging模块简介 logging模块是python内置的标准模块,主要用于输出程序的运行日志. 可以设置输出日志的等级,日志保存路径,日志文件回滚等等. logging模块的基本使用 impor ...
- 四,k8s集群资源清单定义入门
目录 资源对象 创建资源的方法 清单帮助命令 创建测试清单 资源的三种创建方式 资源对象 workload:Pod, ReplicaSet, Deployment, StatefulSet, Daem ...
- zabbix 邮件报警事件:Zabbix discoverer processes more than 75% busy
Problem has been resolved at :: on Problem name: Zabbix discoverer processes more than % busy Host: ...
- Cocos Creator 热更新文件MD5计算和需要注意的问题
Creator的热更新使用jsb.热更新基本按照 http://docs.cocos.com/creator/manual/zh/advanced-topics/hot-update.html?h=% ...
- Django学习系列11:在服务器中处理POST请求
之前的代码还没有为表单指定action=属性,因此提交表单默认返回之前渲染的页面,即“/”,这个由视图函数home_page处理.下面修改这个视图函数,让它能处理POST请求. 这意味着要为视图函数h ...
- IDEA查看JDK源代码
之前已经讲解过如何使用Eclipse查看源代码,IDEA作为一个集成开发环境越来越流行,今天学习以下如何使用Eclipse查看JDK的代码. File->Project Structure,选择 ...