Goku
Goku

Reputation: 534

integer expression expected while running the bash script

While running my below script from the jenkins's execute shell option, I'm getting -- [: 1 2 3 4 5 : integer expression expected, I tried using > symbol too without any lucks, I'm not sure exactly where I went wrong.

Any help will be really helpful.

#!/bin/bash

declare -a folders
declare -a folders_req

db_ver=<the value which I got from my DB with trimmed leading & trailing spaces, like below> 
#db_ver=`echo $( get_value ) |sed -e 's/\-//g' | grep -oP '(?<=DESCRIPTION)(\s+)?([^ ]*)'  | sed -e 's/^[[:space:]]//g' | sed -e's/[[:space:]]*$//' | tr '\n' ' '| cut -d '/' -f2`

scripts_db_dir=`ls -td -- */ | head -1 | cut -d '/' -f1| sed -e 's/^[[:space:]]//g'`

cd ${scripts_db_dir}
folders=`ls -d */ | sed 's/\///g' | sed -e 's/^[[:space:]]//g' | sed -e's/[[:space:]]*$//' | tr '\n' ' '`

  for i in "${folders[@]}"; do

    if [ "${i}" -gt "${db_ver}" ]; then
       echo "inside loop: $i"
       folders_req+=("$i")
    fi
  done

  #echo "$i"
  #echo ${folders_req[@]}

scripts_db_dir contains directory named like - 1 2 3 4 5

Upvotes: 0

Views: 274

Answers (2)

Ian W
Ian W

Reputation: 4767

Given the various comments regarding "parsing ls is bad", consider using find instead:

find * -maxdepth 1 -type d -name '[0-9]*' -print

where:
-maxdepth 1 - searches only the current directory, no sub directories
-type d - looks only for directories
-name '[0-9]*' (or '[[:digit:]]*') - matches only items consisting of all digits
-print - just print the results

Thus:
folders=($(find * -maxdepth 1 -type d -name '[0-9]*' -print))
or just:
for i in $(find * -maxdepth 1 -type d -name '[0-9]*' -print); do

Upvotes: 1

nullPointer
nullPointer

Reputation: 4574

your folders variable should be initialized as an array and not as a string, eg :

folders=($(ls -d */ | sed 's/\///g' | sed -e 's/^[[:space:]]//g' | sed -e's/[[:space:]]*$//' | tr '\n' ' '))

Upvotes: 2

Related Questions