我有一個簡單的 c# 程式,它在運行時需要命令列輸入(來自Console.ReadLine())。我必須提供很多輸入,所以我想知道如何使這個程序自動化。我目前有一個嘗試執行此操作的 shell 腳本,但它不起作用。
#!/bin/sh
dotnet run #run the program
1 #input first argument (this failed so I tried echo 1 instead but no luck)
# <- rest of command line inputs on each line
不太熟悉 shell 腳本,如果您有其他解決方案,我不會固定在這個解決方案上。我的作業系統是 MacOS。
uj5u.com熱心網友回復:
好的,所以我嘗試的是這樣的:
- 我做了一個小的 mcve-Console 應用程式:(dotnet 6)
var input = Console.ReadLine();
while( !string.IsNullOrEmpty(input) )
{
Console.WriteLine("User input: {0}", input);
input = Console.ReadLine();
}
- 然后我做了一點
input.txt
This is a Text.
This is another Text.
1
2
3
This is the final Text.
- 最后
run.sh如下:
#!/bin/sh
dotnet run < "input.txt"
輸出是
用戶輸入:這是一個文本。 用戶輸入:這是另一個文本。 用戶輸入:1 用戶輸入:2 用戶輸入:3 用戶輸入:這是最終的文本。
/bin/sh 在我的例子中鏈接到 dash shell。
如果run.sh您將“input.txt”替換為“$1”,那么您可以呼叫它./run.sh whateveryourinputfileis.txt以使其更加靈活。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/481113.html
上一篇:使用來自另一個函式的引數呼叫函式
