Trying to create bash script to run a script and input some usernames and return a new line

Viewed 57

Creating a bash script that is running against a wiki page to check for contributions right now. The script currently requires you to enter each username 1 by 1 and then press enter and it runs against the entirety of the list.

I want to do something like this

#! /bin/bash

users="user1,user2,user3,user4,user5 etc"

echo $users \n | <script.py>

But I cant get it to return the new line and run automatically, it currently just enters the users but wont "press enter" for me, lazy I know but trying to learn how to script and this seemed like a good in.

Any help would be greatly appreciated, thanks!

EDIT:

Here is the portion of the python code that requires an input of aliases/usernames and then when the user (me) has entered them you press enter on the KB and the script runs

    aliases_input = str(input("Enter comma separated alias(es): "))
    aliases = aliases_input.split(",")
    summary = []
    try:
        for alias in aliases:
            df, total_activity_count = get_contributions(alias)
            summary.append({"alias": alias, "totalContributions": total_activity_count})
            summary_table = pd.DataFrame(data=summary)

            print(
                "Getting total contributions for:",
                alias,
                "\n",
                df,
                "\n",
                "Total Contributions:",
                total_activity_count,
                "\n",
            )
        print("Summary of all aliases", "\n", summary_table)
    except UnboundLocalError:
        pass```

2 Answers

Let me reply with a new answer because your edit also changed the problem description. The current version requires the user inputs to be comma separated and this format is now explicitly exploited in the (newly posted) Python script. The general layout is similar to before.

(1) Create the bash script Script.sh to print the string with users to the screen.

#!/bin/bash
StrArray="user1, user2, user3"
echo "$StrArray"

The output which is printed to the screen can subsequently serve as the input for the Python script ReadStdin.py.

(2) My content of ReadStdin.py is given below. Some short remarks are:

  • I just created some random output in get_contributions in order to get df and total_activity_count.
  • For brevity, I omitted the try-except statements.
  • Your posted code creates the pandas dataframe inside the loop. This seems inefficient as it can be done once after all data has been appended.

Run ./Script.sh | ./ReadStdin.py

    #!/usr/local/bin/python3
    import pandas as pd
    import random
    import sys
    
    # Contribution function
    def get_contributions(alias):
        df = random.randrange(10)
        total_activity_count = random.randrange(10,15)
        return df, total_activity_count
    
    aliases_input = sys.stdin.readlines()[0] # String in first element of list
    aliases = aliases_input.strip().split(",")
    summary = []
    
    for alias in aliases:
        df, total_activity_count = get_contributions(alias)
        summary.append({"alias": alias, "totalContributions": total_activity_count})
    
        print(
                "Getting total contributions for:",
                alias,
                "\n",
                df,
                "\n",
                "Total Contributions:",
                total_activity_count,
                "\n",
                )
    summary_table = pd.DataFrame(data=summary)
    print("Summary of all aliases", "\n", summary_table)

I created the bash script

#!/bin/bash
StrArray=("user1" "user2" "user3")
for val in ${StrArray[@]}; do
  echo "$val"
done

and the Python code

#!/usr/local/bin/python3
import sys
data = sys.stdin.readlines()

print("--- PYTHON STRINGS ---")
print(data)

If I run ./Script.sh | ./ReadStdin.py, then the content of data is ['user1\n', 'user2\n', 'user3\n'] for me. That is, the newline character is indeed already included (as remarked by @Erwin). I'm not sure whether this answers your question but this is the best I can do without having any further details on <script.py>.

Related