[英]How to take the select word from python code and printing in script shell after calling the script from 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')
我在脚本中有这段代码
#! /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
因此,如果我从 python 代码列表中选择任何单词,我需要脚本识别并打印它
我有这个错误如何解决?!
Please enter some text:apple
apple is in the user string
/home/raed/Desktop/1.sh: line 10: syntax error: unexpected end of file
您需要将您的单词作为参数包含在您的 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:
com = '/home/craig/Python_Programs/Script/1.sh ' + word
print("\n{} is in the user string\n\n".format(word))
os.system(com)
此外,在您的 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
以下是我的终端的输出示例。
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'
我相信这会给你带来你想要的结果。
问候,
您忘记使用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
更新
您必须将参数传递给这篇文章中建议的脚本: https ://stackoverflow.com/a/14892402/10968621
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.