...

View Full Version : Shell script problem



philmetz
10-01-2008, 11:45 AM
Hi can someone tell me whats wrong with the following script i made:
#!/bin/sh
file1=$1
file2=$2
if [ $# = 2 && -e $file1]
then
if [ -e $file1 ]
then
echo "File 1 is" $file1
echo "File 2 is" $file2
cp $file1 $file2
else
echo "ERROR: File does not exist!"
fi
else
echo "ERROR: Not enough command line arguments!"
fi



The problem is:

philip@philip-laptop:~/Desktop$ sh copy.sh test.java test2.java
[: 16: missing ]
ERROR: Not enough command line arguments!

oracleguy
10-01-2008, 04:08 PM
The reason is most likely that $# is 3 not 2 because the name of the script is passed in as the first command line argument. Echo out $# and see.

oesxyl
10-02-2008, 12:24 AM
Hi can someone tell me whats wrong with the following script i made:
#!/bin/sh
file1=$1
file2=$2
if [ $# = 2 && -e $file1]
then
if [ -e $file1 ]
then
echo "File 1 is" $file1
echo "File 2 is" $file2
cp $file1 $file2
else
echo "ERROR: File does not exist!"
fi
else
echo "ERROR: Not enough command line arguments!"
fi



The problem is:

philip@philip-laptop:~/Desktop$ sh copy.sh test.java test2.java
[: 16: missing ]
ERROR: Not enough command line arguments!


why don't you use cp -i ?


The reason is most likely that $# is 3 not 2 because the name of the script is passed in as the first command line argument. Echo out $# and see.
no, $# is 2 not 3, 'program name' is not counted here

best regards

brad211987
10-02-2008, 05:56 PM
at this line:


if [ $# = 2 && -e $file1]

do you need a space near the end to make it $file1 ] ?

Its been quite a while since I've done any shell scripting, but I do remember it being picky.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum