Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#!/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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading