Bash errors and script if condition

I am working on a Bash script on RedHat Linux 8.5

I want to create a script that will process a directory, analyse the sub-folders and create a built script.

Its a painful process as I’m not very experienced with writing bash scripts, so far:

#!/bin/bash
cd ~/work-mmcm2
ls -d * >../dirs.txt
pass=1
while read p; do
  if [$pass -eq 1]; then
    cd $p
  #Convert folder to lower case and append .pro extension
    profile=${p,,}.pro
  fi
  echo "Pass[$pass] Testing: $profile"
  pass=1
  #Does file exist?
  if [ -f $profile ]; then
    echo "$profile Exists."
  else
    if [$pass = 1]; then
  #Change first three characters to uppercase and try again
      profile=${p^^^}.pro
      pass=2
    else
      echo "$profile does not exist."
      exit
    fi
  fi
  cd ..
exit
done <../dirs.txt

cd ..

The above is very much early stages and a work in progress, however when I try to run it I get:

line 6: [1: command not found
Pass[1] Testing:
 Exists.

What have I done wrong on line 6 ?

if [$pass -eq 1]; then

>Solution :

if [$pass -eq 1]; then must be if [ "$pass" -eq 1 ]; then. The name of the program in [ (located at /usr/bin/[). Same way you write cat file and not catfile (or cat$var).

It also pays off to quote all variable expansions. Instead of $var it is 99% of the cases better to have "$var", because that will preserve whitespace in the variable value.

Leave a Reply