Thread: Basic Shell Script-Very Frustrating

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212

    Basic Shell Script-Very Frustrating

    Im working on basic shell scripts, how do I get rid of the "-l6"

    [EDIT] Added comments, the comments are added to the first script only because Im lazy and there the same in the second script [/EDIT]


    //screen shots

    [prompt]$ ./count4 -l6 -f1
    -l6
    1
    2
    3
    4
    5
    6
    [prompt]$ ./count4 -f1 -l6
    1
    2
    3
    4
    5
    6
    -l6
    [prompt]$

    [b]//scipt file[b]
    Code:
    #!/bin/bash
    
    # ./count4 -f3 -l15 print 3 to 15 to screen
    if [ $1==-f* ] && [ $2==-l* ]; then
        #a becomes 3
        a=${1#-f} 
        #b becomes 15
        b=${2#-l}
        while (($a<=$b)); do
     echo $a
     a=$(($a+1))
        done
    fi
    
    #if ./count4 -l15 -f3 print 3 to 15 to screen
    if [ $1==-l* ] && [ $2==-f* ]; then
        #a becomes 3
        a=${2#-f}
        #b becomes 15
        b=${1#-l}
        while (($a<=$b)); do
     echo $a
     a=$(($a+1))
        done
    fi
    But if I use this script file I get the output following it


    Code:
    #!/bin/bash
    
    if [ $1==-f* ] && [ $2==-l* ]; then
        a=${1#-f}
        b=${2#-l}
        while (($a<=$b)); do
     echo $a
     a=$(($a+1))
        done
    elif [ $1==-l* ] && [ $2==-f* ]; then
        a=${2#-f}
        b=${1#-l}
        while (($a<=$b)); do
     echo $a
     a=$(($a+1))
        done
    fi

    [prompt]$ ./count4 -f1 -l6
    1
    2
    3
    4
    5
    6
    [prompt]$ ./count4 -l6 -f1
    -l6
    [prompt]$
    Last edited by kwigibo; 05-18-2002 at 09:12 PM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    Maybe if you comment this code some, so I have a better idea as to eactly that it is you're trying to do, I can probably help you.

    starX
    www.axisoftime.com

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    set -v
    set -x

    Put these commands at the start of your script, then you'll see which commands are executed, and what those variables expand to

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    starX:
    This is an exercise in a text book. This is for the question above with the code.

    Write a count4 script, which uses tagged arguments that concatenate the alphabetic flag and its value. Example

    count4 -f5 -l7

    Note there are no spaces between -f and 5

    HINT: look at parameter Expansions as a way of removing the -f from -f5 to yield the number..
    Salem: How do I interpret this?
    [prompt]$ ./count4 -l9 -f4
    set -x

    # I CUT OUT THE CODE IT IS THE ABOVE ONE WITH THE ELIF STATEMENT
    # I DON'T REALLY UNDERSTAND MUCH OF THIS
    + '[' '-l9==-f*' ']'
    + '[' '-f4==-l*' ']'
    + a=-l9
    + b=-f4
    + (( -l9<=-f4 ))
    + echo -l9
    -l9
    + a=1
    + (( 1<=-f4 ))
    [prompt]$
    Last edited by kwigibo; 05-18-2002 at 11:56 PM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    I see where the problem is. I think?

    Is it where the variables "a" and "b" are assigned their values? But how come it works in the "if..then" statement and not the "elif..then" statement?

    Please explain to me why.

    Thanks in advance

    kwigibo

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is it where the variables "a" and "b" are assigned their values?
    Yeah, you have
    + a=-l9
    + b=-f4

    Not
    + a=9
    + b=4

    Which is kinda odd, since you wrote
    a=${2#-f}
    b=${1#-l}

    It seems you're getting the -l parameter in a (you expected to remove -f, and didn't)

  7. #7
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Do you know how I could fix it?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No

  9. #9
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Oh well, Thanks heaps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shell script basic question
    By PedroTuga in forum Linux Programming
    Replies: 1
    Last Post: 09-09-2006, 03:24 AM
  2. invoking shell script
    By agoel01 in forum C Programming
    Replies: 1
    Last Post: 12-01-2005, 11:37 AM
  3. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  4. Arguements with a shell script
    By Doh in forum Linux Programming
    Replies: 1
    Last Post: 11-28-2002, 02:20 PM