我正在尝试编写一个脚本来运行脚本,但在运行脚本之前,我想检查它是dos还是unix格式,并进行相应的转换。这是我到目前为止所知道的:
spawn ssh root@login
expect "assword:"
send "myPass\r"
expect "#"
send "cd /myDir\r"
expect "#"
if head -1 *.sh | grep $'[\x0D]' >/dev/null #check to see if dos or unix
then #if dos then convert to unix and change permissions
dos2unix *.sh
chmod 777 *.sh
else #else execute script
./*.sh
fi
expect "#"是否允许在expect脚本中间包含if语句,如下所示?另外,我如何在if语句( dos2unix和chmod)之后执行多个命令。
发布于 2015-06-17 18:02:21
不,你不能把shell和expect混在一起,但是你可以发送一个复杂的shell命令:
send "cd /myDir\r"
expect "#"
send {for f in *.sh; do head -1 "$f" | grep -q $'[\x0D]' && { dos2unix "$f"; chmod 777 "$f"; }; ./"$f"; done}
send "\r"
expect "#"https://stackoverflow.com/questions/30880685
复制相似问题