Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I'm just trying to make a simple date script that corresponds to different arguements... The filename is dates.sh #!/bin/bash date +"%D" date +"%d/%m/%y" date +"%A, %B %d %Y" date ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    5

    Need help with simple bash script


    Hi,

    I'm just trying to make a simple date script that corresponds to different arguements...

    The filename is dates.sh

    #!/bin/bash

    date +"%D"

    date +"%d/%m/%y"

    date +"%A, %B %d %Y"

    date +"%s"

    I just want to be able to have the arguement "one" to correspond to the first date format (date +"%D"). So that when I type ./dates.sh one, only the first date format is displayed and so on.

    I would appreciate it if someone could help me out.

    Thanks!

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by earthicle View Post
    Hi,

    I'm just trying to make a simple date script that corresponds to different arguements...

    The filename is dates.sh

    #!/bin/bash

    date +"%D"

    date +"%d/%m/%y"

    date +"%A, %B %d %Y"

    date +"%s"

    I just want to be able to have the arguement "one" to correspond to the first date format (date +"%D"). So that when I type ./dates.sh one, only the first date format is displayed and so on.

    I would appreciate it if someone could help me out.

    Thanks!
    Code:
    #!/bin/bash
    case $1 in
      one)
    date +"%D"
        ;;
      two)
    date +"%d/%m/%y"
        ;;
      three)
    date +"%A, %B %d %Y"
        ;;
      four)
    date +"%s"
        ;;
      *)
        echo "Usage: $0 {one|two|three|four}"
        exit 1
    esac
    untested, but should work.

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    5
    thanks a lot, worked like a charm. first attempt at this and that definitely helped.

    sorry about the double post, i didn't see this category initially.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by earthicle View Post
    thanks a lot, worked like a charm. first attempt at this and that definitely helped.
    Glad you got it sorted. Be sure to check out the Bash man page in the future. You can read about the case statement and positional parameters ($1, $2, etc.) and other useful things:

    Code:
    man bash
    sorry about the double post, i didn't see this category initially.
    Hey, no worries! Enjoy your stay here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •