【问题标题】:Is it possible to define a shell script using the c# lang (dotnet core runtime)?是否可以使用 c# lang(dotnet 核心运行时)定义 shell 脚本?
【发布时间】:2018-08-31 09:52:52
【问题描述】:

例如定义一个打印“hello”的脚本。

我可以用 nodejs (hello_node) 做以下事情

#!/bin/node
console.log(`hello ${process.argv[2]}`)

那我就叫它

./hello_node "world"

在 Python 中也是如此

#!/bin/python3.7
print("hello world")

在 PHP 和 ruby​​ 中同样有效,有没有办法在 C# 中做到这一点?

我尝试了以下方法,但没有成功

#!/usr/local/share/dotnet/dotnet run
using System;

public static class Program {

  public static void Main(string[] args)
  {
    Console.WriteLine("Hello " + args[1]);
  }

}

【问题讨论】:

  • 什么“没用”?假设代码被正确嵌入到项目中,这应该工作。请记住,arrays 从零开始,所以如果你执行dotnet run World,你会得到一个IndexOutOfRangeException,如果你执行dotnet run World Something,你应该得到Hello Something(忽略第零个参数World )。 -- 并且很容易创建一个项目,如下所述:.NET Tutorial - Hello World in 10 minutes

标签: c# linux bash shell


【解决方案1】:

shebang (#!) 告诉它使用特定命令打开文件。

dotnet run 命令只能用于运行项目,不能用于运行单个文件。

您可以尝试使用 dotnet-script 工具,但您需要设置目录,根据您要实现的目标,这可能是一个很大的缺点

https://github.com/filipw/dotnet-script

【讨论】:

    猜你喜欢
    • 2018-08-25
    • 2010-12-12
    • 2019-06-17
    • 2012-06-03
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多