カレントディレクトリを変えずに別のディレクトリからプログラムを実行

カレントディレクトリを変更せずにコマンドを実行するためには

$ (cd ./different/directory/ && ls)

などのように、()でコマンドをくくることによりサブプロセスが生成され、可能となる。

 

また、スクリプトで別のディレクトリからプログラムを実行するには

$(cd ./different/directory/ && python ./different/directory/xxxx.py)

とすると良い。

 

また、

import: not found

Syntax error: "(" unexpected

 

のようなエラーで実行できない場合は実行するプログラム

(上の例では./different/directory/automate_comp_jm.py)の一番上に

 

#!/usr/bin/env python

と書く。
これはShebang(Shebang (Unix) - Wikipedia)と呼ばれるもので、
これによって実行が可能となる。

permission deniedのばあいは
chmod 777 ~.py
で権限を変える。


参考文献:

https://superuser.com/questions/271986/execute-a-command-from-another-directory-in-bash

 

https://stackoverflow.com/questions/27455857/import-not-found-when-trying-to-run-a-python-script/27456774