1

This question already has an answer here:

I'm writing this in my script. It has other parts but I'm getting stuck on this part only.

if [[$# == $year $month $day  ]] ; then
    cal $day $month $year
fi

When I run this it give me this msg:

[[3: command not found

So what is the problem? is it a syntax or actual command?

Here's the rest of my script if that helps:

year=$(echo "$year" | bc)
month=$(echo "$month" | bc)
day=$(echo "$day" | bc)

if [[$# == $year $month $day  ]] ; then    
    cal $day $month $year
fi

marked as duplicate by Gilles bash Oct 16 '14 at 22:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This should help you leaps. – Braiam Oct 8 '14 at 1:13
  • The if stmt:is all wrong. What are you trying to do there? – slm Oct 8 '14 at 1:16
  • im trying to use cal command in script to validate date and this is what my conditions are – Hajir Golmohammadi Oct 9 '14 at 17:17
  • if [ $# -eq 0 ]; then echo Usage: chkdate year month day exit 0 fi – Hajir Golmohammadi Oct 9 '14 at 17:19
  • if [[ "$year" == "" || "$month" == "" || "$day" == "" ]]; then # Not enough data! echo Usage: chkdate year month day exit 1 fi if [[ $month == 2 && $(($year%100)) == 0 ]]; then if [[ $(($year%400)) == 0 ]]; then isLeapYear=1 else isNotLeapYear=1 fi elif [[ $month == 2 && $(($year%4)) == 0 ]]; then isLeapYear=1 else isNotLeapYear=1 fi – Hajir Golmohammadi Oct 9 '14 at 17:19
2

You need to put a space after the square brackets, [[.

if [[ $# == $year $month $day ]] ; then    
    cal $day $month $year
fi

Also as you have it written that won't work. You have compare $# and $year $month $day as a string, or something else entirely. Perhaps:

if [[ "$#" == "$year$month$day" ]] ; then    
    cal $day $month $year
fi
2

There are quite a few problems with this.

  1. You need a space after the [[:

    [[ $# == $year $month $day ]]
    
  2. $# is not what you think it is. It is the number of arguments passed to a script or a function. It looks like you think it is actually the list of arguments.

  3. == compares strings, you're giving it a list of sorts. To compare the list of arguments passed to your script with $day $month $year, you would do something like this:

    [[ "$@" == "$day $month $year" ]]
    
  4. cal does not work that way. My cal does not work that way but apparently some newer versions do. I have no idea what you're actually trying to do but it won't work. Are you perhaps looking for date -d "$day/$month/$year"? For example

    $ date -d "08/10/2014"
    Sun Aug 10 00:00:00 CEST 2014
    

    Or, if you want to show the calendar of a specific month:

    $ cal 10 2014
        October 2014      
    Su Mo Tu We Th Fr Sa  
              1  2  3  4  
     5  6  7  8  9 10 11  
    12 13 14 15 16 17 18  
    19 20 21 22 23 24 25  
    26 27 28 29 30 31     
    
  • 1
    Actually, the version of cal from util-linux 2.21.2 (present in Cygwin 1.1.2) does work that way: Usage: / cal [options] [[[day] month] year]. – G-Man Oct 8 '14 at 16:24
  • @G-Man ah, perhaps. Mine (util-linux 2.20.1-5.5) does not. Thanks. – terdon Oct 8 '14 at 16:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.