我有一個基本的script.sh,它在裏面運行一些命令。劇本是這樣的:設置命令執行的超時時間並將結果輸出到文件
#Go to this directory,pass all folders to compile&execute the program.c file
[email protected]~$ for D in /Users/czar/Desktop/1/*; do sh script.sh $D; done
編輯:
(script.sh)
......
`gcc -o program program.c`
if [ $? -eq 0 ]; then
echo "Compiled successfully....\n" >> out.txt
#set a timeout for ./program execution and append results to file
(gtimeout 10s ./program) 2> out.txt # <-- NOT WORKING
......
我通過像終端運行此腳本的輸出我得到的終端,不是那麼重要,但:
# program.c from 1st folder inside the above path
Cycle l=1: 46 46
Cycle l=1: 48 48
Cycle l=2: 250 274 250
Cycle l=1: 896 896
.........
# program.c from 2nd folder inside the above path
Cycle l=1: 46 46
Cycle l=1: 48 48
Cycle l=2: 250 274 250
Cycle l=1: 896 896
.........
目標是讓那些進入out.txt
我得到的輸出是差不多我想要什麼:它會在那10秒內執行任何可能的操作,但不會將結果重定向到out.txt,它只會打印到終端。 我嘗試了每個建議here但沒有運氣。
任何其他想法讚賞。
編輯2:在評論中給出的解決方案。
當你嘗試'gtimeout 10s ./program> out.txt'時出現了什麼問題? – rici
你是'gcc -o program program.c'裏面的反引號嗎? – Fred
謝謝你們花時間回答,@rici當我做'gtimeout 10s ./program> out.txt'時,我得到空out.txt,這很奇怪。 @Fred其實沒有理由,反引號被用來執行一個命令,對我來說它更清楚。 – L1lda