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/ ...
随机推荐
- C++ 让 Win32 Console Application 程序后台运行
方法一:(无闪现) 添加 #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRT ...
- Java 日志缓存机制的实现--转载
概述 日志技术为产品的质量和服务提供了重要的支撑.JDK 在 1.4 版本以后加入了日志机制,为 Java 开发人员提供了便利.但这种日志机制是基于静态日志级别的,也就是在程序运行前就需设定下来要打印 ...
- [转] C++临时变量的生命周期
http://www.cnblogs.com/catch/p/3251937.html C++中的临时变量指的是那些由编译器根据需要在栈上产生的,没有名字的变量. 主要的用途主要有两类: 1) 函数的 ...
- Linux shell入门基础(八)
八.shell脚本sed&awk 01.sed的使用 流编辑器-Steam Editor #ed /etc/passwd 1,10p …… 1s/root/byf/p(替换root为byf) ...
- python函数的使用和返回值
#coding=utf-8 def a(): i=1a() #函数的返回值,用return语句实现 #一个返回值的情况def test(): i=7 return iprint test() #多个返 ...
- 转:Android中的Selector的用法
http://blog.csdn.net/shakespeare001/article/details/7788400
- 编写利用Fragment创建新闻列表
编写利用Fragment创建新闻列表 1.创建新闻实体类News,代码如下: public class News { private String title; private String co ...
- propertychange input change
IE678 支持propertychange事件,可以监听所有属性(包括自定义属性)的改变事件,包括手动修改输入框文本以及js修改输入框文本. propertychange事件有个特点就是不支持冒泡, ...
- 使用第三方SDK出现: duplicate symbol _llvm.cmdline in:
如果是同一个静态库中的文件链接的时候有冲突,可能是这个静态库不支持模拟器,真机运行就好了. 或者可以使用xcode7的虚拟机跑也是没问题的. duplicate symbol _llvm.cmdlin ...
- 路由器to路由器
本文主要介绍一个主路由器连接两个子路由器的方法: 主路由器: 设置主路由器的开始ip地址为192.168.1.100,结束ip地址为192.168.150: 子路由器A: 第一步:LAN设置 第二步: ...