一部のコンテンツが追加されたファイルからコマンドを実行するには?

一部のコンテンツが追加されたファイルからコマンドを実行するには?

次のようないくつかのコマンドを保存するファイルがあります。

sudo PYSPARK_DRIVER_PYTHON=/bin/python2.7 PYSPARK_PYTHON=/bin/python2.7 SPARK_CONF_DIR=/configuration/spark2 /spark2.1/bin/spark-submit --driver-memory 2g --executor-memory 4g --num-executors 100 --jars /lib/json-serde-1.3.7-jar-with-dependencies.jar

上記のコマンドは、ファイル名(例:)が追加された場合にのみ実行されますfile.py。したがって、コマンドラインでこれを実行するには、次のように入力します。

sudo PYSPARK_DRIVER_PYTHON=/bin/python2.7 PYSPARK_PYTHON=/bin/python2.7 SPARK_CONF_DIR=/configuration/spark2 /spark2.1/bin/spark-submit --driver-memory 2g --executor-memory 4g --num-executors 100 --jars /lib/json-serde-1.3.7-jar-with-dependencies.jar file.py

ちなみに、このコマンドを使っていくつかのファイルを実行したいのですが、実行するたびにファイル名を指定したいと思います。このコマンドをというファイルに保存しようとしましたcommand

cat command echo file.py | bash

しかし、うまくいかないようです。どうすればいいですか?

ベストアンサー1

スクリプトで変数を設定し、クイックステータスチェックを実行します。

pyfile="file.py"
sudo PYSPARK_DRIVER_PYTHON=/bin/python2.7 PYSPARK_PYTHON=/bin/python2.7 SPARK_CONF_DIR=/configuration/spark2 /spark2.1/bin/spark-submit --driver-memory 2g --executor-memory 4g --num-executors 100 --jars /lib/json-serde-1.3.7-jar-with-dependencies.jar "${pyfile?python script not specified}"

変数が定義されていないか空の場合、コンストラクタは${var?message}エラーを発生して表示します。messagevar

を使用してデフォルト値を指定することもできます${var-defaultvalue}

より簡単な呼び出し機能に設定することもできます。

runjob() {
    sudo PYSPARK_DRIVER_PYTHON=/bin/python2.7 PYSPARK_PYTHON=/bin/python2.7 SPARK_CONF_DIR=/configuration/spark2 /spark2.1/bin/spark-submit --driver-memory 2g --executor-memory 4g --num-executors 100 --jars /lib/json-serde-1.3.7-jar-with-dependencies.jar "${1?python script not specified}"
}

runjob "/path/to/file.py"
runjob "/path/to/some/other/file.py"

おすすめ記事