Get username from the command line on different platforms

Is there any way in MATLAB to get the name of the user who started the session?

I'm interested in solutions for Windows , Linux, and Mac OSX . I believe that if the solutions are platform specific, both solutions can be integrated as follows:

if ispc user_name = % method 1 elseif isunix user_name = % method 2 elseif ismac user_name = % method 3 end 
+6
source share
3 answers
 if isunix [~, user_name] = system('whoami') % exists on every unix that I know of % on my mac, isunix == 1 elseif ispc [~, user_name] = system('echo %USERDOMAIN%\%USERNAME%') % Not as familiar with windows, % found it on the net elsewhere, you might want to verify end 

Hope this helps! You might also want to include the else I'm confused clause in case you find a system that is not unix or pc.

+4
source

How about using Java (works on all platforms supported by MATLAB):

 user_name = java.lang.System.getProperty('user.name') 
+13
source

To get it on Windows:

getenv('USERNAME')

+1
source

Source: https://habr.com/ru/post/921416/


All Articles