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/ ...
随机推荐
- Java基础知识强化之IO流笔记19:FileOutputStream的三个write方法
1. FileOutputStream的三个write方法: void write(byte[] buffer) Writes the entire contents of th ...
- php 链式操作的实现 学习记录
php 面向对象中实现链式操作的关键部分:调用的方法中返回当前对象 ,从而实现链式操作: <?php namespace commom; class db { public function w ...
- MyBatis 学习总结(一)
1.原生JDBC(Java database connectity)操作数据库(以MySQL数据为例)步骤 1.1 加载驱动 Class.forName("com.mysql.jdbc.Dr ...
- 纯css+js水平时间轴
自定义,并自动加载时间节点 当前时间节点居中,突出显示 时间动态无痕添加 效果图: 初始状态 时间左走到一定2016.1月后 html: <!-- 水平时间轴 --> <div id ...
- MySQL无法登录服务器解决方法
提示:#2000 无法登录 MySQL 服务器 今天用本机装了个phpMyAdmin,版本3.4.8,想用它来连一台内网服务器上的Mysql,于是乎修改phpMyAdmin配置文件config.inc ...
- eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass method
在做arcgis android开发的时候,突然遇到这种错误,The method onClick(View) of type new View.OnClickListener(){} must ov ...
- linux File Handling commands 'ls'.
ref:Linux / Unix Command: ls NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DES ...
- 抓取锁的sql语句-第六次修改
增加异常处理 CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 ...
- jrae源码解析(一)
jare用java实现了论文<Semi-Supervised Recursive Autoencoders for Predicting Sentiment Distributions>中 ...
- [转] NSString / NSMutableString 字符串处理,常用代码
原文 : http://justcoding.iteye.com/blog/1405951 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString , ...