【发布时间】:2017-10-04 18:43:23
【问题描述】:
我想知道在使用 argparse 之后是否可以将 python 脚本作为 bash 作业运行?我尝试这样做,首先使用 argparse 在 python 脚本文件中传递一个参数,然后使用命令:
bash bash1.sh
运行将运行 python 脚本文件的 bash 文件。这导致了一个错误
message_script.py: error: too few arguments
此错误是由于无法识别 argparse 参数而导致的。有什么方法可以让我使用 argparse 传递参数,然后将 python 脚本作为批处理作业运行?
message_script.py 的内容:
import sys
import random
import numpy as np
import argparse
from PIL import Image
import matplotlib.pyplot as plt
#Input of Data File
#Proprtion Alpha
parser = argparse.ArgumentParser()
parser.add_argument("Alpha", help = "proportion of pixels to be embedded in cover image")
args = parser.parse_args()
alpha = args.Alpha
alpha = float(alpha) #args.alpha came back as a string hence needed to be converted to float
#n is the number of pixels in cover image
n = 512*512 #since all images in BossBase testbench are 512 by 512
#Length of message to be embedded in cover image
length_msg = (alpha * n)
len_msg = int(length_msg)
#Generates a random message, a 1D vector consisting of 1s and 0s
msg = np.random.randint(2, size= len_msg)
#Save message in text format representing each bit as 0 or 1 on a separate line
msg_text_file = open("/home/ns3/PycharmProjects/untitled1/msg_file.txt", "w") #Path of Data File
msg_text_file.write("\n".join(map(lambda x: str(x), msg)))
msg_text_file .close()
bash1.sh 的内容:
#!/bin/sh
python message_script.py
【问题讨论】:
-
你能分享更多细节吗?你愿意达到什么目标?可以分享一下 bash1.sh 和 message_script.py 的内容吗?
-
从已知情况来看,bash 脚本不会传递参数。你能发布 bash 脚本吗?
-
@MosheZada 我刚刚附上了 message_script.py 和 bash 文件 bash1.sh 的内容。使用 python 脚本,我正在创建一个文本文件,而 bash 文件只是运行该 python 脚本来创建文本文件。
-
@3k- 我还发布了 bash 脚本
-
脚本在单独运行和批处理运行之间不会保留
alpha的任何内存。
标签: python bash batch-file argparse