简体   繁体   中英

How to take the select word from python code and printing in script shell after calling the script from python?

I have the code python

import os
# put the words you want to match into a list
matching_words = ["apple", "pear", "car", "house"]

# get some text from the user 
user_string = input("Please enter some text:")

# loop over each word in matching_words and check if it is a substring of user_string
for word in matching_words:
    if word in user_string:
        print("\n{} is in the user string\n\n".format(word))
        os.system('/home/raed/Desktop/1.sh')

And I have this code in script

#! /bin/bash
########################
if [ "$2" = "pear" ]; then
    echo " This is '$2'"
elif [ "$2" = "apple" ]; then
    echo " This is '$2'"
else
    echo " This is '$2'"
exit 0

So If I choose any word from list of python code I need the script recognize and print it

I have got this error How to solve it ?!!

Please enter some text:apple

apple is in the user string


/home/raed/Desktop/1.sh: line 10: syntax error: unexpected end of file

You need to include your word as an argument in your python program.

Following is a snippet of code with some minor revisions to your program.

import os
# put the words you want to match into a list
matching_words = ["apple", "pear", "car", "house"]

# get some text from the user 
user_string = input("Please enter some text:")

# loop over each word in matching_words and check if it is a substring of user_string
for word in matching_words:
    if word in user_string:
        com = '/home/craig/Python_Programs/Script/1.sh ' + word
        print("\n{} is in the user string\n\n".format(word))
        os.system(com)

Also, in your bash script, you will want to check argument #1 ($1) in lieu of argument #2 ($2) as noted in the following tweaks to your script.

#! /bin/bash
########################
if [ "$1" = "pear" ]; then
    echo " This is '$1'"
elif [ "$1" = "apple" ]; then
    echo " This is '$1'"
else
    echo " This is '$1'"
exit 0

fi

Following is a sample of the output from my terminal.

Una:~/Python_Programs/Script$ python3 Script.py 
Please enter some text:apple

apple is in the user string


 This is 'apple'
Una:~/Python_Programs/Script$ python3 Script.py 
Please enter some text:pear

pear is in the user string


 This is 'pear'

I believe that will net you the results you are after.

Regards,

You forgot to terminate if statement in bash script with fi

#! /bin/bash ######################## 
if [ "$2" = "pear" ]; 
then 
   echo " This is '$2'" 
elif [ "$2" = "apple" ];
then 
  echo " This is '$2'" 
else 
  echo " This is '$2'" 
exit 0
fi

Update

you have to pass arguments to your script suggested in this post: https://stackoverflow.com/a/14892402/10968621

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM