1

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
  • This should help you leaps. – Braiam Oct 08 '14 at 01:13
  • The if stmt:is all wrong. What are you trying to do there? – slm Oct 08 '14 at 01:16
  • im trying to use cal command in script to validate date and this is what my conditions are – Hajir Golmohammadi Oct 09 '14 at 17:17
  • if [ $# -eq 0 ]; then echo Usage: chkdate year month day exit 0 fi – Hajir Golmohammadi Oct 09 '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 09 '14 at 17:19
  • if [[ $month == 2 && $day -gt 28 && $isNotLeapYear ]]; then # Not Leap year! echo Usage: chkdate year month day exit 2 fi if [[ $month == 2 && $day -gt 29 && $isLeapYear ]]; then # Invalid day! echo Usage: chkdate year month day exit 3 fi – Hajir Golmohammadi Oct 09 '14 at 17:20

2 Answers2

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
slm
  • 369,824
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     
    
terdon
  • 242,166