Shell nested ifs

Hi can someone tell me whats wrong with the following:

#!/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
        echo "Copy complete!"
    else
        echo "ERROR: File does not exist!"
    fi
else
    echo "ERROR: Not enough command line arguments!"
fi    

I get the following output:

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

your code

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

try this

if [ $# -eq "2" && -e $file1 ]

space was missing after file name

I want to use this command:
ls -l some_file

in my shell script

But I want to then determine if the file has execute permission for the file owner.

How could i do that?
Is there a way to then after it did the command above, to select a certain value from it eg:
-rwSr-Sr-- 1 philip philip 126 2008-10-01 17:15 test.java

Could i select the 4th character?

if [ `ls -l som_file | cut -c4` = 'x' ]
then
echo "Owner has executable permission"
else
echo "Owner doesn't hace executable permission"
fi

So for what you said the cut -c4 means select character 4 from the line abobe?

Hi Philmetz,

Yes. cut -c4 will get you the 4th character.:slight_smile:

But the proper way to test that is with test -x some_file

Actually in your original code you want

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

so what is -a?

It's "and". man test for the full scoop. It's a kitchen-sink utility for comparing various things; the shell itself is weak on those operations (although in some modern shells, test is a built-in command).