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 DLib库进行人脸关键点识别
一.首先安装DLib模块 这里只介绍linux安装的过程,windows安装过程请自行百度 1.首先,安装dlib.skimage前:先安装libboost sudo apt-get install ...
- 数据库学习笔记3 基本的查询流 2 select lastname+','+firstname as fullname order by lastname+','+firstname len() left() stuff() percent , select top(3) with ties
数据库学习笔记3 基本的查询流 2 order by子句对查询结果集进行排序 多列和拼接 多列的方式就很简单了 select firstname,lastname from person.pers ...
- c++ 递归算法实现排列组合
通过引用的方式来传值,具体的实现的方法如下 void pc(int m,int n,int &position,int (&a)[100]) { //如果运算得到那个数 if (pos ...
- QT Qdialog的对话框模式以及其关闭
模式对话框 描述 阻塞同一应用程序中其它可视窗口输入的对话框.模式对话框有自己的事件循环,用户必须完成这个对话框中的交互操作,并且关闭了它之后才能访问应用程序中的其它任何窗口.模式对话框仅阻止访问与对 ...
- cgicc使用
CgiCc 使用 一.下载 下载地址: http://ftp.gnu.org/gnu/cgicc/ ftp://ftp.gnu.org/gnu/cgicc/ 二.配置.编译.安装 下载完成后解压:sh ...
- Spring Boot热部署插件
在实际开发中,我们修改某些代码逻辑功能或页面都需要重启应用,这无形中降低了开发效率,热部署是指当我们修改代码后,服务能自动重启加载新修改的内容,而不需要重启应用,这样大大提高了我们开发的效率. Spr ...
- 如何使用git回退部分修改(转)
如何使用git回退部分修改(转) 很多时候,git新手容易误操作,比如,在levelIISZ-1.4.dev分支下,运行了git pull idc cpp-1.0的结果,这样做麻烦很大,经常导 ...
- VAssistX 常用快捷键
函数跳转 Alt + G - 函数定义和声明的跳转Alt + O - 在.h与.cpp文件中实现相互转换Alt + M - 列出当前文件所有的函数Ctrl + Tab - 切换标签 查找 Ctrl + ...
- Springboot设置session超时时间
按优先级高到低说: 第一种: spring boot 启动类里面: package com.mycenter; import org.mybatis.spring.annotation.MapperS ...
- linux中more命令如何使用
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上.兄弟连Linux培训教程() more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(sp ...