命令行参数

#./admin 10 30

读取参数

[root@localhost scrips]# vi test28.sh

#!/bin/bash
factorial=1
for ((number=1; number<=$1; number++))
do
factorial=$[ $factorial * $number ]        #factorial=$(($factorial * $number))
done
echo "Then factorial of $1 is $factorial"

[root@localhost scrips]# bash test28.sh 5
Then factorial of 5 is 120

[root@localhost scrips]# vi test29.sh

#!/bin/bash
total=$(($1*$2))
echo "The first parameter is $1."
echo "The second parameter is $2."
echo "The total value is $total"

[root@localhost scrips]# bash test29.sh 2 5
The first parameter is 2.
The second parameter is 5.
The total value is 10

如果脚本需要的命令行参数不止个,你仍然可以处理,但是需要稍微修改一下变量名,在第9个变量之后,你必须在变量数字周围上花括号,经如${10}。

读取脚本名

#echo "The zero parameter is set to: $0"    #$0是读取脚本名用的变量。

[root@localhost scrips]# vi test1a.sh

#!/bin/bash
name=$(basename $0)       
echo                 
echo "The script name is: $name"

[root@localhost scrips]# bash test1a.sh

The script name is: test1a.sh

测试参数

[root@localhost scrips]# vi test2a.sh

#!/bin/bash
if [ -n "$1" ]
then
echo "Hello $1, glad to meet you."
else
echo "Sorry, you did not identify yourself"
fi

[root@localhost scrips]# bash test2a.sh
Sorry, you did not identify yourself
[root@localhost scrips]# bash test2a.sh 4
Hello 4, glad to meet you.

特殊参数变量

参数统计

  $#示例

[root@localhost scrips]# vi file1.sh

#!/bin/bash
echo "There were $# parameters supplied."

[root@localhost scrips]# bash file1.sh 1 2 3 4 5
There were 5 parameters supplied.

[root@localhost scrips]# vi file2.sh

#!/bin/bash
params=$#
echo
echo "The last parameter is $params
echo "The last parameter is ${!#}        #变量${!#}会返回命令行用到的脚本名
echo

[root@localhost scrips]# bash file2.sh

The last parameter is 0
echo The last parameter is file2.sh

抓取所有的数据

[root@localhost scrips]# vi file3.sh

#!/bin/bash
echo
echo "Using the \$* method: $*"
echo
echo "Using the \$@ method: $@"

[root@localhost scrips]# bash file3.sh rich barbara katie jessica

Using the $* method: rich barbara katie jessica

Using the $@ method: rich barbara katie jessica

$*和$@变量可以用来轻松访问所有的参数,这个两个变量都能够在单个变量中存储所有的命令行参数。$*变量会将命令行上提供的所有参数当作一个单词保存,这个单词包含了命令行中出现的每一个参数值。基本上$*变量会将这些参数视为一个整体,而不是多个个体。

$@变量会将命令行上提供的所有参数当作同一个字符串的多个独立的单词,这样你就能够遍历所有的参数值,得到每个参数,

[root@localhost scrips]# vi file3.sh

#!/bin/bash
echo
count=1
for param in "$*"
do
echo "\$* parameter $count = $param"
count=$[ $count + 1 ]
done

echo
count=1
for param in "$@"
do
echo "\$@ parameter $count = $param"
count=$[ $count + 1 ]
done

[root@localhost scrips]# bash file3.sh rich barbara katie jessica

$* parameter 1 = rich barbara katie jessica

$@ parameter 1 = rich
$@ parameter 2 = barbara
$@ parameter 3 = katie
$@ parameter 4 = jessica

移动变量

[root@localhost scrips]# vi file5.sh

#!/bin/bash
echo
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done

[root@localhost scrips]# bash file5.sh 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的值会被删除(注意:变量$0的值,也就是程序名,不会改变)

处理选项

查找选项

[root@localhost scrips]# vi file8.sh

#!/bin/bash
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done

[root@localhost scrips]# bash file8.sh -a -b -c -d

Found the -a option
Found the -b option
Found the -c option
-d is not an option

case语句会检查每个参数是不是有效选项,如果是的话,就运行对应case语句中的命令,不管选项按什么顺序出现在命令行上,这种方法都适用。

分离参数和选项

[root@localhost scrips]# vi file10.sh

#!/bin/bash
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option" ;;
esac
shift
done
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done

[root@localhost scrips]# bash file10.sh -a -b -c test1 test2 test3

Found the -a option
Found the -b option
Found the -c option
test1 is not an option
test2 is not an option

[root@localhost scrips]# bash file10.sh -a -b -c -- test1 test2 test3

Found the -a option
Found the -b option
Found the -c option
Parameter #1: test1
Parameter #2: test2
Parameter #3: test3

shell-处理用户输入的更多相关文章

  1. Shell 读取用户输入

    14.2  读取用户输入 14.2.1  变量 上一章我们谈到如何定义或取消变量,变量可被设置为当前shell的局部变量,或是环境变量.如果您的shell脚本不需要调用其他脚本,其中的变量通常设置为脚 ...

  2. shell获取用户输入

    主题: 再学shell之获取用户输入echo -n(不换行)和read命令-p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 和“读文件”深入学习 1.基本读取read命令接收标准输 ...

  3. Shell编程—用户输入

    1命令行参数 1.1读取参数 bash shell会将一些称为位置参数(positional parameter)的特殊变量分配给输入到命令行中的所有参数.这也包括shell所执行的脚本名称.位置参数 ...

  4. linux shell获取用户输入

    一.获取用户输入1.基本读取read命令接收标准输入的输入,或其它文件描述符的输入.得到输入后,read命令将数据输入放入一个标准变量中.[root@rac2 ~]# cat t8.sh #!/bin ...

  5. 《Linux命令行与shell脚本编程大全》第十四章 处理用户输入

    有时还会需要脚本能够与使用者交互.bash shell提供了一些不同的方法来从用户处获得数据, 包括命令行参数,命令行选项,以及直接从键盘读取输入的能力. 14.1 命令行参数 就是添加在命令后的数据 ...

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

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

  7. shell脚本,提示用户输入一个用户名,如果存在;显示用户UID和SHELL信息;否则,则显示无此用户;显示完成之后,提示用户再次输入;如果是quit则退出;

    [root@localhost wyb]# cat tishiuser.sh #!/bin/bash #提示用户输入一个用户名,如果存在:显示用户UID和SHELL信息:否则, #则显示无此用户:显示 ...

  8. shell中处理用户输入

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

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

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

  10. Linux&shell之处理用户输入

    写在前面:案例.常用.归类.解释说明.(By Jim) 命令行参数$1为第一个参数,$2为第二个参数,依次类推...示例: #!/bin/bash # using one command line p ...

随机推荐

  1. 《为什么说 Prometheus 是足以取代 Zabbix 的监控神器?》

    为什么说 Prometheus 是足以取代 Zabbix 的监控神器?   Kuberneteschina 致力于提供最权威的 Kubernetes 技术.案例与Meetup! ​关注他 12 人赞同 ...

  2. 详解C++ STL multiset 容器

    详解C++ STL multiset 容器 本篇随笔简单介绍一下\(C++STL\)中\(multiset\)容器的使用方法及常见使用技巧. multiset容器的概念和性质 \(set\)在英文中的 ...

  3. php 学习笔记之关于时区的那点事

    科普一下什么是时区 众所周知,地球绕着太阳转的同时也会自转,因此同一时刻不同地区所接收到太阳照射的情况不同,所以有的地区是日出,有的地区是日落,还有的地区可能是黑夜. 既然地球上的不同地区时间不同,那 ...

  4. Bootstrap分页查询

    前台方法: function show() { $('#reportTable').bootstrapTable({ method: 'get', url: "@Url.Action(&qu ...

  5. 第四组 团队Git现场编程实战

    组员职责分工 组员 分工 林涛(组长) 分配任务.整理数据.写博客 童圣滔 UI界面制作 林红莲 UI界面制作 潘雨佳 测评出福州最受欢迎的商圈 于瀚翔 测评出福州最受欢迎的商圈 覃鸿浩 测评出福州人 ...

  6. pycharm的安装与破解

    一.首先去Pycharm官网,或者直接输入网址:http://www.jetbrains.com/pycharm/download/#section=windows,下载PyCharm安装包,根据自己 ...

  7. pixijs shader颗粒化显示贴图

    pixijs shader颗粒化显示贴图 const app = new PIXI.Application({ transparent: true }); document.body.appendCh ...

  8. DDR基础知识

    1.前言 DDR的全称为Double Data Rate SDRAM,也就是双倍速率的SDRAM,SDRAM在一个CLK周期传输一次数据,而DDR在一个CLK周期传输两次数据,分别在上升沿和下降沿各传 ...

  9. 一位IT民工的十年风雨历程

    距离2020年只有30天了,转眼毕业快10年. 回首自己,已三十有三,中年危机. 古人云三十而立,我却还在测试途中摸爬滚打. 创业,自由职业永远是一个梦,白日梦. 焦虑.迷茫.看不到希望. 这两天一场 ...

  10. 缘起 Dubbo ,讲讲 Spring XML Schema 扩展机制

    背景 在 Dubbo 中,可以使用 XML 配置相关信息,也可以用来引入服务或者导出服务.配置完成,启动工程,Spring 会读取配置文件,生成注入 相关 Bean.那 Dubbo 如何实现自定义 X ...