期望在bash脚本中使用特殊字符2部分

时间:2012-10-17 01:51:32

标签: bash expect

所以我试图在bash中期望正常工作。

这是脚本内容......

[root@mysql1 ~]# 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;"

这是脚本的输出......

[root@mysql1 ~]# ./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'
rack@198.61.221.179's password:
bash: ls -lart: command not found

命令未正确执行。我也试过/ bin / ls但它仍然无法找到它。

第二部分......同样的脚本......

我在bash中有一个变量,特别是密码。在这种情况下,密码是“$ 5!@?” 我想要做的就是浏览每个角色,测试它是否是一个特殊角色并逃脱它。例如......

pass=as$5!@?

到目前为止我所拥有的内容很接近但不适用于特殊字符,它适用于非特殊的...

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

有人想过如何在每个特殊字符之前添加\吗?

希望能够解决问题。

1 个答案:

答案 0 :(得分:2)

在最后一个行块中,尝试在需要转义的每个特殊字符之前手动添加\。那不应该是多少工作。

另外,使用==进行等式检查,即:

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