0

I am a computer science student and am currently working on a Linux assignment broken down into tasks.

The current task I am working on requires me to prompt the user to enter their name and the hour of the day and then to use if statements to evaluate the input and output an appropriate message I also have to look for a file and output an appropriate message if it doesn't exist, I have inserted my attempt below.

#!/bin/bash
echo "Enter your name"
read name
echo "What hour is it?"
read hour

echo "$name $hour"

if ["$hour" -gt 23]
then
 echo "That hour is not correct"
fi

if ["$hour" -ge 0 -o "$hour" -le 7]
then
 echo "It's very early $name, you should be asleep"
fi

if ["$hour" -ge 8 -o "$hour" -le 22]
then
 echo "Good day $name"
fi

if ["$hour" -eq 23]
then
 echo "Time for bed $name"
fi

if [-f "fandmf13.txt"]
then
  cat fandmf13.txt
else
  echo "File not found"
fi

When I execute this code I get the following error message:

./fandmf4.sh: line 9: [8: command not found
  ./fandmf4.sh: line 14: [8: command not found
  ./fandmf4.sh: line 19: [8: command not found
  ./fandmf4.sh: line 24: [8: command not found
  ./fandmf4.sh: line 29: [-f: command not found
  File not found

If anyone can give me some pointers or tips as to where I'm going wrong that would be great

6
  • Start with checking code on shellcheck.net Commented May 21, 2020 at 11:40
  • you need to add spaces before [] Commented May 21, 2020 at 11:42
  • Does this answer your question? if, elif, else statement issues in Bash Commented May 21, 2020 at 11:42
  • @DigvijayS ah thank you, wasn't aware of shellcheck.net I shall definitely check it out! Commented May 21, 2020 at 11:43
  • You need a space between [ and -f, otherwise bash is looking for a command named "[-f" Commented May 21, 2020 at 11:45

1 Answer 1

0

you should review the logic, but you can try this:

There is a space missing between if [[, also you need a space between [[ and variables inside it

expression1 && expression2

True if both expression1 and expression2 are true.

expression1 || expression2

True if either expression1 or expression2 is true.

#!/bin/bash
echo "Enter your name"
read name
echo "What hour is it?"
read hour

echo "${name}-${hour}"

if [[ "$hour" -gt 23 ]]
then
  echo "That hour is not correct"
fi

if [[ "$hour" -ge 0 || "$hour" -le 7 ]]
then
  echo "It's very early ${name}, you should be asleep"
fi

if [[ "$hour" -ge 8 || "$hour" -le 22 ]]
then
  echo "Good day $name"
fi

if [[ "$hour" -eq 23 ]]
then
    echo "Time for bed $name"
fi

if [[ -f "fandmf13.txt" ]]
then
    cat fandmf13.txt
else
   echo "File not found"
fi
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.