每秒列出时间作为 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2752346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listing time every second as a Bash script
提问by Caleb
first time here as I've finally started to learn programming. Anyway, I'm just trying to print the time in nanoseconds every second here, and I have this:
第一次来这里,因为我终于开始学习编程了。无论如何,我只是想在这里每秒以纳秒为单位打印时间,我有这个:
#!/usr/bin/env bash
while true;
do
date=(date +%N) ;
echo $date ;
sleep 1 ;
done
Now, that simply yields a string of date's, which isn't what I want. What is wrong? My learning has been rather messy, so I hope you'll excuse me for this if it's really simple. Also, I did manage to fine this, that worked on the prompt:
现在,这只是产生一串日期,这不是我想要的。怎么了?我的学习一直比较混乱,所以如果真的很简单,我希望你能原谅我。另外,我确实设法解决了这个问题,这对提示有效:
while true ; do date +%N ; sleep 1 ; done
But that obviously doesn't work as a script?
但这显然不能作为脚本工作?
Edit, if anyone sees this: Ahh, this does indeed fix my error. I note you didn't add a ; Is that because I only defined a variable? Also, could you explain what the $ does? I thought it was for calling variables. And I see that the above line will indeed work as a script; I had expected the output of date to not be put on the screen.
编辑,如果有人看到这个:啊,这确实解决了我的错误。我注意到你没有添加 ; 那是因为我只定义了一个变量吗?另外,你能解释一下 $ 的作用吗?我以为是为了调用变量。我看到上面的行确实可以作为脚本工作;我原以为 date 的输出不会出现在屏幕上。
回答by codaddict
Change
改变
date=(date +%N) ;
to
到
date=$(date +%N)
回答by Dmitry Spikhalskiy
This version should work
这个版本应该可以用
#!/bin/bash
while true; do
date=$(date +"%N")
echo Current date is $date
sleep 1
done
回答by Aleksandr Poteriachin
You can enclose your command between "`" (Backtick) too. For example:
您也可以将命令括在“`”(反引号)之间。例如:
date=`date +%N`
Regards
问候

