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 ...
- 05-27-2010 #1Just Joined!
- Join Date
- May 2010
- Posts
- 5
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!
- 05-28-2010 #2
If you know will there only three parameter - why not use
If thats not your requirement - I would suggest to looking to "shift" operatorecho $1
echo $2
echo $3
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
-------------------
- 05-28-2010 #3Just 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...
- 05-28-2010 #4
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
-------------------
- 05-28-2010 #5Just 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
- 05-28-2010 #6Linux 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.
- 05-28-2010 #7Just Joined!
- Join Date
- May 2010
- Posts
- 5
Could you give an example please?
- 05-28-2010 #8
Few more links , on command line parsing.
Bash script – Passing arguments to the shell script Unix/Linux.
Linux.com :: Parsing arguments for your shell script- 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
-------------------
- 05-28-2010 #9Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
Code:for a in $(seq $#);do echo ${!a} done0 + 1 = 1 != 2 <> 3 != 4 ...
Until the camel can pass though the eye of the needle.
- 05-28-2010 #10Just Joined!
- Join Date
- May 2010
- Posts
- 5
It ain't workin'...still...
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!


Reply With Quote
