Run script for each line and do something based on output












-6














I have a Python script that someone else made. Inside my bash script I would like to execute it for each line in a txt file, using each line as an argument. Then based on the output of the python script, add that line to a new txt file.



At the moment I run



sudo python tester.py 10.0.1.1 999 user pass


Where "10.0.1.1 999 user pass" would be a line in my txt file.



If that succeeds it spits out "SUCCESS!". This is where I would like to copy that line to a new file.



Now, to make it more complicated I would like to change the python script based on the start of the line. This isnt 100% needed but saves me spliiting the txt file before hand.



eg. If the line starts with "10" use script 1. If it starts with "192" use script 2. Both have the same "SUCCESS!" output.



I have tried this



cat list.txt | while read line
do
python tester.py $line
if $(...) == "S"*
then
$line >> good.txt
fi
done


Something isnt right though.



Hopefully that made enough sense for someone to be able to help me.



Chris










share|improve this question




















  • 2




    StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. Please update your question to show what you have already tried in a Minimal, Complete, and Verifiable example. For further information, please see How to Ask, and take the tour :)
    – Barmar
    Nov 20 at 20:11










  • Use a while read loop to read the file line by line into variables. You can test the first variable to decide which python script to run. Then run the script, passing the variables as arguments. Use $(...) to capture the output, and an if statement to test if it's SUCCESS! and then write to the file.
    – Barmar
    Nov 20 at 20:13










  • Nothing there is anything more than basic shell scripting. What part of it is confusing you?
    – Barmar
    Nov 20 at 20:14










  • I am still a beginner. The little I know if from looking at examples. I have tried this "cat list.txt | while read line; do python tester.py "$line"; done" but nothing prints
    – Chris
    Nov 20 at 20:20












  • Don't put $line in quotes. The python script expects 4 separate arguments, that will combine the entire line as one argument.
    – Barmar
    Nov 20 at 20:22
















-6














I have a Python script that someone else made. Inside my bash script I would like to execute it for each line in a txt file, using each line as an argument. Then based on the output of the python script, add that line to a new txt file.



At the moment I run



sudo python tester.py 10.0.1.1 999 user pass


Where "10.0.1.1 999 user pass" would be a line in my txt file.



If that succeeds it spits out "SUCCESS!". This is where I would like to copy that line to a new file.



Now, to make it more complicated I would like to change the python script based on the start of the line. This isnt 100% needed but saves me spliiting the txt file before hand.



eg. If the line starts with "10" use script 1. If it starts with "192" use script 2. Both have the same "SUCCESS!" output.



I have tried this



cat list.txt | while read line
do
python tester.py $line
if $(...) == "S"*
then
$line >> good.txt
fi
done


Something isnt right though.



Hopefully that made enough sense for someone to be able to help me.



Chris










share|improve this question




















  • 2




    StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. Please update your question to show what you have already tried in a Minimal, Complete, and Verifiable example. For further information, please see How to Ask, and take the tour :)
    – Barmar
    Nov 20 at 20:11










  • Use a while read loop to read the file line by line into variables. You can test the first variable to decide which python script to run. Then run the script, passing the variables as arguments. Use $(...) to capture the output, and an if statement to test if it's SUCCESS! and then write to the file.
    – Barmar
    Nov 20 at 20:13










  • Nothing there is anything more than basic shell scripting. What part of it is confusing you?
    – Barmar
    Nov 20 at 20:14










  • I am still a beginner. The little I know if from looking at examples. I have tried this "cat list.txt | while read line; do python tester.py "$line"; done" but nothing prints
    – Chris
    Nov 20 at 20:20












  • Don't put $line in quotes. The python script expects 4 separate arguments, that will combine the entire line as one argument.
    – Barmar
    Nov 20 at 20:22














-6












-6








-6







I have a Python script that someone else made. Inside my bash script I would like to execute it for each line in a txt file, using each line as an argument. Then based on the output of the python script, add that line to a new txt file.



At the moment I run



sudo python tester.py 10.0.1.1 999 user pass


Where "10.0.1.1 999 user pass" would be a line in my txt file.



If that succeeds it spits out "SUCCESS!". This is where I would like to copy that line to a new file.



Now, to make it more complicated I would like to change the python script based on the start of the line. This isnt 100% needed but saves me spliiting the txt file before hand.



eg. If the line starts with "10" use script 1. If it starts with "192" use script 2. Both have the same "SUCCESS!" output.



I have tried this



cat list.txt | while read line
do
python tester.py $line
if $(...) == "S"*
then
$line >> good.txt
fi
done


Something isnt right though.



Hopefully that made enough sense for someone to be able to help me.



Chris










share|improve this question















I have a Python script that someone else made. Inside my bash script I would like to execute it for each line in a txt file, using each line as an argument. Then based on the output of the python script, add that line to a new txt file.



At the moment I run



sudo python tester.py 10.0.1.1 999 user pass


Where "10.0.1.1 999 user pass" would be a line in my txt file.



If that succeeds it spits out "SUCCESS!". This is where I would like to copy that line to a new file.



Now, to make it more complicated I would like to change the python script based on the start of the line. This isnt 100% needed but saves me spliiting the txt file before hand.



eg. If the line starts with "10" use script 1. If it starts with "192" use script 2. Both have the same "SUCCESS!" output.



I have tried this



cat list.txt | while read line
do
python tester.py $line
if $(...) == "S"*
then
$line >> good.txt
fi
done


Something isnt right though.



Hopefully that made enough sense for someone to be able to help me.



Chris







python linux bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 21:04

























asked Nov 20 at 20:09









Chris

486




486








  • 2




    StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. Please update your question to show what you have already tried in a Minimal, Complete, and Verifiable example. For further information, please see How to Ask, and take the tour :)
    – Barmar
    Nov 20 at 20:11










  • Use a while read loop to read the file line by line into variables. You can test the first variable to decide which python script to run. Then run the script, passing the variables as arguments. Use $(...) to capture the output, and an if statement to test if it's SUCCESS! and then write to the file.
    – Barmar
    Nov 20 at 20:13










  • Nothing there is anything more than basic shell scripting. What part of it is confusing you?
    – Barmar
    Nov 20 at 20:14










  • I am still a beginner. The little I know if from looking at examples. I have tried this "cat list.txt | while read line; do python tester.py "$line"; done" but nothing prints
    – Chris
    Nov 20 at 20:20












  • Don't put $line in quotes. The python script expects 4 separate arguments, that will combine the entire line as one argument.
    – Barmar
    Nov 20 at 20:22














  • 2




    StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. Please update your question to show what you have already tried in a Minimal, Complete, and Verifiable example. For further information, please see How to Ask, and take the tour :)
    – Barmar
    Nov 20 at 20:11










  • Use a while read loop to read the file line by line into variables. You can test the first variable to decide which python script to run. Then run the script, passing the variables as arguments. Use $(...) to capture the output, and an if statement to test if it's SUCCESS! and then write to the file.
    – Barmar
    Nov 20 at 20:13










  • Nothing there is anything more than basic shell scripting. What part of it is confusing you?
    – Barmar
    Nov 20 at 20:14










  • I am still a beginner. The little I know if from looking at examples. I have tried this "cat list.txt | while read line; do python tester.py "$line"; done" but nothing prints
    – Chris
    Nov 20 at 20:20












  • Don't put $line in quotes. The python script expects 4 separate arguments, that will combine the entire line as one argument.
    – Barmar
    Nov 20 at 20:22








2




2




StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. Please update your question to show what you have already tried in a Minimal, Complete, and Verifiable example. For further information, please see How to Ask, and take the tour :)
– Barmar
Nov 20 at 20:11




StackOverflow is not a free coding service. SO expects you to try to solve your own problem first. Please update your question to show what you have already tried in a Minimal, Complete, and Verifiable example. For further information, please see How to Ask, and take the tour :)
– Barmar
Nov 20 at 20:11












Use a while read loop to read the file line by line into variables. You can test the first variable to decide which python script to run. Then run the script, passing the variables as arguments. Use $(...) to capture the output, and an if statement to test if it's SUCCESS! and then write to the file.
– Barmar
Nov 20 at 20:13




Use a while read loop to read the file line by line into variables. You can test the first variable to decide which python script to run. Then run the script, passing the variables as arguments. Use $(...) to capture the output, and an if statement to test if it's SUCCESS! and then write to the file.
– Barmar
Nov 20 at 20:13












Nothing there is anything more than basic shell scripting. What part of it is confusing you?
– Barmar
Nov 20 at 20:14




Nothing there is anything more than basic shell scripting. What part of it is confusing you?
– Barmar
Nov 20 at 20:14












I am still a beginner. The little I know if from looking at examples. I have tried this "cat list.txt | while read line; do python tester.py "$line"; done" but nothing prints
– Chris
Nov 20 at 20:20






I am still a beginner. The little I know if from looking at examples. I have tried this "cat list.txt | while read line; do python tester.py "$line"; done" but nothing prints
– Chris
Nov 20 at 20:20














Don't put $line in quotes. The python script expects 4 separate arguments, that will combine the entire line as one argument.
– Barmar
Nov 20 at 20:22




Don't put $line in quotes. The python script expects 4 separate arguments, that will combine the entire line as one argument.
– Barmar
Nov 20 at 20:22












2 Answers
2






active

oldest

votes


















1














By quoting $line you're combining all the words in the line into a single argument, but the Python script need 4 separate arguments.



You should read the first field of the line into a variable of its own so you can test it to decide which script to run.



Then assign the output of the script to a variable, test that against SUCESS!, and write to the output file if it matches.



while read -r ip rest
do
case "$ip" in
10.*) script=tester.py ;;
192.*) script=tester2.py ;;
*) echo "IP $ip doesn't start with 10 or 192";
exit;;
esac
result=$(sudo python $script $ip $rest)
if [[ $result == SUCCESS* ]]
then
echo "$ip $rest"
fi
done < list.txt > good.txt





share|improve this answer























  • Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
    – Chris
    Nov 20 at 21:27












  • You have Windows CRLF newlines in your script. Use dos2unix to fix it.
    – Barmar
    Nov 20 at 21:30










  • Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
    – Chris
    Nov 20 at 21:50












  • That should work.
    – Barmar
    Nov 20 at 21:52










  • I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
    – Chris
    Nov 20 at 21:58



















0














Pass the arguments as distinct elements.



while read -a line
do python tester.py "${line[@]}"
done < list.txt > log.txt 2>&1


That will also read the file without starting a pointless cat, and log your output. Edit details as needed.



You could also use a case statement on ${line[0]} to determine which program to run.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400779%2frun-script-for-each-line-and-do-something-based-on-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    By quoting $line you're combining all the words in the line into a single argument, but the Python script need 4 separate arguments.



    You should read the first field of the line into a variable of its own so you can test it to decide which script to run.



    Then assign the output of the script to a variable, test that against SUCESS!, and write to the output file if it matches.



    while read -r ip rest
    do
    case "$ip" in
    10.*) script=tester.py ;;
    192.*) script=tester2.py ;;
    *) echo "IP $ip doesn't start with 10 or 192";
    exit;;
    esac
    result=$(sudo python $script $ip $rest)
    if [[ $result == SUCCESS* ]]
    then
    echo "$ip $rest"
    fi
    done < list.txt > good.txt





    share|improve this answer























    • Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
      – Chris
      Nov 20 at 21:27












    • You have Windows CRLF newlines in your script. Use dos2unix to fix it.
      – Barmar
      Nov 20 at 21:30










    • Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
      – Chris
      Nov 20 at 21:50












    • That should work.
      – Barmar
      Nov 20 at 21:52










    • I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
      – Chris
      Nov 20 at 21:58
















    1














    By quoting $line you're combining all the words in the line into a single argument, but the Python script need 4 separate arguments.



    You should read the first field of the line into a variable of its own so you can test it to decide which script to run.



    Then assign the output of the script to a variable, test that against SUCESS!, and write to the output file if it matches.



    while read -r ip rest
    do
    case "$ip" in
    10.*) script=tester.py ;;
    192.*) script=tester2.py ;;
    *) echo "IP $ip doesn't start with 10 or 192";
    exit;;
    esac
    result=$(sudo python $script $ip $rest)
    if [[ $result == SUCCESS* ]]
    then
    echo "$ip $rest"
    fi
    done < list.txt > good.txt





    share|improve this answer























    • Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
      – Chris
      Nov 20 at 21:27












    • You have Windows CRLF newlines in your script. Use dos2unix to fix it.
      – Barmar
      Nov 20 at 21:30










    • Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
      – Chris
      Nov 20 at 21:50












    • That should work.
      – Barmar
      Nov 20 at 21:52










    • I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
      – Chris
      Nov 20 at 21:58














    1












    1








    1






    By quoting $line you're combining all the words in the line into a single argument, but the Python script need 4 separate arguments.



    You should read the first field of the line into a variable of its own so you can test it to decide which script to run.



    Then assign the output of the script to a variable, test that against SUCESS!, and write to the output file if it matches.



    while read -r ip rest
    do
    case "$ip" in
    10.*) script=tester.py ;;
    192.*) script=tester2.py ;;
    *) echo "IP $ip doesn't start with 10 or 192";
    exit;;
    esac
    result=$(sudo python $script $ip $rest)
    if [[ $result == SUCCESS* ]]
    then
    echo "$ip $rest"
    fi
    done < list.txt > good.txt





    share|improve this answer














    By quoting $line you're combining all the words in the line into a single argument, but the Python script need 4 separate arguments.



    You should read the first field of the line into a variable of its own so you can test it to decide which script to run.



    Then assign the output of the script to a variable, test that against SUCESS!, and write to the output file if it matches.



    while read -r ip rest
    do
    case "$ip" in
    10.*) script=tester.py ;;
    192.*) script=tester2.py ;;
    *) echo "IP $ip doesn't start with 10 or 192";
    exit;;
    esac
    result=$(sudo python $script $ip $rest)
    if [[ $result == SUCCESS* ]]
    then
    echo "$ip $rest"
    fi
    done < list.txt > good.txt






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 22:23

























    answered Nov 20 at 20:28









    Barmar

    418k34243344




    418k34243344












    • Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
      – Chris
      Nov 20 at 21:27












    • You have Windows CRLF newlines in your script. Use dos2unix to fix it.
      – Barmar
      Nov 20 at 21:30










    • Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
      – Chris
      Nov 20 at 21:50












    • That should work.
      – Barmar
      Nov 20 at 21:52










    • I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
      – Chris
      Nov 20 at 21:58


















    • Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
      – Chris
      Nov 20 at 21:27












    • You have Windows CRLF newlines in your script. Use dos2unix to fix it.
      – Barmar
      Nov 20 at 21:30










    • Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
      – Chris
      Nov 20 at 21:50












    • That should work.
      – Barmar
      Nov 20 at 21:52










    • I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
      – Chris
      Nov 20 at 21:58
















    Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
    – Chris
    Nov 20 at 21:27






    Thanks I didnt spot this. When I try it I get "test.sh: line 4: syntax error near unexpected token $'inr'' 'est.sh: line 4: case "$ip" in"
    – Chris
    Nov 20 at 21:27














    You have Windows CRLF newlines in your script. Use dos2unix to fix it.
    – Barmar
    Nov 20 at 21:30




    You have Windows CRLF newlines in your script. Use dos2unix to fix it.
    – Barmar
    Nov 20 at 21:30












    Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
    – Chris
    Nov 20 at 21:50






    Thanks I cant beleieve I missed that. Changed it over in Notepad ++. I am only getting the errors printing in the output. The reply starts with "SUCCESS!" but there is some other crap after it. I figured adding a star would allow for that? Like this if [[ $result == SUCCESS* ]]
    – Chris
    Nov 20 at 21:50














    That should work.
    – Barmar
    Nov 20 at 21:52




    That should work.
    – Barmar
    Nov 20 at 21:52












    I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
    – Chris
    Nov 20 at 21:58




    I thought so. It might be worth mentioning that the python script isnt printing anything. It was printing with my first attempt (after you told me to loose the quotes) but not with what you gave me.
    – Chris
    Nov 20 at 21:58













    0














    Pass the arguments as distinct elements.



    while read -a line
    do python tester.py "${line[@]}"
    done < list.txt > log.txt 2>&1


    That will also read the file without starting a pointless cat, and log your output. Edit details as needed.



    You could also use a case statement on ${line[0]} to determine which program to run.






    share|improve this answer


























      0














      Pass the arguments as distinct elements.



      while read -a line
      do python tester.py "${line[@]}"
      done < list.txt > log.txt 2>&1


      That will also read the file without starting a pointless cat, and log your output. Edit details as needed.



      You could also use a case statement on ${line[0]} to determine which program to run.






      share|improve this answer
























        0












        0








        0






        Pass the arguments as distinct elements.



        while read -a line
        do python tester.py "${line[@]}"
        done < list.txt > log.txt 2>&1


        That will also read the file without starting a pointless cat, and log your output. Edit details as needed.



        You could also use a case statement on ${line[0]} to determine which program to run.






        share|improve this answer












        Pass the arguments as distinct elements.



        while read -a line
        do python tester.py "${line[@]}"
        done < list.txt > log.txt 2>&1


        That will also read the file without starting a pointless cat, and log your output. Edit details as needed.



        You could also use a case statement on ${line[0]} to determine which program to run.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 20:30









        Paul Hodges

        2,7581321




        2,7581321






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400779%2frun-script-for-each-line-and-do-something-based-on-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen