簡體   English   中英

源命令在 shell 腳本中運行時找不到文件

[英]Source command not unable to find file when run within a shell script

我有使用以下源命令調用另一個腳本文件的腳本是我的腳本文件

 echo "script starting"
 source common_util.sh
 -- other commands ---

在第 2 行,我收到此錯誤。

No such file or directory/common_util.sh

我什至嘗試過./仍然返回相同的錯誤,

但實際上該文件夾中存在文件, PWD與父腳本和common_util.sh相同

但是,如果我將common_util.sh與命令行分開運行,則source common_util.sh正常工作,但是在上面包含此內容時不起作用。

我在MacOS seirra上運行它

請就此提出建議。

**父腳本**

  echo  "starting script"
  root_folder=$(dirname $0)"/"
  echo "before starting scripts  $root_folder"
  #import common_util script
  source ./common_util1.sh

common_util1.sh

 echo "This is in common_util1.sh"

output

  users/> sh parent_script.sh 
  starting script
  before starting scripts  ./
  : No such file or directory/common_util1.sh

實際上我的腳本是在 windows OS 中編寫的,所以當我嘗試在 Mac OS 中運行該腳本時,它有那些隱藏的托架\r導致 shell 以不同的方式處理文件名然后導致找不到文件錯誤。

因此,我使用dos2unix命令將該腳本文件和支持腳本文件的 rest 轉換為與 Mac OS 兼容,您可以在下面找到安裝dos2unix的鏈接

dos2unix 自制鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM