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命令接收标准输 ...
随机推荐
- python 目录管理与文件管理
目录管理(os) system:执行系统命令 # 执行系统命令 os.system('cls') name:获取操作系统名称 # 操作系统名称,nt代表Windows, posix代表类unix pr ...
- 改变font-weight的数值,样式并不会改变的原因
通常情况下,一个特定的字体仅会包含少数的可用字重.若所指定的字重不存在直接匹配,则会通过字体匹配算法规则匹配使用邻近的可用字重.这也就是为什么我们有时候使用特定字重时没有“生效”,看起来跟其它字重差不 ...
- 在 React 组件中使用 Refs 指南
原文:Fullstack React's Guide to using Refs in React Components作者:Yomi Eluwande译者:博轩 译文:https://segment ...
- Java内存泄漏真实案例
内存泄漏:当不再需要一个对象时,垃圾收集器会回收它:如果不需要的对象一直在产生而不被收回,就称作“内存泄漏”. 以下为本人在工作中遇到的内存泄漏的案例: 1.对于大量的请求,使用了Executors. ...
- wampserver You don't have permission to access / on this server. 解决方法
最近在安装最近版wampserver 2.2 d时发现安装好后启动服务器,访问localhost显示You don't have permission to access / on this serv ...
- 标准C语言(12)
一个存储区的地址应该是它自身大小的整数倍(双精度浮点类型存储区的地址只需要是4的整数倍),这个规则叫数据对齐,结构体内部的存储区通常也需要遵守数据对齐的规则,数据对齐有可能导致结构体相邻子存储区之间有 ...
- Lucky HDU - 5213 (莫队,容斥)
WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...
- Codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) E.Trips
比赛的时候想到怎么做了 没调出来(感觉自己是个睿智) 给你N个点M条边,这M条边是一条一条加进去的 要求你求出加入每一条边时图中极大'K度'子图的大小 极大'K度'子图的意思是 要求出一个有尽量多的点 ...
- Summer training round2 #5 (Training #21)
A:正着DFS一次处理出每个节点有多少个优先级比他低的(包括自己)作为值v[i] 求A B 再反着DFS求优先级比自己高的求C #include <bits/stdc++.h> #incl ...
- java 获取bean的方式
我们知道可以通过ApplicationContext的getBean方法来获取Spring容器中已初始化的bean.getBean一共有以下四种方法原型: l getBean(String name) ...