Bash - current date and calendar with "if Else"

Asked

Viewed 36 times

-2

#!/bin/bash

echo "1 - calendario"
echo "2 - data"
echo "opção: "
read var

if [$var -eq 1];
 then
         calendario=$(cal)
         echo $calendario
elif [$var -eq 2];
then
        now=$(date)
        echo $now
else
        echo "opção invalida"
fi 

this is the output:

1 - calendario
2 - data
opção:
1
./calendar.sh: line 8: [1: command not found
./calendar.sh: line 12: [1: command not found
opção invalida

I want to give the user two options, one to print a calendar and the other to the current date, but without using "case", only with "if Else", I don’t know what I’m doing wrong

  • the last line of your code is fi really? anyway, try to structure/format the question better, it’s a bit confusing

  • I’ve tried to change it to make it better, but yes, it’s fi even

1 answer

1

The error says that in line 8 (if [$var -eq 1];) and in the line 12 (elif [$var -eq 2];) has invalid command.

The if has not ; at the end of the command, which would be the specified error (see syntax)

#!/bin/bash

echo "1 - calendario"
echo "2 - data"
echo "opção: "
read var

if [$var -eq 1]
then
    calendario=$(cal)
    echo $calendario
elif [$var -eq 2]
then
    now=$(date)
    echo $now
else
    echo "opção invalida"
fi 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.