如何在Shell脚本中传递命令行参数


当前第2页 返回上一页

我们还可以通过改变命令行参数在shell脚本中的位置来访问它们。比如用$1访问第一个命令行参数。现在将参数换成1.意味着第二个参数现在位于第一个位置,相同的第三个位于第二个位置,依此类推。

使用下面的内容创建shell脚本myscript3.sh,并使用参数执行。现在现在观察如何在shell脚本中使用“shift <number>”命令移动参数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#!/bin/bash

 

echo First Argument is: $1

 

echo " >> Shifting argument position by 1"

shift 1

 

echo Now first Argument is: $1

echo " >> Now Shifting position with 2"

shift 2

 

echo Now first Argument is: $1

echo " >> Now shifting position with 4"

shift 4

 

echo Now first Argument is: $1

执行脚本并密切观察脚本中$1的输出。

1

2

3

4

5

6

7

8

9

[root@tecadmin ~]# sh myScrit3.sh a friend in need is a friend indeed

 

First Argument is: a

 >> Shifting argument position by 1

Now first Argument is: friend

 >> Now Shifting position with 2

Now first Argument is: need

 >> Now shifting position with 4

Now first Argument is: indeed

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的Linux教程视频栏目!

以上就是如何在Shell脚本中传递命令行参数的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

linux默认使用的shell是什么

shell编程--grep命令如何用?

如何在运维过程中对shell内建命令

如何在shell脚本中传递命令行参数

adb shell是什么意思

如何在shell脚本中以隐藏字符的形式输入密码

linux下shell如何获取某目录下所有文件夹的名称详解

linux shell ftp按照日期去下载文件的方法

一文读懂shell编程三剑客之一的sed命令

linux中shell脚本怎么运行

更多相关阅读请进入《shell》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...