Skip to content

Latest commit

 

History

History
437 lines (376 loc) · 13.6 KB

File metadata and controls

437 lines (376 loc) · 13.6 KB

Shell we dance?

<List>

  • A more advanced exercise compared to JScript via cscript on Windows : 1. DIR (2024.09.26)

    • The main focus was to explore whether JSON could be handled as a script language in Windows, but it turned out to be impossible
      • JScript follows the ECMAScript standard, but it hasn’t been updated in a long time, and many commonly used JavaScript keywords are not supported
      • In short, using JavaScript as-is in the Windows script environment is not easy
    • Instead, since the tool was already in hand, practiced by reading a CSV file: successful
  • Code and Results

    Code : ReadCSV.ts
    const COLUMN_WIDTH: number = 24; // Fixed column width for each cell in the output
    // Create FileSystemObject
    var fso: any = WScript.CreateObject("Scripting.FileSystemObject");
    
    // Get the command-line arguments
    var args: any = WScript.Arguments;
    
    // Check if the CSV file path is provided as an argument
    if (args.length < 1) {
        WScript.Echo("Usage: cscript JScript_RunViaCscript.js <csvFileName>");
        WScript.Quit(1);
    }
    // Get the CSV file name from the argument
    var csvFileName: string = args.Item(0);
    
    // Get the current folder where the script is running
    var scriptFullPath: string = WScript.ScriptFullName;
    var currentFolder: string = fso.GetParentFolderName(scriptFullPath);
    
    // Construct the full path to the CSV file
    var csvFilePath: string = fso.BuildPath(currentFolder, csvFileName);
    
    // Exit early if the file doesn't exist
    if (!fso.FileExists(csvFilePath)) {
        WScript.Echo("File not found: " + csvFilePath);
        WScript.Quit(1);
    }
    // Function to read CSV content from file
    function readCsvFile(filePath: string): string {
        var file = fso.OpenTextFile(filePath, 1); // 1 = ForReading
        var content = file.ReadAll();
        file.Close();
        return content;
    }
    // Function to manually trim whitespace from both ends of a string
    function manualTrim(str: string): string {
        var start: number = 0;
        var end: number = str.length - 1;
    
        while (start <= end && (str.charAt(start) === ' ' || str.charAt(start) === '\t')) {
            start++;
        }
        while (end >= start && (str.charAt(end) === ' ' || str.charAt(end) === '\t')) {
            end--;
        }
        return str.substring(start, end + 1);
    }
    // Function to pad a string to a specified width by appending spaces
    function padString(str: string, width: number): string {
        str = manualTrim(str); // Trim the string first
        var paddedString: string = str;
        while (paddedString.length < width) {
            paddedString += " "; // Append spaces until reaching the desired width
        }
        return paddedString;
    }
    // Function to print the separator line
    function printSeparatorLine(columns: number) {
        var separatorLine: string = "";
        for (var i = 0; i < columns * COLUMN_WIDTH; i++) {
            separatorLine += "-";
        }
        WScript.Echo(separatorLine);
    }
    // Function to print headers
    function printHeaders(headers: string[]) {
        var headerOutput: string = "";
        for (var h = 0; h < headers.length; h++) {
            headerOutput += padString(headers[h], COLUMN_WIDTH);
        }
        WScript.Echo(headerOutput);
    }
    // Function to process CSV content
    function processCsvContent(csvContent: string) {
        var lines: string[] = csvContent.split("\r\n");
        var headers: string[] = lines[0].split(",");
    
        printSeparatorLine(headers.length);
        printHeaders(headers);
        printSeparatorLine(headers.length);
    
        for (var i = 1; i < lines.length; i++) {
            var line: string = lines[i];
            var trimmedLine: string = manualTrim(line);
            if (trimmedLine !== "") {
                var row: string[] = trimmedLine.split(",");
                var record: string = "";
                for (var j = 0; j < headers.length; j++) {
                    record += padString(row[j] ? manualTrim(row[j]) : "N/A", COLUMN_WIDTH);
                }
                WScript.Echo(record);
            }
        }
    
        printSeparatorLine(headers.length);
    }
    // Main execution flow
    var csvContent: string = readCsvFile(csvFilePath);
    processCsvContent(csvContent);
    Code : ReadCSV.js (Compiled from ReadCSV.ts)
    tsc ReadCSV.ts --target es5 --outDir .
    (code omitted)
    Run & Results
    cscript ReadCSV.js WealthiestAmericans_2023.csv
    ------------------------------------------------------------------------------------------------------------------------------------------------
    Rank                    Name                    Net Worth (Billion $)   Age                     State                   Source
    ------------------------------------------------------------------------------------------------------------------------------------------------
    1                       Elon Musk               251                     52                      Texas                   Tesla & SpaceX
    2                       Jeff Bezos              161                     59                      Washington              Amazon
    3                       Larry Ellison           158                     79                      California              Oracle
    4                       Warren Buffett          121                     93                      Nebraska                Berkshire Hathaway
    5                       Larry Page              114                     50                      California              Google
    6                       Bill Gates              111                     67                      Washington              Microsoft
    7                       Sergey Brin             110                     50                      California              Google
    8                       Mark Zuckerberg         106                     39                      California              Facebook
    9                       Steve Ballmer           101                     67                      Washington              Microsoft
    10                      Michael Bloomberg       96.3                    81                      New York                Bloomberg LP
    ……
    ------------------------------------------------------------------------------------------------------------------------------------------------
  • Practicing JScript as a Windows scripting language, as an alternative to Batchfile or PowerShell

  • Code and Results

    Code : DIR.ts
    // Define interfaces for better type checking
    interface WScriptShell {
        Exec(command: string): WScriptExec;
    }
    
    interface WScriptExec {
        StdOut: {
            ReadAll(): string;
        };
    }
    
    // Use type assertion to tell TypeScript the type of WScript
    const shell: WScriptShell = (WScript as any).CreateObject("WScript.Shell");
    
    // Execute 'dir' command to list files in the current directory
    const exec: WScriptExec = shell.Exec("cmd /c dir");
    
    // Read the output from the command
    const output: string = exec.StdOut.ReadAll();
    
    // Print the output
    (WScript as any).Echo(output);
    Code : DIR.js (Compiled from DIR.ts)
    tsc DIR.ts --target es5 --outDir .
    // Use type assertion to tell TypeScript the type of WScript
    var shell = WScript.CreateObject("WScript.Shell");
    // Execute 'dir' command to list files in the current directory
    var exec = shell.Exec("cmd /c dir");
    // Read the output from the command
    var output = exec.StdOut.ReadAll();
    // Print the output
    WScript.Echo(output);
    Run & Results
    cscript DIR.js
    ……
    
     C 드라이브의 볼륨: SSD (C:)
     볼륨 일련 번호: ****-****
    
     C:\**** 디렉터리
    
    2024-09-26  오후 04:46    <DIR>          .
    2024-09-26  오후 04:46    <DIR>          ..
    2024-09-26  오후 04:49               410 DIR.js
    2024-09-26  오후 04:49               679 DIR.ts
    2024-09-26  오후 04:45                36 DIR_Conv.bat
    2024-09-26  오후 04:46                16 DIR_Run.bat
    ……
                  14개 파일              15,937 바이트
    ……
  • Just for fun ☞ related meme

  • Code and Results

    Code : helloWorldEcho.sh
    #!/bin/bash
    helloWorld() {
        # Retrieves the current function name and calls the given function dynamically.
        #
        # Arguments:
        #     funcName (str): The name of the function to call.
        # Returns:
        #     None
        local funcName=$1
        local currentFuncName="${FUNCNAME[0]}"
    
        # Invokes the function with the given name and passes the current function name as an argument.
        "$funcName" "$currentFuncName"
    }
    Run & Results
    # Call the helloWorld function.
    helloWorld "echo"
    helloWorld
  • I've been suddenly curious about these

  • References

  • Code and Results

    cmd : TotalSize.cmd
    dir
    C 드라이브의 볼륨: SSD (C:)
    볼륨 일련 번호: ****-****
    
    C:\****\MyPractice\Shell 디렉터리
    
    2024-03-01  오전 01:52    <DIR>          .
    2024-03-01  오전 01:52    <DIR>          ..
    2024-03-01  오전 02:13               104 TotalSize.cmd
    2024-03-01  오전 02:17               183 TotalSize.ps1
    2024-03-01  오전 02:18               164 TotalSize.sh
                  3개 파일                 451 바이트
                  2개 디렉터리   4,160,643,072 바이트 남음
    dir *.cmd
    C 드라이브의 볼륨: SSD (C:)
    볼륨 일련 번호: ****-****
    
    C:\****\MyPractice\Shell 디렉터리
    
    2024-03-01  오전 02:13               104 TotalSize.cmd
                  1개 파일                 104 바이트
                  0개 디렉터리   4,164,726,784 바이트 남음
    PowerShell : TotalSize.ps1
    get-childitem
        디렉터리: C:\****\MyPractice\Shell
    
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    -a---      2024-03-01   오전 2:13        104 TotalSize.cmd
    -a---      2024-03-01   오전 2:17        183 TotalSize.ps1
    -a---      2024-03-01   오전 2:18        164 TotalSize.sh
    get-childitem *.ps1
    -a---      2024-03-01   오전 2:17        183 TotalSize.ps1
    get-childitem | measure-object -property length -sum
    Count    : 3
    Average  :
    Sum      : 451
    Maximum  :
    Minimum  :
    Property : Length
    Bash : TotalSize.sh
    ls -l
    total 3
    -rw-r--r-- 1 fya 197609 104 Mar  1 02:13 TotalSize.cmd
    -rw-r--r-- 1 fya 197609 183 Mar  1 02:17 TotalSize.ps1
    -rwxr-xr-x 1 fya 197609 164 Mar  1 02:18 TotalSize.sh
    du -c 
    3       .
    3       total
    du -ch
    3.0K    .
    3.0K    total
    du -cb
    451     .
    451     total
    du -cb *
    104     TotalSize.cmd
    183     TotalSize.ps1
    164     TotalSize.sh
    451     total
    du -cb *.sh
    164     TotalSize.sh
    164     total