简体   繁体   English

如何在shell脚本中使用“ time”命令?

[英]How can I use the `time` command IN a shell script?

I often use time to time a command via console: 我经常使用time通过控制台来计时命令:

time PGPASSWORD=postgres psql -U postgres -h localhost -f init.sql

I now created a shell script which does the same: 我现在创建了一个shell脚本,它执行相同的操作:

#!/bin/sh
time PGPASSWORD=postgres psql -U postgres -h localhost -f init.sql

Now I get the following error: 现在我得到以下错误:

time: cannot run PGPASSWORD=postgres: No such file or directory 时间:无法运行PGPASSWORD = postgres:没有此类文件或目录
Command exited with non-zero status 127 命令以非零状态退出127

How can I use time in a script? 如何在脚本中使用time

bash defines time as a pre-command modifier; bashtime定义为命令前修饰符; /bin/sh is just running the external command time . /bin/sh只是在运行外部命令time You probably want #!/bin/bash as your shebang. 您可能想要#!/bin/bash作为您的shebang。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM