要求
腳本實現在A機器,遠程安裝軟件package到B機器
思路
將需要安裝的軟件包package從A拷貝到B,然后從A機器ssh到B遠程執行安裝腳本
為了用戶能更方便的安裝xx服務,決定用shell寫一個工具,實現在本地遠程安裝服務包,限時1天,由于之前shell寫得少,實現過程中遇到很多比較基礎的問題,記錄一下
實現version1
業務相關信息用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
問題記錄
-
shell語言中&& & ; 區別
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.
-
傳參接收不到
按照被注釋調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執行bash setup.sh -param ${array[3]}
命令時,${array[3]}
值一直為空,不管怎么樣都傳遞不進去。
中間google了很多答案,都沒解決,后來抱著解決問題優先的態度,打算先把變量值寫到遠端主機的一個臨時文件里,用的時候再讀?。ㄓ?code>tempvar=$(cat /opt/test/bin/temp.txt)或tempvar=$(< /opt/test/bin/temp.txt)
讀取到變量tempvar
,再使用),試了也不行;
后來折騰了一個多小時,發現是低級錯誤(初學者的代價啊cry),將ssh需要執行的命令用"
引起了(而非'
)就好了,即ssh root@${array[1]} 'mkdir -p /opt/test && \
mkdir前的'
以及最后的'
換成"
貼一個區分單引號、雙引號、反引號的鏈接:
http://www.cnblogs.com/gx-303841541/archive/2012/10/25/2740333.html
-
ssh登陸到遠端主機執行的命令怎么能不顯示
使用>/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