所以我试图让bash中的expect正常工作。
这是脚本内容...
[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'
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="156774767e55242c2d3b23243b2727243b24222c" rel="noreferrer noopener nofollow">[email protected]</a>'s password:
bash: ls -lart: command not found
命令未正确执行。我也尝试了/bin/ls,但仍然找不到它。
第二部分...相同的脚本...
我在 bash 中有一个变量,具体来说,是一个密码。在本例中,密码是“as$5!@?” 我想做的是遍历每个字符,测试它是否是特殊字符并转义。例如...
pass=as$5!@?
到目前为止,我所拥有的很接近,但不适用于特殊字符,它将适用于非特殊字符......
echo -n $pass | while read -n 1 c; do [[ "$c" = [!@#$%^&*().] ]] && echo -n "\\"; echo -n $c; done
有人知道如何在每个特殊字符之前添加\吗?
希望能够解决这个问题。
最佳答案
在最后一行 block 中,尝试在每个需要转义的特殊字符之前手动添加\。这应该不是什么难事。
此外,使用 == 进行相等性检查,即:
echo -n $pass | while read -n 1 c; do [[ "$c" == [!@#$%^&*().] ]] && echo -n "\\"; echo -n $c; done
关于bash - bash 脚本中期望包含特殊字符 2 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925986/