写在前面:案例、常用、归类、解释说明。(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之处理用户输入的更多相关文章

  1. Linux shell脚本读取用户输入的参数

    新建一个test.sh文件 #!/bin/sh echo "1 : For Test" echo "2 : For nohup &" whiletrue ...

  2. shell编程中用户输入处理(shell 04)

    shell编程中用户输入处理1.命令行参数2.脚本运行时获取输入 命令行参数 通过空格来进行分割的位置参数 :$+position $0,$1,$2 ....$0 :程序名$1,$2,$3 ... $ ...

  3. Linux - 简明Shell编程13 - 用户输入(UserInput)

    脚本地址 https://github.com/anliven/L-Shell/tree/master/Shell-Basics 示例脚本及注释 1 - arguments #!/bin/bash i ...

  4. [转]linux shell数据重定向(输入重定向与输出重定向)详细分析

      在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件 ...

  5. shell中处理用户输入

    1.使用命令行参数 在shell执行的时候命令行中输入的所有参数可以赋值给一些特殊变量,这些变量成为位置变量参数. 包括: $0返回脚本名称.$1为第一个参数.$2为第二个参数 ...$9第九个参数 ...

  6. Linux shell简单创建用户脚本

    前面介绍简单的shell编写规则. 现在开始编写一个简单的shell脚本. Linux shell介绍 编写shell脚本    1.创建脚本文件    2.根据需求,编写脚本    3.测试执行脚本 ...

  7. 自学Linux Shell13.3-获得用户输入(read命令)

    Bash shell提供了一些不同的方法来从用户处获得数据,包括以下3中方法: 命令行参数(添加在名利后面的数据) 命令行选项(可修改命令行为的单个字母)主要getopt.getopts命令 直接从键 ...

  8. shell初级-----处理用户输入

    命令行参数 读取参数 位置参数变量是标准的数字:$0是程序名,$1是第一个参数,$2,是第二个参数,直到第九个参数$9. 特殊的变量:$#表示参数个数,$?表示最后运行的命令的结束代码(返回值) 每个 ...

  9. linux shell数据重定向(输入重定向与输出重定向)详细分析

    linux shell下常用输入输出操作符是: 1. 标准输入 (stdin) :代码为 0 ,使用 < 或 << : /dev/stdin -> /proc/self/fd/ ...

随机推荐

  1. HDU 2066-一个人的旅行(最短路Dijkstra)

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. VMware下安装Ubuntu,那么必须安装VMware-tools,才能获得更好的体验,包括屏幕分辨率、声音、和windows共享剪贴板等等

    在VMware下安装Ubuntu,那么必须安装VMware-tools,才能获得更好的体验,包括屏幕分辨率.声音.和windows共享剪贴板等等. 个人觉得安装vmware-tools很重要的几点: ...

  3. 【Java规划】DOM XML Parser分解、遍历、创XML

    1.前言 DOM XML Parser简介 DOM 它是 W3C 处理 XML 规范 API,这是很多其他的 XML 地基处理相关标准,不仅是 Java,其他的,如 Javascript,PHP.MS ...

  4. mac eclipse配置 tomcat

    mac由于各种软件不兼容,某些软件对于开发来说很苦恼,tomcat这个东西一直不知道怎么弄,项目都开了好几天了,一直用在虚拟机上用windows系统,但是用虚拟机明显感觉到电池待机时间更短了,所以就研 ...

  5. jQuery代码优化 事件委托篇

    <转自 http://www.jb51.net/article/28770.htm> 参考文章:  解密jQuery事件核心 - 绑定设计(一) 参考文章:  解密jQuery事件核心 - ...

  6. Python开发【第七篇】:面向对象 和 python面向对象进阶篇(下)

    Python开发[第七篇]:面向对象   详见:<Python之路[第五篇]:面向对象及相关> python 面向对象(进阶篇)   上一篇<Python 面向对象(初级篇)> ...

  7. python增删改查

    ###增删改查 names = ["zhangding","wangxu","wudong","cheng"] #增na ...

  8. javascript实现Map

    function Map() { this.elements = new Array(); // 获取MAP元素个数 this.size = function() { return this.elem ...

  9. Alibaba FastJson

    import com.alibaba.fastjson.JSON import com.alibaba.fastjson.JSONObject class Test { static main(arg ...

  10. 不同浏览器创建XMLHttpRequest对象

    function getXHR() { if (XMLHttpRequest) { return new XMLHttpRequest(); } else { return new ActiveXOb ...