0

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

Goku
  • 482
  • 1
  • 7
  • 22
  • 1
    Maybe you can further simplify the question by finding the exact line of error. Try running the script manually with following flags `bash -ex script.sh`. `-x` shows debug output, `-e` terminates execution right after the first non zero error code. – Anubis Apr 23 '19 at 10:09
  • 1
    Also, I will point you to the famous page [Why you shouldn't parse the output of ls](http://mywiki.wooledge.org/ParsingLs)! I would suggest you to loop through directory content using a glob. e.g. `for file in *; do echo $file; done` – Anubis Apr 23 '19 at 10:15
  • 1
    @anubis Except you should [quote the variable](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Apr 23 '19 at 10:35
  • Ah, yes ;) I overlooked that as it was an example. But you are correct. – Anubis Apr 23 '19 at 10:47
  • @Anubis: Thank you for ur valuable input and the link shared :) – Goku Apr 23 '19 at 11:28

2 Answers2

2

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' ' '))
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • ... Which probably is better written `folders=(*/)` if I'm guessing the intent correctly. Definitely avoid [parsing `ls` output.](http://mywiki.wooledge.org/ParsingLs) – tripleee Apr 23 '19 at 10:34
  • @funkyjelly: Worked perfectly, I was under impression that I've declared the array, so "ls parsed output" will be taken as an array. Could you kindly give me links where I can read much into it, thanks – Goku Apr 23 '19 at 11:30
  • here's a [link](https://linuxconfig.org/how-to-use-arrays-in-bash-script). I'd also suggest to take into account @tripleee suggestion in these comments. – nullPointer Apr 23 '19 at 11:42
  • 2
    `variable=string` (or `variable=\s\t\r\i\n\g` or `variable="string"` or `variable='string'` or `variable=$(echo string)` with a command substitution) creates a simple scalar variable. `variable=(expression)` creates an array. – tripleee Apr 23 '19 at 11:51
  • funkyjelly & @tripleee : vivid explanation, thanks – Goku Apr 23 '19 at 14:13
1

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

Ian W
  • 4,559
  • 2
  • 18
  • 37