[英]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我有代码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因此,如果我从 python 代码列表中选择任何单词,我需要脚本识别并打印它
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.您需要将您的单词作为参数包含在您的 python 程序中。
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.此外,在您的 bash 脚本中,您需要检查参数 #1 ($1) 来代替参数 #2 ($2),如以下脚本调整中所述。
#! /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
您忘记使用
fi
终止 bash 脚本中的if
语句
#! /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您必须将参数传递给这篇文章中建议的脚本: https ://stackoverflow.com/a/14892402/10968621
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.