[英]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.