简体   繁体   English

在while循环中犯了错误? bash脚本

[英]Mistake in while loop? bash script

My Code: 我的代码:

#!/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

This script is to delete the file "screenlog.0" send a command (X21) to an screen interface. 该脚本是删除文件“ screenlog.0”,向屏幕界面发送命令(X21)。 Thats the first part and it works. 多数民众赞成在第一部分,它的工作原理。

The second Part is the Problem: That should test the content of "screenlog.0", is there an something with a "T" inside save the contant into a variable. 第二部分是问题:应该测试“ screenlog.0”的内容,里面是否有一个带有“ T”的内容,并将内容保存到变量中。

The error: 错误:

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

To the "screen": Its an screen of an usb device that recive radio messages like this: 到“屏幕”:它是USB设备的屏幕,接收如下的无线电消息:

T350B00A66E2
H34D04DE4254

The script have to scan for the incomming messages with "T" at the beginning (The first letter is a Type field behind this a hex code. 脚本必须在开头扫描带有“ T”的传入消息(第一个字母是此十六进制代码后面的Type字段。

Some ideas to correct or other solutions? 一些可以纠正的想法或其他解决方案?

I corrected my code a bit: 我更正了我的代码:

#!/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

The new error is: 新的错误是:

grep: .0: No such file or directory

All 5 seconds.... 全部5秒....

The file screenlog.0 exist .. :( 文件screenlog.0存在.. :(

oh...you missed fi in your script :). 哦...您在脚本中错过了fi :)。 Like syntax as follows if [ condition ];then #dosomething fi if [ condition ];then #dosomething fi

For your script 对于您的脚本

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM