Basic Shell Script-Very Frustrating [Archive] - C Board

PDA

View Full Version : Basic Shell Script-Very Frustrating


kwigibo
05-18-2002, 07:25 PM
Im working on basic shell scripts, how do I get rid of the "-l6"

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


//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]$

//scipt file[b]

#!/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


[b]But if I use this script file I get the output following it



#!/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]$

starX
05-18-2002, 08:38 PM
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

Salem
05-18-2002, 11:37 PM
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

kwigibo
05-18-2002, 11:47 PM
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]$

kwigibo
05-19-2002, 12:06 AM
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

Salem
05-19-2002, 12:48 AM
> 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)

kwigibo
05-19-2002, 02:18 AM
Do you know how I could fix it?

Salem
05-19-2002, 02:31 AM
No

kwigibo
05-19-2002, 02:43 AM
Oh well, Thanks heaps.