Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hello! I'm not sure if this is the right spot to post my problem but I'm a newbie so I thought I would post here... I have a shell program ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    5

    Exclamation Shell Program

    Hello!
    I'm not sure if this is the right spot to post my problem but I'm a newbie so I thought I would post here...

    I have a shell program that i want to execute.

    The problem sounds like this: "I want to use the command line and call on the Shell program to print (echo) the positional parameters which they're read after calling the file." If I wasn't too clear with the thing I want to do, excuse my English, I'll post the code that i'm writing in the command line to execute. Here it is:

    ./program.sh alfa beta gamma

    and I'm hoping for it to print(echo) alfa beta and gamma on a newline. Ok, now that my problem is clear (or so I hope) let's continue.

    I've tried by myself some versions of Shell script, but without luck. Then I've called a friend who pretends he knows Shell and asked him to help me. He gave me 3 versions of code, but none worked. Here are his versions:

    for i
    do
    echo $i
    done
    exit
    ------------------------------------------(this line separates the versions of Shell script)

    I=$#
    while test $I -gt 0
    do
    eval echo \$$I este parametru de intrare
    I=`expr $I-1`
    done
    -------------------------------------------(same thing here as above)

    I=1
    while test $I -le $#
    do
    eval echo \$$I este parametru de intrare
    I=`expr $I+1`
    done

    Ok... when I'm tryin to execute these programs in the terminal, i get this error for the last two versions:

    "line 6: syntax error near unexpected token 'done'
    line 6: 'done' "

    And for the first version of code I get this error:

    "line 2: syntax error near unexpected token '$'do\r' '
    line 2: 'do "

    Well... I've looked over the code a few times and can't seem to find a logical error to my syntax... but nevertheless it doesen't work...

    Can anyone help me out please? It's really urgent!

    10x in advance!

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    If you know will there only three parameter - why not use
    echo $1
    echo $2
    echo $3
    If thats not your requirement - I would suggest to looking to "shift" operator
    Special Variable Types

    Excellent tutorial on shell script :
    Advanced Bash-Scripting Guide

    HTH
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    Well i'm looking for a variable that will store $# (the number of parameters, even if they are 5,10,20,etc.)... and then in a loop print them on the screen...

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Did you checked "shift " ? it might be useful , in your case.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    I'm checking out your links right now... I'll come back shortly with a reply to tell you if i fixed the problem or not

  6. #6
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    You can also create a sequence with seq command and loop over it.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  7. #7
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    Could you give an example please?

  8. #8
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  9. #9
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    for a in $(seq $#);do
    echo ${!a}
    done
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  10. #10
    Just Joined!
    Join Date
    May 2010
    Posts
    5

    It ain't workin'...still...

    Quote Originally Posted by nmset View Post
    Code:
    for a in $(seq $#);do
    echo ${!a}
    done
    After trying the file that contains JUST this code... i tried to run the file with "./prog.sh alfa beta gamma" and i got the "same" error... except with another line...

    The errors are:

    "./prog.sh : line 1: syntax error near unexpected token '$'do\r' '
    '/prog.sh: line 1: 'for a in $(seq $#); do"

    Maybe my program is not working properly... I think it's time to mention that I'm using cygwin on windows 7 to test my programs... but also have sent the 3 versions from my first post to a friend who has Ubuntu to test them and he got the exact same errors... so i don't think cygwin could be the fault...

    So, anyone has ideas? Do these pieces of code work on your linux systems? I would like to find an answer... to know if I'm not doing it right or the syntax isn't good... Thanks all for trying!

Page 1 of 2 1 2 LastLast

Posting Permissions

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