Results 1 to 7 of 7

Thread: If then and user choices in a script

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    720
    Distro
    Ubuntu 14.04 Trusty Tahr

    Question If then and user choices in a script

    Hi Guys,

    I'm back with more annoying questions.

    Background:

    I'm using a script called tasky to add and remove items from Google tasks.

    The command to add a task is simple:

    Code:
    tasky a subject -d date -n note
    In order to make the process easier I wrote a script that walks the user through adding a new task.

    Code:
    #!/bin/bash
    read -p Subject: sub
    read -p "Due Date(MM/DD/YY):" ddate
    read -p "Note:" note
    
    
    ~/scripts/tasky a "$sub" -d "$ddate" -n "$note"
    With this script, the user has to provide a date and note.

    What would I need for the script to ask the user:

    3. If there is a due date enter it, if not skip it.
    4. If there is a note, enter it, if not skip it.

    The part I don't know how to do is if there is a date enter it or skip if there is no due date and if there is a note enter it or skip if there is no note.

    I hope that makes sense.

    I really appreciate the help guys!
    Last edited by GrouchyGaijin; May 22nd, 2013 at 11:21 AM. Reason: answered part of my own question
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,439
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: If then and user choices in a script

    Code:
    #!/bin/bash
    
    params=()
    read -p Subject: sub
    read -p "Due Date(MM/DD/YY):" ddate
    read -p "Note:" note
    [ -n $ddate ] && params+=( "-d" "$ddate" )
    [ -n $note ] && params+=( "-n" "$note" )
    
    ~/scripts/tasky a "$sub" "${params[@]}"
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    720
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: If then and user choices in a script

    Thank you.
    That is pretty cool except the note works, I can either add a note or skip it.
    But if I do not add a date, it crashes.

    Code:
    Traceback (most recent call last):
      File "/home/john/scripts/tasky", line 457, in <module>
        main(parse_arguments(sys.argv))
      File "/home/john/scripts/tasky", line 440, in main
        handle_input_args(args)
      File "/home/john/scripts/tasky", line 250, in handle_input_args
        d = time.strptime(dstr, "%m/%d/%y")
      File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
        return _strptime(data_string, format)[0]
      File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
        (data_string, format))
    ValueError: time data '' does not match format '%m/%d/%y'
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,439
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: If then and user choices in a script

    is it possible to run ~/scripts/tasky a "subject" without date and note?
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    720
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: If then and user choices in a script

    Yes it is.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,439
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: If then and user choices in a script

    me fail
    Code:
    #!/bin/bash
    
    params=()
    read -p "Subject: " sub
    read -p "Due date (MM/DD/YY): " ddate
    read -p "Note: " note
    
    [ -n "$ddate" ] && params+=( "-d" "$ddate" )
    [ -n "$note" ] && params+=( "-n" "$note" )
    
    ~/scripts/tasky a "$sub" "${params[@]}"
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    720
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: If then and user choices in a script

    That fixed it
    Thank you man!!
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

Tags for this Thread

Bookmarks

Posting Permissions

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