bonduel
asked on
tcsh shell script expanding * as files
Hello,
Here is a simple shell script saved in a file called "test":
__________________________ ___
if [ $# -gt 0 ]
then
echo "arguments number $#"
while [ $1 ]
do
echo "param $1"
shift
done
fi
__________________________ ___
If I launch it with "test *" and if in the directory there are two files "file1" and "test" here is the result:
__________
arguments number 2
param file1
param test
___________
My question is: why?
What I expected was:
___________
arguments number 1
param *
___________
Another thing strange (in my opinion), is that if I remove the "if" test lines like that:
___________________
echo "arguments number $#"
while [ $1 ]
do
echo "param $1"
shift
done
___________________
The result of the execution of the script "test *" is:
___________________
arguments number 2
while: Expression Syntax.
___________________
Do you have an idea?
Thanx
Here is a simple shell script saved in a file called "test":
__________________________
if [ $# -gt 0 ]
then
echo "arguments number $#"
while [ $1 ]
do
echo "param $1"
shift
done
fi
__________________________
If I launch it with "test *" and if in the directory there are two files "file1" and "test" here is the result:
__________
arguments number 2
param file1
param test
___________
My question is: why?
What I expected was:
___________
arguments number 1
param *
___________
Another thing strange (in my opinion), is that if I remove the "if" test lines like that:
___________________
echo "arguments number $#"
while [ $1 ]
do
echo "param $1"
shift
done
___________________
The result of the execution of the script "test *" is:
___________________
arguments number 2
while: Expression Syntax.
___________________
Do you have an idea?
Thanx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ha, and about the while error, I should replace the test:
while [ $1 ]
with
while [ "$1" != "" ]
Like this it works.
while [ $1 ]
with
while [ "$1" != "" ]
Like this it works.
ASKER
In fact, it is simple to avoid this by quoting the * like:
test "*"
I found this by doing some tests.
I think it is 200 wasted points, and some of your time.
Thank you again.