如何在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脚本中传递命令行参数的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

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

实例分享shell字符截取命令之cut命令

linux终端执行shell脚本时提示权限不足

什么是posix shell

详细讲解linux shell中的printf的用法

linux中什么是shell

linux中终端与shell的概念

shell判断字符串为空的方法

shell关系运算符介绍

linux shell中的curl和wget如何使用代理ip的方法教程

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



打赏

取消

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

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

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

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

评论

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