函數可以在命令列運行,但不能在腳本內運行

函數可以在命令列運行,但不能在腳本內運行

我有一個 shell 腳本文件,它啟動 ruby​​ 開發伺服器並設定視窗的標題。由於某種原因,它在 OS X 中不起作用,但在 Ubuntu 中卻可以。

這是我的腳本:

[10:24:48] [user@mac site_web]$ tail ./sdev.sh
#!/bin/bash
title "dev server port 3000"
RAILS_ENV=development rails s -p 3000 --debugger

從命令列運行良好,但在腳本內運行失敗。

[10:18:17] [user@mac site_web]$ title "dev server" 
title changed
[10:18:29] [user@mac site_web]$ ./sdev.sh
./sdev.sh: line 2: title: command not found 

最後一行就是問題所在。

和我的標題函數(在我的 ~/.bash_profile 中):

# function for setting terminal titles in OSX
function title {
  printf "\033]0;%s\007" "$1"
  echo "title changed" 
}

我是否需要以不同的方式執行此操作,因為它是在 OSX 上?

編輯:我嘗試將標題函數新增到 /Users/[me]/.bashrc 中,但仍然收到錯誤。

答案1

我沒有可用的 Mac 來嘗試此操作,但是如果您在腳本中包含對函數來源的引用,該怎麼辦:

#!/bin/bash
. $HOME/.bash_profile
title "dev server port 3000"
RAILS_ENV=development rails s -p 3000 --debugger

http://ubuntuforums.org/showthread.php?t=664657

相關內容