Linux

如何從 bash 腳本執行其他腳本,例如 bash 和 jython?

  • March 23, 2022

我有一個名為test.sh的腳本,它位於目錄**/home/user**中,程式碼如下:

#!/bin/bash

echo "Execute command number 1 (bash)"
. /opt/IBM/taddm/dist/bin/control.sh
 
echo "Execute command number 2 (jython)"
. /opt/IBM/taddm/dist/bin/collect_logs.jy "@g"

執行命令 1 時,我收到以下消息:

./test.sh: line 5: /opt/IBM/taddm/dist/bin/control.sh: No such file or directory

當我去目的地(/ bin)並從那裡執行命令時,它沒有問題。


執行命令 2 時,我收到以下消息:

/opt/IBM/taddm/dist/bin/collect_logs.jy: line 1: Script: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 2: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 2: $'user@XXXXX:~\a\E[?1034h[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 3: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 3: user@XXXXX:/opt/IBM/taddm/dist/log/support[user@XXXXX: No such file or directory
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 4: total: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 5: -rw-r-----.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 5: $'31mTADDM_ISA_Collection.General.20220323140619.zip\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 6: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 6: user@XXXXX:/opt/IBM/taddm/dist/log/support[user@XXXXX: No such file or directory
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 7: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 7: user@XXXXX:/opt/IBM/taddm/dist/log/support[user@XXXXX: No such file or directory
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 8: total: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 9: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 9: user@XXXXX:/opt/IBM/taddm/dist/log/support[user@XXXXX: No such file or directory
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 10: $'\E[3': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 10: $'J\E[H\E[2J\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 10: user@XXXXX:/opt/IBM/taddm/dist/log/support[user@XXXXX: No such file or directory
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 11: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 11: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 12: total: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 13: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 13: $'34m12:35:27\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 14: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 14: $'34m2022\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 15: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 15: $'34m22\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 16: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 16: $'34mCET\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 17: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 17: $'34mMar\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 18: -rwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 18: $'32mmenu.sh\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 19: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 19: $'34mPM\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 20: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 20: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 21: $'\E[3': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 21: $'J\E[H\E[2J\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 21: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 22: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 22: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 23: total: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 24: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 24: $'34m12:35:27\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 25: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 25: $'34m2022\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 26: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 26: $'34m22\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 27: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 27: $'34mCET\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 28: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 28: $'34mMar\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 29: -rwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 29: $'32mmenu.sh\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 30: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 30: $'34mPM\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 31: -rw-rw-r--.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 32: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 32: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 33: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 33: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 34: total: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 35: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 35: $'34m12:35:27\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 36: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 36: $'34m2022\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 37: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 37: $'34m22\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 38: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 38: $'34mCET\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 39: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 39: $'34mMar\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 40: -rwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 40: $'32mmenu.sh\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 41: drwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 41: $'34mPM\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 42: -rwxrwxr-x.: command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 42: $'32mtest.sh\E[0m\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 43: $'\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 43: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 44: $'\E[3': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 44: $'J\E[H\E[2J\E]0': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 44: $'user@XXXXX:~\a[user@XXXXX': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 46: $'\r': command not found
/opt/IBM/taddm/dist/bin/collect_logs.jy: line 51: syntax error near unexpected token `$'do\r''
'opt/IBM/taddm/dist/bin/collect_logs.jy: line 51: `do

當我去目的地(/ bin)並從那裡執行命令時,它沒有問題。


我也嘗試了不帶文件副檔名的命令(/bin/control & /bin/collect_logs),我嘗試使用source而不是,.但這也無濟於事。

為什麼我不能從我自己的 bash 腳本中執行其他腳本?

謝謝

如果腳本在您的 $PATH 中並且它們是可執行的,則意味著您可以通過鍵入 control.sh或從任何地方啟動它們collect_logs.jy "@g"

你可以做

#!/bin/bash

echo "Execute command number 1 (bash)"
control.sh
 
echo "Execute command number 2 (jython)"
collect_logs.jy "@g"

如果您需要指定目錄,只需省略點:

#!/bin/bash

echo "Execute command number 1 (bash)"
/opt/IBM/taddm/dist/bin/control.sh
 
echo "Execute command number 2 (jython)"
/opt/IBM/taddm/dist/bin/collect_logs.jy "@g"

您可能需要先使它們可執行:

chmod +x /opt/IBM/taddm/dist/bin/control.sh
chmod +x /opt/IBM/taddm/dist/bin/collect_logs.jy

引用自:https://unix.stackexchange.com/questions/696530