要求
腳本實(shí)現(xiàn)在A機(jī)器殿漠,遠(yuǎn)程安裝軟件package到B機(jī)器
思路
將需要安裝的軟件包package從A拷貝到B,然后從A機(jī)器ssh到B遠(yuǎn)程執(zhí)行安裝腳本
為了用戶能更方便的安裝xx服務(wù),決定用shell寫一個(gè)工具贱纠,實(shí)現(xiàn)在本地遠(yuǎn)程安裝服務(wù)包耳峦,限時(shí)1天,由于之前shell寫得少窜司,實(shí)現(xiàn)過(guò)程中遇到很多比較基礎(chǔ)的問(wèn)題讽坏,記錄一下
實(shí)現(xiàn)version1
業(yè)務(wù)相關(guān)信息用xxx param4等屏蔽
#!/bin/bash
function help()
{
#usage
cat << HELP
------------------------------------------------------------------------------
please input 4 parameters in order:
1st: path of xxx package,
2nd: ip of the host which xxx will install
3rd: root's paasword of the host you inputted just now(2nd parameter)
4th: param4
Example:
/opt/test/xx.zip 192.168.0.2 YourPaasWord param4
HELP
}
function install()
{
read -a array
echo ${array[*]}
scp ${array[0]} root@${array[1]}:/
echo "bash setup.sh -param ${array[3]}"
ssh root@${array[1]} "mkdir -p /opt/test && \
cd /opt/test && \
cp /xx-*.zip ./ && \
unzip xx-*.zip && \
cd /opt/test/bin && \
bash setup.sh -param ${array[3]} "
# bash setup.sh -param ${array[3]} >/dev/null 2>&1 &"
################################################
# ssh root@${array[1]} 'mkdir -p /opt/test && \
# cd /opt/test && \
# cp /xx-*.zip ./ && \
# unzip xx-*.zip && \
# cd /opt/test/bin && \
# bash setup.sh -param ${array[3]} '
################################################
}
while [ 1 ]
do
help
install
sleep 1
done
問(wèn)題記錄
-
shell語(yǔ)言中&& & ; 區(qū)別
If previous command failed with ; the second one will run.
But with && the second one will not run.This is a "lazy" logical "AND" operand between operations.
If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.
-
傳參接收不到
按照被注釋調(diào)ssh寫法,即
################################################
# ssh root@${array[1]} 'mkdir -p /opt/test && \
# cd /opt/test && \
# cp /xx-*.zip ./ && \
# unzip xx-*.zip && \
# cd /opt/test/bin && \
# bash setup.sh -param ${array[3]} '
################################################
ssh執(zhí)行bash setup.sh -param ${array[3]}
命令時(shí)例证,${array[3]}
值一直為空路呜,不管怎么樣都傳遞不進(jìn)去。
中間google了很多答案织咧,都沒(méi)解決胀葱,后來(lái)抱著解決問(wèn)題優(yōu)先的態(tài)度,打算先把變量值寫到遠(yuǎn)端主機(jī)的一個(gè)臨時(shí)文件里笙蒙,用的時(shí)候再讀鹊钟臁(用tempvar=$(cat /opt/test/bin/temp.txt)
或tempvar=$(< /opt/test/bin/temp.txt)
讀取到變量tempvar
,再使用)捅位,試了也不行轧葛;
后來(lái)折騰了一個(gè)多小時(shí),發(fā)現(xiàn)是低級(jí)錯(cuò)誤(初學(xué)者的代價(jià)啊cry)艇搀,將ssh需要執(zhí)行的命令用"
引起了(而非'
)就好了尿扯,即ssh root@${array[1]} 'mkdir -p /opt/test && \
mkdir前的'
以及最后的'
換成"
貼一個(gè)區(qū)分單引號(hào)、雙引號(hào)焰雕、反引號(hào)的鏈接:
http://www.cnblogs.com/gx-303841541/archive/2012/10/25/2740333.html
-
ssh登陸到遠(yuǎn)端主機(jī)執(zhí)行的命令怎么能不顯示
使用>/dev/null 2>&1重定向(源碼中被注釋掉了# bash setup.sh -param ${array[3]} >/dev/null 2>&1 &"
)
>/dev/null 2>&1是什么鬼衷笋?
>is for redirect
/dev/null is a black hole where any data sent, will be discarded
2 is the file descriptor for Standard Error
> is for redirect
& is the symbol for file descriptor (without it, the following 1 would be considered a filename)
1 is the file descriptor for Standard Out
Therefore >/dev/null 2>&1 is redirect the output of your program to /dev/null. Include both the Standard Error and Standard Out.
Reference
https://stackoverflow.com/questions/6152659/bash-sh-difference-between-and
https://stackoverflow.com/questions/13338870/what-does-at-the-end-of-a-linux-command-mean
https://stackoverflow.com/questions/3314660/passing-variables-in-remote-ssh-command
https://unix.stackexchange.com/questions/163352/what-does-dev-null-21-mean-in-this-article-of-crontab-basics