0

I created a simple script in bash for showing logs, I read the docs of bash scripts, and actually I did everything right, but my script not working. I'm really rookie in sh scripts so I need to ask about it here. So my script is:

#!/bin/bash

echo How many lines of log you want to see: 
read linesCount
echo 'What type of log you want to see? (info / error / warn) type all or just hit enter to see all types of logs.'
read logType

if [${#logType} -lt 3]
then
        tail -n $linesCount root/projects/retwiter/logs/logs.log |grep $logType
fi

When I run the script it shows me this: show-retwiter-logs.sh: 8: show-retwiter-logs.sh: [5: not found In the second 'record' I typed error it is why this shows it -> [5: not found But I actually don't know why

programmer
  • 159
  • 1
  • 11
  • Add spaces around the `[` and the `]`. – tkausl Oct 20 '20 at 15:30
  • `[` is a _command_ -- actually, it's an alternate name for the command named `test`. Just like you'd have to run `if test "${#logType}" -lt 3; then` with all the spaces, that's true if you're calling `test` under its shorter `[` name as well. Just as you need to run `ls -l file` instead of `ls-lfile`, `[` follows the same parsing rules as everything else. – Charles Duffy Oct 20 '20 at 15:32
  • Run the script with bash -x to debug it. – Raman Sailopal Oct 20 '20 at 15:34
  • ...btw, speaking of the rules everything else follows, be sure you quote your expansions. http://shellcheck.net/ will provide guidance on that count. `tail -n "$linesCount" ...` will provide more informative error messages when `linesCount` is empty than `tail -n $linesCount ...` will, as when you quote the variable even an empty string is passed to the child process, instead of being turned into no arguments at all. (That's not the only potential error case that quoting correctly avoids, just the easiest to describe tersely). – Charles Duffy Oct 20 '20 at 15:34

0 Answers0