shell脚本在执行时,向脚本传递参数
2015-10-23 15:54
846 查看
Shell脚本在执行时,也可以像命令和应用程序一样,接收脚本参数。
一个应用实例:
vi addhead.sh:
suaddhead ns=591 <$1 |sushw key=dt a=20000|sushw key=fldr,cdp a=1,1 b=0,1 c=1,0 j=1301,1301> $2
执行:
./addhead.sh rtm_step10_lap.dat rtm_step10_lap.su(其中,rtm_step10_lap.dat是输入数据,输入给$1, 输出为:rtm_step10_lap.su)
教程如下:
可以使用系统定义的位置变量,位置变量是一类比较特殊的变量,引用脚本参数时,可以使用$1到$9这9个变量。
示例:cat example1.sh
#!/bin/bash
#Demonstrate the use of positive variables.
echo "The first parameter:"$1
echo "The second parameter:"$2
echo "The third parameter:"$3
echo "The fourth parameter:"$4
上面的脚本中,分别使用$1 $2 $3 $4捕获传递给脚本文件的第1 2 3 4个参数,并将其输出。
执行:
#为脚本文件添加可执行权限
chmod u+x example1.sh
#./example1.sh One Two Three Four
The first parameter: One
The second parameter: Two
The three parameter: Three
The fourth parameter: Four
示例2:example2.sh
echo '$1' "=" $1 '$2' "=" $2 '$3' "=" $3
echo '$4' "=" $4 '$5' "=" $5 '$6' "=" $6
echo '$7' "=" $7 '$8' "=" $8 '$9' "=" $9
运行:
./example2.sh a b c d e f g h i
得到:
$1 = a $2 = b $3 = c
$4 = d $5 = e $6 = f
$7 = g $8 = h $9 = i
一个应用实例:
vi addhead.sh:
suaddhead ns=591 <$1 |sushw key=dt a=20000|sushw key=fldr,cdp a=1,1 b=0,1 c=1,0 j=1301,1301> $2
执行:
./addhead.sh rtm_step10_lap.dat rtm_step10_lap.su(其中,rtm_step10_lap.dat是输入数据,输入给$1, 输出为:rtm_step10_lap.su)
教程如下:
可以使用系统定义的位置变量,位置变量是一类比较特殊的变量,引用脚本参数时,可以使用$1到$9这9个变量。
示例:cat example1.sh
#!/bin/bash
#Demonstrate the use of positive variables.
echo "The first parameter:"$1
echo "The second parameter:"$2
echo "The third parameter:"$3
echo "The fourth parameter:"$4
上面的脚本中,分别使用$1 $2 $3 $4捕获传递给脚本文件的第1 2 3 4个参数,并将其输出。
执行:
#为脚本文件添加可执行权限
chmod u+x example1.sh
#./example1.sh One Two Three Four
The first parameter: One
The second parameter: Two
The three parameter: Three
The fourth parameter: Four
示例2:example2.sh
echo '$1' "=" $1 '$2' "=" $2 '$3' "=" $3
echo '$4' "=" $4 '$5' "=" $5 '$6' "=" $6
echo '$7' "=" $7 '$8' "=" $8 '$9' "=" $9
运行:
./example2.sh a b c d e f g h i
得到:
$1 = a $2 = b $3 = c
$4 = d $5 = e $6 = f
$7 = g $8 = h $9 = i
相关文章推荐
- bash: /usr/bin/php^M: bad interpreter: No such file or directory
- Ubuntu Error:couldn't to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
- 捣鼓vm里centos能用本地xshell连接
- XShell连接虚拟机Linux
- linux下shell脚本移动、复制文件笔记
- 采用Bash脚本性能监控过程
- shell 中数学计算总结
- shell常用命令和方法总结记录
- shell脚本read传递参数
- 多层文件夹内文件遍历函数
- shell编程报错 [: missing `]'
- Windows环境下eclipse提交到远程wordcount程序报错 at org.apache.hadoop.util.Shell.runCommand(Shell.java:545)
- shell假设判断能否上网
- Linux shell函数使用
- Gnome3提取gnome-shell.css以及修改和编译
- shell 如何实现两个表的join操作
- Linux_shell编程基础_source命令和点命令
- 小白学习shell编程
- shell中变量自增的实现方法
- 学习日志---linux shell编程之while和until