iT邦幫忙

1

[bash] 在bash script 裡用apt-get

#!/bin/bash
apt-get update   
apt-get install git -y 

以上是我*.sh的程式碼

user@pc:~/$ sudo bash ./test.sh 
E: The update command takes no arguments
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package 

sh裡的程式碼在terminal上直接跑都可以正常運作,請問為什麼用script就不正常呢?
(Ubuntu18.04)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
dragonH
iT邦超人 5 級 ‧ 2020-05-21 11:02:18
最佳解答

我的正常

image

你的情況應該是 apt-get update 時出問題

E: Unable to locate package

這裡應該會說哪個 package 有問題

可以從這著手

參考

看更多先前的回應...收起先前的回應...
wrxue iT邦好手 1 級 ‧ 2020-05-21 11:16:20 檢舉

我也很納悶為什麼它沒說什麼有問題,git我已經裝過了,這程式碼只是小測試,所以有沒有apt-get update都無所謂(雖然upate看起來也是有問題,不知道它到底要什麼arguments)。
我在terminal直接跑apt-get install git是這樣的,應該不會沒辦法locate才對。

user@pc:~/$ sudo apt-get install git -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
git is already the newest version (1:2.17.1-1ubuntu0.7).
0 upgraded, 0 newly installed, 0 to remove and 13 not upgraded.
dragonH iT邦超人 5 級 ‧ 2020-05-21 11:23:28 檢舉

不是 apt-get install git 時出問題阿

你看起來在 apt-get update 就有問題了

喔喔 看錯了

wrxue iT邦好手 1 級 ‧ 2020-05-21 11:26:07 檢舉

sh內改成這樣去跑

#!/bin/bash                
apt-get install git -y  

回應是

Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package 

我知道apt-get update有問題,但apt-get install git也有問題QQ

dragonH iT邦超人 5 級 ‧ 2020-05-21 11:37:56 檢舉

那在commandline 先下 sudo apt-get update

再跑呢?

感覺你的 sources.list 怪怪的

有很大機率不只 git 裝不起來

另外想問一下

你這是 WSL 嗎

我的環境是 WSL1 + ubuntu 18.04

不過我剛開了一台 ec2 instance 跑

也是正常沒問題

wrxue iT邦好手 1 級 ‧ 2020-05-21 11:51:18 檢舉

那在commandline 先下 apt-get update

再跑呢?

還是一樣...

user@pc:~/$ sudo apt-get update
Hit:1 http://tw.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://tw.archive.ubuntu.com/ubuntu bionic-updates InRelease             
Hit:3 http://dl.google.com/linux/chrome/deb stable InRelease                   
Hit:4 http://tw.archive.ubuntu.com/ubuntu bionic-backports InRelease           
Hit:5 http://packages.microsoft.com/repos/vscode stable InRelease              
Get:6 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]    
Hit:7 http://ppa.launchpad.net/ondrej/php/ubuntu bionic InRelease              
Hit:8 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease            
Hit:9 http://ppa.launchpad.net/rvm/smplayer/ubuntu bionic InRelease            
Hit:10 http://ppa.launchpad.net/wireshark-dev/stable/ubuntu bionic InRelease   
Fetched 88.7 kB in 2s (54.0 kB/s)
Reading package lists... Done
user@pc:~/$ sudo bash ./test.sh 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package 

這不是WSL,是電腦進來就是Ubuntu了

dragonH iT邦超人 5 級 ‧ 2020-05-21 11:59:28 檢舉

也有可能是因為你使用 editor

造成的問題

不過都在 linux 下編輯的話

應該是不會有這問題才對

不過還是可以試試看

例如他把換行之類的也讀進去

所以他執行時才會說

E: The update command takes no arguments

因為他可能讀到的是

apt-get update \r

之類的

我猜的

wrxue iT邦好手 1 級 ‧ 2020-05-21 12:04:30 檢舉

我原先使用vim做編輯,試改用gedit還是一樣,這問題好煩啊XD

James iT邦大師 6 級 ‧ 2020-05-21 12:53:30 檢舉

訊息是apt-get update不需要argument,不是說你少給了argument。
在console隨便打apt-get update xxx就會得到同一個訊息,所以顯然你update後面有看不到的字元。

wrxue iT邦好手 1 級 ‧ 2020-05-21 13:14:31 檢舉

的確 在sh中改成

#!/bin/bash
apt-get update && 
apt-get install git -y

apt-get update就可以跑了,但會說'\r': command not found,所以它是有讀到\r
請問這有解嗎,還是就給它讀到也沒差?在linux下的換行不是\n而已嗎,為什麼還會有\r(使用vim editor)

dragonH iT邦超人 5 級 ‧ 2020-05-21 13:18:37 檢舉

這我只有在跨系統的遇到過

你這 script 是完全在 linux 環境下寫的嗎

還是你是在 windows ssh 過去

或者 code 是在網頁 copy 過去之類的

或者你是編輯舊有的 script

wrxue iT邦好手 1 級 ‧ 2020-05-21 13:26:03 檢舉

神人! 這檔案之前的確是在ubuntu下寫的,但有上傳去google drive,因為之前不知道sudo bash test.sh可以以sudo去執行test.sh所以放著沒用,現在下載下來再嘗試,就遇到這樣的問題。

我重新建一個test.sh,什麼都能正常了,包含apt-get install git -y。太感謝你了

dragonH iT邦超人 5 級 ‧ 2020-05-21 13:27:14 檢舉

/images/emoticon/emoticon42.gif

我要發表回答

立即登入回答