2010-10-19 75 views
0

我有一个shell脚本写在bash和这个脚本应该采取文件作为参数,任何一个可以告诉我怎么写脚本的apprecited如何通过文件作为参数传递给脚本文件

在这个任何想法

感谢,

+0

可能重复(http://stackoverflow.com/questions/3955604/help-with-bash-script) – 2010-10-19 21:56:28

回答

0

如果你需要对文件进行操作,你可以把文件名作为参数,并只使用该文件具有指定名称。

如果你只需要读取文件的内容,您可以使用重定向到有脚本读取文件的内容,在标准,你可以使用./script < inputfile

2

做到这一点,您可以访问命令行参数传递使用positional parameters你的脚本。

另外要检查是否已将正确数量的参数传递给脚本,可以使用变量$#,该变量包含传递的参数数量。

if [ $# -eq 1 ]; then 
     # exactly 1 argument was passed..use it..its available in $1 
     echo "Argument $1" 
else 
     # either 0 or >1 arguments were passed...error out. 
     echo "Incorrect number of arguments passed" 
     exit 1    
fi 

采样运行:

$ bash a.sh 
Incorrect number of arguments passed 
$ bash a.sh foo 
Argument foo 
$ bash a.sh foo bar 
Incorrect number of arguments passed 
$ 
的[帮助,bash脚本]
相关问题