![](/img/trans.png)
[英]Shell Script :How to pass script's command-line arguments through to commands that it invokes?
[英]How to pass parameters using command line arguments in shell script
我是shell脚本的新手,我正在研究jmeter的shell脚本。 到目前为止,运行jmeter脚本,我编写了我的shell脚本,如下所示:
#! bin/sh
start(){
echo "Please enter the file name .jmx extension"
read file
echo "Please enter the log file name .jtl extension"
read log_file
jmeter.sh -n -t $file -l $log_file
}
while [ "$1" != "" ]; do
case "$1" in
start )
start
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
esac
shift
done
我有一个终止进程的停止方法。 在这里,对于这个脚本,我要求用户在不同的行中输入.jmx
fileName和.jtl
fileName。 但我希望用户能够在键入执行脚本的命令时传递.jmx
fileName和.jtl
fileName。
例如: $ ./script.sh .jmx fileName .jtl fileName
然后,脚本应该运行。
我不知道怎么做。 有人可以帮忙吗?
由于从stdin read
读取,因此需要在stdin上传递文件名:
{ echo "file.jmx"; echo "file.jtl"; } | ./script.sh start
使用here-document可以更整洁:
./script.sh start <<END_INPUT
file.jmx
file.jtl
END_INPUT
一些代码审查:如果用法仅使用单个参数“start”或“stop”,则不需要while循环:
#!/bin/sh
do_start_stuff() { ... }
do_stop_stuff() { ... }
case "$1" in
start) do_start_stuff;;
stop) do_stop_stuff;;
*) echo "Usage: $0 {start|stop}"; exit 1;;
esac
要重写脚本以获取所有参数:
#!/bin/sh
usage() {
echo "Usage $0 {start ...|stop}"
# provide more info about arguments for the start case
# provide an example usage
}
case "$1" in
stop) do_stop_stuff ;;
start)
shift
if [ "$#" -ne 4 ]; then usage; exit 1; fi
jmeter.sh -n -t "$1" "$2" -l "$3" "$4"
;;
*) usage ;;
esac
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.