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

1 Answers1

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
Community
  • 1
  • 1
Mahmoud Odeh
  • 942
  • 1
  • 7
  • 19