-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCmdSyntax.nsh
More file actions
executable file
·58 lines (44 loc) · 1.68 KB
/
CmdSyntax.nsh
File metadata and controls
executable file
·58 lines (44 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env dotnet-shell
using System.Net.Http;
using System.Text.Json;
// Variables can easily be created from a command's stdout
if (string.IsNullOrWhiteSpace(`echo hello`))
{
echo oh no!
}
// By default the type is a string
var ps=`ps aux`;
Console.WriteLine("Result is a "+ps.GetType().Name+" should be a string");
Console.WriteLine("LastLine = " + ps.Trim().Split(Environment.NewLine).Last());
// But you can also easily convert to other types like an int
int aNumber=`/bin/echo 500`;
Console.WriteLine("Result is a "+aNumber.GetType().Name+" "+aNumber+" should be an int with value 500");
// Or a list
List<string> z=`dmesg`; z.Distinct().Count();
// StdOut representing a file path can be converted to a file or directory
FileInfo file = `/bin/echo /bin/ls`;
var exists = file.Exists ? "Exists" : "Doesn't exist";
// You can pass variables into cmds like this
echo File $exists$
DirectoryInfo dir = `/bin/echo /bin/`;
var dirExists = dir.Exists ? "Exists" : "Doesn't exist";
var result = `echo $dirExists$`;
Console.WriteLine("Directory " + result);
// Or a stream
Stream stream = `cat CmdSyntax.nsh`;
Console.WriteLine("Result is a "+stream.GetType().Name);
char x = (char) stream.ReadByte();
Console.WriteLine("First char of stream = " + x + " should be #");
// Maybe you have a web request which returns JSON, you can quickly make that a
// object
public class IPInfo
{
public string ip { get; set; }
}
using (var client = new HttpClient())
using (var response = await client.GetAsync("https://api64.ipify.org?format=json"))
using (var content = response.Content)
{
var obj = JsonSerializer.Deserialize<IPInfo>(await content.ReadAsStringAsync());
Console.WriteLine(obj.ip);
}