iT邦幫忙

0

Linux-Centos :bash shell自動安裝腳本(自動取代文字、yum等)

各位好:
請問如果想要讓centos自動安裝yum+ 開啟某個文件檔 (然後搜尋符合字串取代之)+自動回應選項,
以下有可能寫腳本辦到嗎?

#!/bin/bash
echo "安裝中..."
rpm -Uvh http://nginx.org/packages/centos/xxxxxx.noarch.rpm

請問
1●假如要讓他自動開啟/etc/sudoers這個文件,
然後搜尋

root	ALL=(ALL) ALL 

自動改成

#root	ALL=(ALL) ALL 

或是將
#PermitRootLogin no

改成

PermitRootLogin no

然後自動存檔?

2●假設腳本在進行安裝mysql時候,
#yum -y mysql
接著螢幕上會要你輸入

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
...
... Success!

請問每行的"y"啦、密碼啦~要如何讓script自動幫你輸入?

isthome iT邦新手 4 級 ‧ 2015-01-04 13:26:44 檢舉
再請問進階點的,
如果是這樣有多種情況下,
#PermitRootLogin no  
#PermitRootLogin yes
PermitRootLogin yes
#PermitRootLogin            yes    (n個空格)
PermitRootLogin no

一律修改成:

PermitRootLogin no



我是使用2段式轉換:

sed -i 's/\(PermitRootLogin\)\s\+ yes/\1 no/' /etc/sudoers
sed -i 's/^#\(PermitRootLogin no\)/\1/' /etc/sudoers

1.先將yes改成no
2.將註解拿掉(如果有)
請問是否有更好的方式呢?
謝謝

isthome iT邦新手 4 級 ‧ 2015-01-04 13:35:13 檢舉
以下是實驗過,
#PermitRootLogin yes
#PermitRootLogin  yes
#PermitRootLogin no
#PermitRootLogin  no
PermitRootLogin  yes
PermitRootLogin yes
PermitRootLogin  no
PermitRootLogin no


sed -i 's/\(PermitRootLogin\)\s\+yes/\1 no/' 1.txt
sed -i 's/^#\(PermitRootLogin\)\s\+\(no\)/\1 no/' 1.txt


變成:
PermitRootLogin no
PermitRootLogin no
PermitRootLogin no
PermitRootLogin no
PermitRootLogin no
PermitRootLogin no
PermitRootLogin  no
PermitRootLogin no
wiseguy iT邦超人 1 級 ‧ 2015-01-05 08:34:39 檢舉
一次就可以了
sed -i 's/^#\?\(PermitRootLogin\)\s\+.*$/\1 no/' 1.txt
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
wiseguy
iT邦超人 1 級 ‧ 2015-01-03 09:31:45
最佳解答

你的問題答案是"可以"。

  1. 請使用 sed,例如:

    <pre class="c" name="code">sed -i 's/^root ALL=(ALL) ALL/#&/;s/^#(PermitRootLogin no)/\1/' /etc/sudoers

  2. 請參考 mysql 參數 -p 可直接指定密碼。

我要發表回答

立即登入回答