473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

perl "whoami" in WINDOWS (want to use different directory for each user)

5
Hello.

I write a perl script that 5 different people will use.
Each one of the 5 will run the script on the same computer, but will login to this computer with a different user_name and so (I guess) with a different user environment variable.

If we were working on Unix, I could simply use the WHOAMI unix command in the perl script. But we are working with WINDOWS.

So, in short, how can the perl script know who is the current user, without asking him, in order to use a different directlry for each user ?

Thanks
Roni
<EMAIL REMOVED BY MOD>
Jul 3 '07 #1
4 5090
numberwhun
3,509 Expert Mod 2GB
Hello.
Hello!

I write a perl script that 5 different people will use.
Each one of the 5 will run the script on the same computer, but will login to this computer with a different user_name and so (I guess) with a different user environment variable.

If we were working on Unix, I could simply use the WHOAMI unix command in the perl script. But we are working with WINDOWS.

So, in short, how can the perl script know who is the current user, without asking him, in order to use a different directlry for each user ?
Ok, first.... perldoc is your friend. Reference it often and always. ( just a tip that could have helped here).

That said, take a look at this page. That is the perldoc page for Perl's special variables. On that page, you will see that Perl has the hash %ENV already defined, containing all of the environment variables as keys and their values as.....well.....the values.

Now, the question would be, "How do I utilize that variable?". That question can be answered by visiting this perldoc page. That page is for the keys function, which basically grabs all of the keys in a hash for you.

The code on that page(the very first example) is the code that you are looking for. Here it is:

Expand|Select|Wrap|Line Numbers
  1.     @keys = keys %ENV;
  2.     @values = values %ENV;
  3.     while (@keys) {
  4.     print pop(@keys), '=', pop(@values), "\n";
  5.     }
  6.  
Now, if you are using strict and warnings (as you should ALWAYS be doing), then you will want to prepend the array definitions with "my" or Perl will throw complaints at you.

When you run this code on your Windows machine (perl <scriptName>), it will print out ALL of the environment variables defined, and their corresponding values (using the values function shown above). One of the variables is "USERNAME".

Now, all you have to do is write some code that will reference the USERNAME variable from the hash and grab its value so your script can use it.

Hope this helps to put you on the path to completing your script.

Regards,

Jeff
Jul 3 '07 #2
KevinADC
4,059 Expert 2GB
I think it will depend on which version of Windows you are using. But try Jeffs suggestions and see if that works for you.
Jul 3 '07 #3
roniz5
5
Thank you very much Jeff!

It works.

Thanks for the quick, accurate and full of humor reply :)

Roni
Jul 4 '07 #4
numberwhun
3,509 Expert Mod 2GB
Excellent! Glad that I could help and that it worked for you.

Regards,

Jeff
Jul 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: R. Bressers | last post by:
Hi, I've got the following problem with PHP5.0 : index.php: ---------- <?php system("./whoami.pl");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.