0

I want to check directory 1 8 and 4 present inside a list of parent directory that is passed as argument to the script. PFB my script

#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do

cd $line
echo "checking in $line" >> ../filesearch.log

if [ [ -d 1]  &&  [ -d 8]  &&  [ -d 4] ]; then
echo "1 8 4 exists" >> ../filesearch.log
else
    echo "Dir 1 8 4 does not exist" >> ../filesearch.log
fi
cd ../

done < "$1"

I am getting the below error when i use if [ [ -d 1] && [ -d 8] && [ -d 4] ];

 ./fs.sh: line 8: [: -d: binary operator expected

if we use [[ -d 1 ]]; instead of if [ [ -d 1] && [ -d 8] && [ -d 4] ]; my code is working fine but my requirement is to check for all the above mentioned files(1 4 8) are present in the parent directory or not.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

3 Answers3

1

Your underlying problem here is that you assume that [ is just same kind of parentheses, and that you can nest them. That is not true. The expression [ ... ] is the same as test ....

The test utility has the possibility to combine multiple test with -a for and and with -o for or. See the manual for details.

So you can write your check as test -d 1 -a d 4 -a -d 8 or [ -d 1 -a d 4 -a -d 8 ]

RalfFriedl
  • 8,981
0

Do it like this if [ -d 1 ] && [ -d 8 ] && [ -d 4 ]

edit: disregard, the answer below is the correct one.

0

Try this,

if [[ -d 1 && -d 8 && -d 4 ]];
Siva
  • 9,077