1

I am looping through a list of clearcase files to see if the text "Merge <-" is not part of the output of ct describe.

I have tried running a while loop on this list of clearcase files then appending it to another file if it meets my desired condition. Below is the exact logic I used:

16 FILTER_LIST=cut -f1 -d'@' branchmerge_versions.txt
17 touch temp.txt
18 echo $FILTER_LIST > temp.txt
19 
20 while read t; do
21    isMerged=`cleartool describe t | grep -e "Merge <-"`
22   if [[ "x$isMerged" == "x" ]]; then
23          echo "$t" >> filesToMerge.txt
24   fi
25 done < temp.txt
26

Running bash -n on the script returned these errors:

filter.sh: line 21: unexpected EOF while looking for matching ``'
filter.sh: line 26: syntax error: unexpected end of file

Why would the command backticks lead o an unexpected EOF error?

isakbob
  • 1,439
  • 2
  • 17
  • 39
  • 1
    `[[ $isMerged = "" ]]` would be sufficient, as `bash` (and almost any modern implementation of `[`) can handle empty strings just fine. `[[ -z $isMerged ]]` would be even better. – chepner Jun 17 '19 at 15:16
  • 7
    Are there any other backticks in lines 1-20? `bash` might think that the first backtick on line 21 actually *closes* a previous one, making the last one an opening backtick. Regardless, you should replace any use of backticks with `$(...)`; they are supported by `bash` and any other POSIX-compliant shell. `isMerged=$(cleartool describ t | grep -e "Merge <-")`. – chepner Jun 17 '19 at 15:21
  • 1
    did you look at the contents of your `temp.txt`? Good luck. – shellter Jun 17 '19 at 20:06
  • You have several bugs in just the short snippet you've posted. I recommend building your script in much smaller pieces that you can quickly try out, and using https://www.shellcheck.net/ to check for various sorts of issues that might not show up in basic smoke-tests. – ruakh Jun 17 '19 at 22:24
  • 1
    There's absolutely no point to `[[ "x$isMerged" == "x" ]]` as an idiom. `if ! [[ $isMerged ]]` will do fine as bash-only code, or if you want standards compliance, `[ -z "$isMerged" ]`. – Charles Duffy Jun 17 '19 at 22:25
  • 1
    ...no shell compliant with the 1992 POSIX sh standard requires the `"x$Foo"` workaround, nor any version of bash or ksh ever released. (The only cases where it can be useful on post-1980 shells are if you're combining multiple tests with the obsolescent `-a` and `-o` operators, but they're marked obsolescent in the standard, and not supported in `[[ ]]` anyhow). – Charles Duffy Jun 17 '19 at 22:25
  • 1
    Note that `grep` has a `-q` or `--quiet` flag that suppresses output, so you can actually eliminate `isMerged` completely and write `if ! cleartool describe t | grep -e -q "Merge <-" ; then` (except that I'm pretty sure you actually want `"$t"` rather than just `t`). – ruakh Jun 17 '19 at 22:29

1 Answers1

1

As I explained in "What is the difference between $(command) and `command`` in shell programming?"

embedded command substitutions and/or the use of double quotes require careful escaping with the backslash character.
We prefer $( ... )

In your case, do try with

isMerged=$(cleartool describe t | grep -e "Merge <-")

But, as commented, check first the content of your input file temp.txt.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250