bash 腳本中 apt-get 失敗?

bash 腳本中 apt-get 失敗?

我有幾台機器(運行 Ubuntu LTS 12.4 64 位元)需要以相同的方式進行配置,因此我創建了一個 shell 腳本,它將在機器第一次啟動時自動運行。它在大多數情況下都有效,但任何對 apt-get 的呼叫都會失敗。作為範例,這裡有兩個我要執行的命令。

debconf-set-selections ./files/ldap.preseed
apt-get -y install ldap-auth-client

如果我直接輸入這些行,結果如下:

root@spare:/tmp/scripts# debconf-set-selections ./files/ldap.preseed
root@spare:/tmp/scripts# apt-get -y install ldap-auth-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be installed:
  ldap-auth-config libnss-ldap libpam-ldap
The following NEW packages will be installed:
  ldap-auth-client ldap-auth-config libnss-ldap libpam-ldap
0 upgraded, 4 newly installed, 0 to remove and 67 not upgraded.
Need to get 0 B/200 kB of archives.
After this operation, 877 kB of additional disk space will be used.
Preconfiguring packages...
[and so on...]

但如果我執行僅包含這些行的 shell 腳本,則會發生這種情況:

root@spare:/tmp/scripts# ./ldap.cr
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package ldap-auth-client

當我使用 sudo 運行腳本或先將其 chown 為 root 時,也會發生相同的情況。直接呼叫或透過 shell 腳本呼叫對程式來說有什麼區別?是否有可能以其他方式說服它?

編輯:更多資訊:

root@spare:/tmp/scripts# apt-cache policy ldap-auth-client
ldap-auth-client:
  Installed: (none)
  Candidate: 0.5.3
  Version table:
    0.5.3.0
       500 http://us.archive.ubuntu.com/ubuntu/ precise/main amd64 Packages

答案1

在執行腳本之前透過 dos2unix 運行該腳本會使其正常工作。我猜測 apt-get 正在讀取行末尾的額外 \r 作為包名稱的一部分,從而尋找 ldap-auth-client\r (不存在)而不是 ldap-auth -客戶端(確實如此)。

答案2

嘗試執行以下命令

apt-get update         

執行之前apt-get 安裝命令

也要確保網址/etc/apt/sources.list可用。

相關內容