Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Customized syntax

  1. #1
    Join Date
    Sep 2012
    Beans
    3

    Customized syntax

    Hey, I just started out some weeks whit Ubuntu, but me big problem is that i'm using long and complex syntaxes;

    Code:
    ./example.pl -p 2 -u 5 -p domain.com
    To:
    Code:
    ./final_example.pl -D domain.com
    I looked at the alias feature, but what I saw /'ve noticed is that it can not handle custom parameters, and that I should write a function to make. just me knowledge was not that far.

    can someone maby sent me in the good direction (peace of code and a nice tutorial?)

  2. #2
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Customized syntax

    Hi jelee ,

    Welcome to the UbuntuForums !

    When a application or program has multiple functionalities , you need to pass an array of switches to direct you as per your wish .

    You can create a shell script from which you can call the ./example.pl with predefined options .

    Code:
    #!/bin/bash
    
    domain=$1
    ./example.pl -p 2 -u 5 -p $domain
    you can then call the script something like below

    Code:
    ./final_example.pl domain.com
    Last edited by codemaniac; September 17th, 2012 at 12:19 PM.
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  3. #3
    Join Date
    Sep 2012
    Beans
    3

    Re: Customized syntax

    Hey, Codemaniac
    Thank you for you fast reaction! i was on the good way, If i compair the both codes,
    but still got i some questions about this.
    how can i add a parameter to this?

    /final_example.pl -D domain.com

    and some error handling?

    if ( $domain == 0 | $domain =< 4) { echo "you forget parameter -D"; }

    Still looking for a good tutorial

  4. #4
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Customized syntax

    Quote Originally Posted by jelee View Post
    Hey, Codemaniac
    Thank you for you fast reaction! i was on the good way, If i compair the both codes,
    but still got i some questions about this.
    how can i add a parameter to this?

    /final_example.pl -D domain.com

    and some error handling?

    if ( $domain == 0 | $domain =< 4) { echo "you forget parameter -D"; }

    Still looking for a good tutorial
    You can try something like below .

    Code:
    doption=false        # domain not specified
    
    for option in $*
    do
       case $option in
          -D)   doption=true;shift;domain="$1";shift;;
          -*)
             echo "invalid arguments"
             echo "Usage : /final_example.pl -D domain.com"
             exit 1;;
       esac
    done
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,003
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Customized syntax

    Quote Originally Posted by jelee View Post
    Hey, Codemaniac
    Thank you for you fast reaction! i was on the good way, If i compair the both codes,
    but still got i some questions about this.
    how can i add a parameter to this?

    /final_example.pl -D domain.com

    and some error handling?

    if ( $domain == 0 | $domain =< 4) { echo "you forget parameter -D"; }

    Still looking for a good tutorial
    if you just want to add a couple of fixed parameters to the command, you can use:
    Code:
    #! /bin/bash
    thecommand your fixed parms "$@"
    exit $?
    where "$@" will be replaced by all arguments to your script. Error handling can be restricted to propagating the return code of the command you call (which ends up in the special variable "$?").

    For this kind of use, you don't really need to handle problems yourself

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,399
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Customized syntax

    i don't think for would cooperate with shift so i'd use while controlled by the number of params (each shift decreases that number)
    Code:
    while (($#))
    do
      case "$1" in
        -D)   doption=true;shift;domain="$1";;
        *)    echo "'$1' - invalid argument";;
      esac
      shift
    done
    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
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Customized syntax

    Quote Originally Posted by Vaphell View Post
    i don't think for would cooperate with shift
    But surprisingly it does , please note the extra shift .
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

  8. #8
    Join Date
    Sep 2012
    Beans
    3

    Re: Customized syntax

    Code:
    #!/bin/bash
    
    # command
    sudo nmap -sS $1
    
    #parameters
    ipoption=$1
    
    #error afhandeling 
    ipoption=false  #domain not specified
    
    for option in $*
    do
    case $ipoption in 
        -ip) option=true;shift;domain="$1";shift;;
        -*) 
        echo "invalid arguments" 
        echo "Usage : test_script.pl -ip ip adres"
        exit;;
    esac
    done
    I got this right now, put it wont want to work for me.
    it seems to me that the script nmap overuled, (get the manual of nmap to see after running the script whit parameter (and withoud a parameter its saying no host specified)

    Questions:
    - Can i overule a other script? turn off the feedback/Echo withoud entering nmap?
    - Can someone give me a working script (i see that there are more ten one solution for this, different peaces of code.)
    for me as "noob" and small experince in php, is this pretty new for me. and need a working solution.. something easy to use and to add more parameters. and to learn

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,399
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Customized syntax

    But surprisingly it does , please note the extra shift .
    yet it doesn't
    Code:
    #!/bin/bash
    
    for param
    do
      echo  "$((++i)): $param"
      case $param in
        -D) shift; x="$1"; shift; echo -n "-D $x, ";;
        *) echo -n "$param, ";;
      esac
      echo "params left [$#]: $@"
    done
    output:
    Code:
    $ ./test_params.sh a b -D xyz
    1: a
    a, params left [4]: a b -D xyz
    2: b
    b, params left [4]: a b -D xyz
    3: -D
    -D b, params left [2]: -D xyz
    4: xyz
    xyz, params left [2]: -D xyz
    for loop is constructed at the beginning and fixed, shifting doesn't do anything worthwhile and calling $1 doesn't make sense at all (you get wrong arg).
    Only while+shift make sure that you get to access each param only once.

    Code:
    #!/bin/bash
    
    while (($#))
    do
      param="$1"
      echo  "$((++i)): $param"
      case $param in
        -D) shift; x="$1"; echo -n "-D $x, ";;
        *) echo -n "$param, ";;
      esac
      shift
      echo "params left [$#]: $@"
    done
    output:
    Code:
    $ ./test_params2.sh a b -D xyz
    1: a
    a, params left [3]: b -D xyz
    2: b
    b, params left [2]: -D xyz
    3: -D
    -D xyz, params left [0]:
    Last edited by Vaphell; September 17th, 2012 at 03:18 PM.
    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

  10. #10
    Join Date
    Jan 2010
    Location
    Kolkata India
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Customized syntax

    Quote Originally Posted by Vaphell View Post
    yet it doesn't
    Hi ,

    The objective was to first use shift to ignore the switch , then grab the value to the variable and again a shift to point to the next switch .It works and expects you to adhere a standard -option value convention .

    Please have this simple example .
    Code:
    #!/bin/bash
    
    for option in $*
    do
            case $option in
            -n) shift
                NAME=$1
                echo "The name is $NAME"
                shift
            ;;
            -a) shift
                AGE=$1
                echo "Age is $AGE"
                shift
            ;;
            -*) echo "Invalid Option"
            ;;
            esac
    done
    Code:
    ./passOptions.sh -n Arijit -a 25
    The name is Arijit
    Age is 25
    “Progress is made by lazy men looking for easier ways to do things”
    — Robert A. Heinlein

Page 1 of 2 12 LastLast

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
  •