2012-10-17 37 views
1

所以我想在bash中得到期望的正常工作。預計在特殊字符的bash腳本2部分

這裏是腳本內容...

[[email protected] ~]# cat mysql_repl.sh 
#!/bin/bash 
read -p "Master server ip: " masterip 
read -p "What is the regular user to log into the master server: " masteruser 
read -p "What is the password for the regular user for the master server: " masterpass 
read -p "What is the root password for the master server: " masterrootpass 

read -p "Slave server ip: " slaveip 
read -p "What is the regular user to log into the slave server: " slaveuser 
read -p "What is the password for the regular user for the slave server: " slavepass 
read -p "What is the root password for the slave server: " slaverootpass 



expect -c "set slaveip $slaveip;\ 
set slaveuser $slaveuser;\ 
set slavepass $slavepass;\ 
set timeout -1;\ 
spawn /usr/bin/ssh $slaveip -l $slaveuser 'ls -lart';\ 
match_max 100000; 
expect *password:;\ 
send -- $slavepass\r;\ 
interact;" 

下面是腳本的輸出...

[[email protected] ~]# ./mysql_repl.sh 
Master server ip: 
What is the regular user to log into the master server: 
What is the password for the regular user for the master server: 
What is the root password for the master server: 
Slave server ip: xxx.xxx.xxx.xxx 
What is the regular user to log into the slave server: rack 
What is the password for the regular user for the slave server: test 
What is the root password for the slave server: DVJrPey99grJ 
spawn /usr/bin/ssh 198.61.221.179 -l rack 'ls -lart' 
[email protected]'s password: 
bash: ls -lart: command not found 

該命令沒有正確執行。我也試過/ bin/ls,但它仍然無法找到它。

第二部份...同一腳本...

我有一個變量在bash,具體而言,一個密碼。在這種情況下,密碼是「as $ 5!@?」 我想要做的是穿過每個角色,測試它是否是特殊角色並逃離它。因此,例如...

[email protected]? 

我至今接近但不工作的特殊字符,這將對於非特殊工作...

echo -n $pass | while read -n 1 c; do [[ "$c" = [[email protected]#$%^&*().] ]] && echo -n "\\"; echo -n $c; done 

人對如何我的想法可以在每個特殊字符前加一個\?

希望能夠澄清問題。

+6

[這是不是你想要解決的問題。(http://mywiki.wooledge.org/XyProblem) –

+0

那麼什麼我想要做的是在bash腳本中使用'expect -c',因爲我在bash中很體面,但對期望一無所知。我正在讀取用戶的密碼,然後將該密碼分配給一個變量。我將這個密碼輸入「」「expect -c」set timeout -1; \ spawn ssh $ {slaveip} -l $ {slaveuser}'ls -lart'; \ match_max 100000; expect * password:*; \ send - $ {slavepass} \ r; \ interact;「 – user1601716

+1

您試過引用它嗎? –

回答

2

在您的最後一行代碼塊中,嘗試在每個需要轉義的特殊字符前手動添加\。這應該不是很多工作。

此外,使用==進行平等的檢查,即:

echo -n $pass | while read -n 1 c; do [[ "$c" == [[email protected]#$%^&*().] ]] && echo -n "\\"; echo -n $c; done 
+0

這工作....兩件事情,我需要把通行證放在單引號中,不要讓它干涉,並且需要逃離所有的特價。謝謝! – user1601716