简体   繁体   中英

AWK: find out if running on windows

How can i find out whether i am on Windows or not?

I need to execute an OS specific command in AWK.

Specifically, i want to system("mkdir DIR") on windows and system("mkdir -p DIR") everywhere else (Unix/Linux/OS X/BSD), to create a directory recursively.

This is a slightly optimized version of the one by Birei .

The OS environment var is only and (supposedly) always specified on windows, and it should always contain some form of the string "windows", for example "Windows_NT".

awk '
    BEGIN {
        is_windows = 0;
        if (index(tolower(ENVIRON["OS"]), "windows") > 0) {
            is_windows = 1;
        }
    }
    ...
' input-file

Thanks to the guys from #awk and Birei !

I can't test this answer because I don't have access to a Windows system, but give a try to the ENVIRON variable. Linux and Unix paths are separated with : while Windows paths are separated with ; . You could check it, something like:

awk '
    BEGIN {
        is_windows = 0;
        if ( index( ENVIRON["PATH"], ";" ) > 0 ) {
            is_windows = 1;
        }
    }
    ...
' input-file

This could fail if there is only one directory in the Windows path, but I hope you get the idea. Perhaps a combination of several environment variables or something similar could be more helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM