假如 Python 脚本 - test_shell_python.py

import argparse

def parse_args():
    """Parse input arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', dest='visualize', default='OFF')
    parser.add_argument('-o', dest='outputpath')
    args = parser.parse_args()
    return args

if name == "__main__":
    args = parse_args()

    if args.visualize == 'ON':
        print("Visualize is ON.")
        outputpath = args.outputpath
        print("Output to ", outputpath)
    else:
        print("Visualize is OFF.")

Shell 调用 test_shell_python.py 脚本并传递参数的方法:

#!/bin/bash
visualize=$1
outputpath=$2
DIR=$(dirname "$0")
python $DIR/ test_shell_python.py  -v $visualize -o $outputpath

运行 shell 脚本并传递参数:

sh test_shell_python.sh ON /home/ubuntu/test/
Last modification:October 9th, 2018 at 09:31 am