gpt4 book ai didi

linux - 期望脚本在运行脚本之前检查文件类型并更改它

转载 作者:太空宇宙 更新时间:2023-11-04 03:38:04 24 4
gpt4 key购买 nike

我正在尝试编写一个脚本来运行脚本,但在运行脚本之前我想检查它是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 "#"

我可以像这样在期望脚本中间包含 if 语句吗?另外,如何在 if 语句(dos2unix 和 chmod)之后执行多个命令。

最佳答案

不,你不能像那样混合 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 "#"

关于linux - 期望脚本在运行脚本之前检查文件类型并更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com