I want to read my files line by line every 5 seconds. This time I just tried one-line bash command to do this.
And bash command is:
let X=1;while [ $X -lt 20 ];do cat XXX.file |head -$X|tail -1;X=$X+1;sleep 5;done
However I got the error like:
-bash: [: 1+1: integer expression expected
What’s the problem?
btw, why can’t we do $X < 20? (Instead we have to do -lt, less than?)
thx
Your assignment
X=$X+1doesn’t perform arithmetic. If$Xis 1, it sets it to the string"1+1". ChangeX=$X+1tolet X=X+1orlet X++.As for the use of
-ltrather than<, that’s just part of the syntax of[(i.e., thetestcommand). It uses=and!=for string equality and inequality-eq,-ne,-lt,-le,-gt, and-gefor numbers. As @Malvolio points out, the use of<would be inconvenient, since it’s the input redirection operator.(The
test/[command that’s built into the bash shell does accept<and>, but not<=or>=, for strings. But the<or>character has to be quoted to avoid interpretation as an I/O redirection operator.)Or consider using the equivalent
(( expr ))construct rather than theletcommand. For example,let X++can be written as((X++)). At least bash, ksh, and zsh support this, though sh likely doesn’t. I haven’t checked the respective documentation, but I presume the shells’ developers would want to make them compatible.