-1

I have a line of code in bash which I need to have run in python.

printf "operator: 'finger ${USER}|grep Name:| cut -f3 -d:(${USER})\n" > temp_header

I'm having a little trouble interpreting it, as I'm new to shell scripts and bash in particular, but from what I've gathered it wants to pull the user's username and name from finger and write them to a new header.

Am I interpreting this correctly, and if so, would I want to use windows cmd prompt to get username/name from the server?

user2049004
  • 157
  • 1
  • 10
  • `subprocess.Popen("""printf "operator: 'finger ${USER}|grep Name:| cut -f3 -d:(${USER})\n" > temp_header""", shell=True).communicate()` (this is a cheat.. just threw it in there). But why are you referring to windows and their command prompt when you're using bash? I'm confused...:) – Torxed Jun 06 '14 at 13:05
  • you cannot run this command from windows. You should first connect to the server, for instance via putty, and once on the server you can run your script locally – daouzli Jun 06 '14 at 13:09
  • @Torxed It's a script running on a unix machine and I need to have it run in windows. – user2049004 Jun 06 '14 at 13:10
  • Do you realize that you have in fact a script that generates an other script named `temp_header` (not exactly a script, the executable part is after `operator: '``)? – daouzli Jun 06 '14 at 13:21
  • and furthermore the generated script uses `finger` that doesn't necessarily exist in all unix like systems – daouzli Jun 06 '14 at 13:22
  • @user2049004 Well, that didn't clarify things all to much i'm afraid. This script runs on your Linux machine, which generates a windows-bat script? or? – Torxed Jun 06 '14 at 13:36
  • That's a really poor way to get the current user's real name. `finger` is disabled most places these days anyhow. The standard way to do this would be `getent`. See also http://stackoverflow.com/questions/833227/whats-the-easiest-way-to-get-a-users-full-name-on-a-linux-posix-system. – tripleee Jun 06 '14 at 13:37
  • http://stackoverflow.com/questions/3438634/how-to-get-logged-in-users-full-name-in-windows is C++ but appears to be close to a real answer for Windows. – tripleee Jun 06 '14 at 13:43
  • @Torxed It's part of a script to add some functionality to a CVS repository. I need to rewrite the bash scripts, initially used on a linux machine, in python to run on a windows machine. I'm not sure what else to tell you. As I said I have pretty much zero experience with shell scripts, I think I was just confused about the '|' symbols in the line. Based on the rest of the scripts I have, I wouldn't think that I should be printing the whole 'operator: 'finger ${USER}|grep Name:| cut -f3 -d:(${USER})\n' as a string. Seems like the header file would just want the username... – user2049004 Jun 06 '14 at 14:00
  • @user2049004 I got confused because shell scripts are Unix specific and can't run on Windows. You're looking at converting a script into something that runs on Windows, i guess that's where Python comes in? In that case, have a look at **daouzli**s answer because it will get you a bit along the way, but you won't be able to execute the `| cut` in windows because there's no such command there. I'd be looking to converting the entire sequence instead. – Torxed Jun 06 '14 at 20:17
  • possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – jb. Jun 06 '14 at 21:21

2 Answers2

1

If i understand well, you want to get the current user full name. Then, the command line seems to be

printf "operator: `finger ${USER} | grep Name: | cut -f3 -d:` \n"

edit: i found that about getting user full name on linux directly in python : full unix username of a user

Community
  • 1
  • 1
FunkySayu
  • 7,641
  • 10
  • 38
  • 61
  • ... except the proper use of `printf` would dictate `printf "operator: %s\n" "$(finger "$USER" | grep Name: | cut -f3 .d:)"` ... and still, the proper way to do that is with `getent`, not `finger` – tripleee Jun 06 '14 at 15:24
0

The following

printf "operator: 'finger ${USER}|grep Name:| cut -f3 -d:(${USER})\n" > temp_header

can be interpreted as: create the file temp_header. In that file print the message operator: 'finger ${USER}|grep Name:| cut -f3 -d:(${USER})\n where you replace ${USER} by the content of the environment variable $USER

So in Python you should do the following:

import os

with open('temp_header', 'w') as f:
    user = os.environ['USER']
    txt = "operator: 'finger {0}|grep Name:| cut -f3 -d:({1})\n".format(user, user)
    f.write(txt)

But note that the temp_header contains a command that cannot be executed by windows unless using a unix like shell as bash.

daouzli
  • 15,288
  • 1
  • 18
  • 17