Linux&shell之处理用户输入
写在前面:案例、常用、归类、解释说明。(By Jim)
命令行参数
$1为第一个参数,$2为第二个参数,依次类推...
示例:
#!/bin/bash
# using one command line parameter factorial=
for((number = ;number<=$;number++))
do
factorial=$[ $factorial*$number ]
done
echo The factorial of $ is $factorial
调用
./test1 5
(这样就把参数传递进去了)
结果:
The factorial of 5 is 120
#!/bin/bash
# testing parameters before use if [ -n "$" ]
then
echo Hello $,glad to meet you.
else
echo "Sorry,you didn't identify yourself."
fi
(最好能验证一下是否有参数传入,这样最安全了,编程时也要这样处理)
获取所有数据
$* 所有参数作为一个单词处理
$@ 所有参数作为多个单词处理
$# 最后一个参数
#!/bin/bash
# testing $* and $@ echo "Using the \$* method:$*"
echo "Using the \$@ method:$@" ./test1 rich jessica tom
结果:
Using the $* method:rich jessica tom
Using the $@ method:rich jessica tom
貌似没差别,不过我们来看一个例子,就知道差别了
#!/bin/bash
# testing $* and $@ count=
for param in "$*"
do
echo "\$* Parameter #$count = $param"
count=$[ $count + ]
done count=
for param in "$@"
do
echo "\$@ Parameter #$count = $param"
count=$[ $count + ]
done
测试:test1 1 2 3 4 5
结果:
$* Parameter #1 = 1 2 3 4 5
$@ Parameter #1 = 1
$@ Parameter #2 = 2
$@ Parameter #3 = 3
$@ Parameter #4 = 4
$@ Parameter #5 = 5
(由结果就能看出差别了)
移位
shift将参数变量左移一个位置,于是,变量$3的值就移给变量$2,变量$2的值移给变量$1,变量$1的值被丢弃。
#!/bin/bash
# demonstrating the shift command count=
while [ -n "$" ]
do
echo "Parameter #$count = $"
count=$[ $count + ]
shift
done
执行:
test1 1 2 3 4 5
结果:
Parameter #1 = 1
Parameter #2 = 2
Parameter #3 = 3
Parameter #4 = 4
Parameter #5 = 5
(每次向前移一位,最后被置空了)
shift 2表示移动两位
处理选项
#!/bin/bash
# extracting options and parameters while [ -n "$" ]
do
case "$" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
--) shift
break;;
*) echo "$ is not an option" ;;
esac
shift
done count=
for param in $@
do
echo "Parameter #$count:$param"
count=$[ $count + ]
done
测试:
test1 -a -b -c test1 test2 -c
结果:
Found the -a option
Found the -b option
Found the -c option
test1 is not an option
test2 is not an option
Found the -c option
(每一个参数都遍历一次)
测试2:test1 -a -b -c -- test1 test2 -a
结果:
Found the -a option
Found the -b option
Found the -c option
Parameter #1:test1
Parameter #2:test2
Parameter #3:-a
(跳出循环,并将剩余的参数存入$@中去,供下面的代码使用,即便有-a出现也不会被发现了)
使用getopt命令
getopt -q ab:cd -a -b -test1 -cde test2 test3
(表示有a,b,c,d,其中b后面跟一个参数)
解析的结果为
-a -b '-test1' -c -d -- 'test2' 'test3'
getopt -q ab:cd -a -b -test1 -d -c -cd test2 -c test3
解析结果为
-a -b '-test1' -d -c -c -d -c -- 'test2' 'test3'
(由此可以看出,它会将所有的选项与参数分离,按照一定的顺序,之间会自动加上--)
来看一段完整的代码
#!/bin/bash
# extracting options and parameters
set -- `getopt -q ab:c "$@"`
while [ -n "$" ]
do
case "$" in
-a) echo "Found the -a option" ;;
-b) param="$"
echo "Found the -b option,with parameter value $param"
shift;;
-c) echo "Found the -c option" ;;
--) shift
break;;
*) echo "$ is not an option" ;;
esac
shift
done count=
for param in $@
do
echo "Parameter #$count:$param"
count=$[ $count + ]
done
测试:test1 -a -b test1 -cd test2 test3
结果:
Found the -a option
Found the -b option,with parameter value 'test1'
Found the -c option
Parameter #1:'test2'
Parameter #2:'test3'
(可以想象它就是按照解析的数据进行处理的)
getopts和它的堂兄弟getopt很相像。
案例:
#!/bin/bash
# simple demonstration of the getopts command while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option,with parameter value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option:$opt" ;;
esac
done
(它会自动处理移位,自动处理参数到变量$OPTARG,自动处理)
测试:
test1 -ab test1 -c -d
结果:
Found the -a option
Found the -b option,with parameter value test1
Found the -c option
Unknown option:?
getopts命令每个处理选项,环境变量OPTIND的值会加1,。当到达getopts处理的末尾时,可以使用shift命令
和OPTIND值进行操作来移动到参数。
看代码:
#!/bin/bash
# simple demonstration of the getopts command while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option,with parameter value $OPTARG";;
c) echo "Found the -c option" ;;
d) echo "Found the -d option" ;;
*) echo "Unknown option:$opt" ;;
esac
done shift $[ $OPTIND - ] count=
for param in "$@"
do
echo "Parameter $count : $param"
count=$[ $count+ ]
done
测试:test1 -a -b test1 -cd test2 test3
结果:
Found the -a option
Found the -b option,with parameter value test1
Found the -c option
Found the -d option
Parameter 1 : test2
Parameter 2 : test3
获取用户输入
基本读取
read命令接受标准输入,得到输入后,read命令将数据存放到一个标准变量中。
案例:
#!/bin/bash
# testing read echo "Enter your name:"
read name
echo "Welcome ,$name !"
测试:
test1
Enter your name:
jim
Welcome ,jim !
#!/bin/bash
# testing read echo -n "Enter your name:"
read name
echo "Welcome ,$name !"
测试:
[root@localhost shellscript]# test1
Enter your name:jim
Welcome ,jim !
(抑制后面新的字符出现,-n就不会换行了)
read有个-p命令,允许在read命令行中直接指定一个提示:
#!/bin/bash
# testing read -p option read -p "Please enter your age:" age
days=$[ $age * ]
echo "That makes you over $days days old!"
测试:
[root@localhost shellscript]# test1
Please enter your age:25
That makes you over 9000 days old!
(25是输入的)
Linux&shell之处理用户输入的更多相关文章
- Linux shell脚本读取用户输入的参数
新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...
- shell编程中用户输入处理(shell 04)
shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...
- Linux - 简明Shell编程13 - 用户输入(UserInput)
脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 1 - arguments #!/bin/bash i ...
- [转]linux shell数据重定向(输入重定向与输出重定向)详细分析
在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件 ...
- shell中处理用户输入
1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...
- Linux shell简单创建用户脚本
前面介绍简单的shell编写规则. 现在开始编写一个简单的shell脚本. Linux shell介绍 编写shell脚本 1.创建脚本文件 2.根据需求,编写脚本 3.测试执行脚本 ...
- 自学Linux Shell13.3-获得用户输入(read命令)
Bash shell提供了一些不同的方法来从用户处获得数据,包括以下3中方法: 命令行参数(添加在名利后面的数据) 命令行选项(可修改命令行为的单个字母)主要getopt.getopts命令 直接从键 ...
- shell初级-----处理用户输入
命令行参数 读取参数 位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2,是第二个参数,直到第九个参数$9. 特殊的变量:$#表示参数个数,$?表示最后运行的命令的结束代码(返回值) 每个 ...
- linux shell数据重定向(输入重定向与输出重定向)详细分析
linux shell下常用输入输出操作符是: 1. 标准输入 (stdin) :代码为 0 ,使用 < 或 << : /dev/stdin -> /proc/self/fd/ ...
随机推荐
- 排序(5)---------高速排序(C语言实现)
继shell发明了shell排序过后呢,各位计算机界的大牛们又開始不爽了,为什么他能发明.我就不能发明呢.于是又有个哥们蹦出来了.哎...那么多排序,就木有一个排序是中国人发明的.顺便吐槽一下,一百年 ...
- Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触
第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...
- Strust2 <c:forEach> 循环控制标签
<c:forEach>为循环控制标签 语法:迭代一集合对象中的所有成员 <c:forEach [var="varName"] items="collec ...
- Python中利用函数装饰器实现备忘功能
Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下 " ...
- noip 2015 运输计划 (lca+二分)
/* 95 最后一个点T了 qian lv ji qiong 了 没学过树剖 听chx听xzc说的神奇的方法 Orz 首先求出每个计划的路径长度 这里写的倍增 然后二分答案 对于每个ans 统计> ...
- Java数字、货币值和百分数等的格式化处理
如果我们用下列语句输出一个数 System.out.println(123456.789); 将会在Console看到输出 123456.789 那么如何得到123,456.789这种格式化的输出呢? ...
- My.Ioc 代码示例——如何使用默认构造参数,以及如何覆盖默认构造参数
在 Ioc 世界中,有些框架(例如 Autofac/NInject/Unity)支持传递默认参数,有些框架(例如 SimpleInjector/LightInjector 等)则不支持.作为 My.I ...
- (转)DedeCms Runphp 标签中调用其他变量的方法
DedeCms Runphp 标签中调用其他变量的方法 我们都知道,在DedeCMS中是可以使用PHP的,常见的方法就是if else了,例如模板制作中,我们需要对来源和作者进行判断,如果为空,则提示 ...
- Linux命令--用户用户组管理
新增用户组 : groupadd groupadd [-g GID] 组名 不加-g 则按照系统默认的gid创建组,跟用户一样,gid也是从500开始的 修改用户组信息 : groupmod grou ...
- 05_Smart-image通过SoftReference提高性能
文章导读: 文件介绍了常见的图片下载开源插件smart-image, 由于移动设备硬件受限,因此Android的相关app都要考虑到性能的关系, 所以很多的第三方插件都使用到了缓存cache技术,本人 ...