linux - while循环出错?脚本

标签 linux bash shell while-loop

我的代码:

#!/bin/bash
rm screenlog.0
screen -X stuff 'X21'$(printf \\r)
while :
do
grep -i "T" $screenlog.0
if [ $? -eq 0 ];
then
FILE=/etc/passwd 
VAR=`head -n 1 $FILE` 
echo $VAR
rm screenlog.0
break
done

此脚本用于删除文件“screenlog.0”,向屏幕界面发送命令(X21)。 这是第一部分,它有效。

第二部分是问题: 那应该测试“screenlog.0”的内容,里面有没有带“T”的东西,将内容保存到变量中。

错误:

line 11: syntax error near unexpected token `done'
line 11: `done'

到“屏幕”:它是一个 USB 设备的屏幕,可以接收这样的 radio 消息:

T350B00A66E2
H34D04DE4254

脚本必须扫描以“T”开头的传入消息(第一个字母是十六进制代码后面的类型字段。

一些纠正或其他解决方案的想法?

我稍微修改了我的代码:

#!/bin/bash
>screenlog.0
screen -X stuff 'X21'$(printf \\r)
while :
do
sleep 2
grep -i "T" $screenlog.0
if [ $? -eq 0 ];
then
screenlog.0=/etc/passwd
VAR=`head -n 1 $screenlog.0`
echo $VAR
break
fi
done

新的错误是:

grep: .0: No such file or directory

全部 5 秒....

文件 screenlog.0 存在.. :(

最佳答案

哦...您在脚本中遗漏了 fi :)。语法如下 if [ condition ];then #dosomething fi

为你的脚本

if [ $? -eq 0 ];then
    FILE=/etc/passwd  
    VAR=`head -n 1 $FILE` 
    echo $VAR
    rm screenlog.0
    break
fi

关于linux - while循环出错?脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22729806/

相关文章:

linux - 如何忽略从 AC_CHECK_HEADERS 生成的 `configure' 拾取的系统文件

regex - awk 模式匹配

bash - 在 bash 脚本中的特定范围内创建一系列不同的随机数

bash - 将正确的月份开始日和结束日传递给并行

linux - 嵌套 for 循环一年内几个月

regex - awk 或 sed : Return lines between two instances of the same pattern

Linux:追加命令

linux - 使用 bash 脚本遍历一堆变量

ruby - Bash - 如何从后台 ruby​​ 脚本获取 STDOUT

android - 如何从脚本中退出/关闭 Android 模拟器