-1

Below is a simple script to find out whether all files exists and are having size greater than zero or not. From here I know '-s' is used for the task.

if [ -s ${file1} && -s ${file2} && -s ${file3}]; then
   echo "present"
   echo "Perform analysis"
else
   echo "not present";

   echo "Value of file1: `-s ${file1}`"
   echo "Value of file2: `-s ${file2}`"
   echo "Value of file3: `-s ${file3}`"
   echo "skip";
fi

The files are present on the path same as the script. I have checked the files names and it is correct. I am getting the following error :

./temp.ksh[]: [-s: not found [No such file or directory]
not present
./temp.ksh[]: -s: not found [No such file or directory]
Value of file1:
./temp.ksh[]: -s: not found [No such file or directory]
Value of file2:
./temp.ksh[]: -s: not found [No such file or directory]
Value of file3:

I can't seem to find out what's wrong with above. Is this specific to KornShell? I can only use KSH.

kvantour
  • 25,269
  • 4
  • 47
  • 72
Ankit Chauhan
  • 646
  • 6
  • 20
  • You need an extra space between the closing square bracket. `if [ -s ${file1} && -s ${file2} && -s ${file3} ];` – kvantour Oct 18 '18 at 08:46
  • I tried adding extra space. Now the error is ./temp.ksh[]: [: ']' missing Between.... echo should work at least. – Ankit Chauhan Oct 18 '18 at 08:50

3 Answers3

1

With reference to this question's answer. The error is using [ ] in if statement instead of [[ ]] as [[ ]] can interpret && but [ ] can't.

Ankit Chauhan
  • 646
  • 6
  • 20
0

You seem to misunderstand the test command which is also written as [ Conditional Expression ]. Conditional expressions can be written inside a test command but not used as an executable statement.

So don't do

echo "Value of file1: `-s ${file1}`"

But do

echo "Value of file1: $( [[ -s ${file1} ]] && echo 0 || echo 1)"

Also, the -s does not return the size, but checks if the size is zero or not as a return code.

Furthermore, the test command does not know && (as mentioned by Ankit Chauhun's answer).

So don't do

if [ -s ${file1} && -s ${file2} && -s ${file3} ]; then

But do any of

if [ -s ${file1} ] && [ -s ${file2} ] && [ -s ${file3} ]; then
if [[ -s ${file1} && -s ${file2} && -s ${file3} ]]; then

You might be interested in:

if [[ -s ${file1} && -s ${file2} && -s ${file3} ]]; then
   echo "present"
   echo "Perform analysis"
else
   echo "not present";

   stat --printf="Value of file1: %s" "$file1"
   stat --printf="Value of file2: %s" "$file2"
   stat --printf="Value of file3: %s" "$file3"
   echo "skip";
fi
kvantour
  • 25,269
  • 4
  • 47
  • 72
  • I did what you suggested. But the echo output is blank and for if statement it is : ./temp.ksh[]: [: ']' missing . I also printed the file names and tried to cat the same name and I am able to see the contents of file. – Ankit Chauhan Oct 18 '18 at 08:59
  • Now I am getting 0 in echo which you suggested after **But do**. But for the if statement it is still giving me same error which I mentioned in above comment. – Ankit Chauhan Oct 18 '18 at 09:05
0

The other answers look good to me but I had to try it just to see. This works for me:

file1=a.txt
file2=b.txt
file3=c.txt

if [[ -s ${file1} && -s ${file2} && -s ${file3} ]]
then
    echo "present"
    echo "Perform analysis"
else
    echo "not present";

    for name in ${file1} ${file2} ${file3}
    do
        if [[ ! -s ${name} ]]
        then
            date > ${name}  # put something there so it passes next time
            echo "just created ${name}"
        fi
    done
    echo "skip";
fi

I put that into a file called checkMulti.sh and got the following output:

$ ksh checkMulti.sh
not present
just created a.txt
just created b.txt
just created c.txt
skip
$ ksh checkMulti.sh
present
Perform analysis
$ rm a.txt
$ ksh checkMulti.sh
not present
just created a.txt
skip
$ ksh checkMulti.sh
present
Perform analysis

The use of single brackets [ went away with ksh 88. I suggest always using double [[ these days.

Also, check to make sure you have spaces before and after the brackets and before the dashs. I just tried to reproduce your error and got (this is wrong):

$ if [[-s fail]]   #<<< missing space before -s and after filename
> then
> echo yes
> fi
-ksh: [[-s: not found [No such file or directory]
$

But if I put in the spaces that are needed (and create a file) I get:

$ date > good  # put something in a file
$ if [[ -s good ]]
> then
>     echo yes
> fi
yes
$
user1683793
  • 1,213
  • 13
  • 18