0

I'm modifying this recursive directory crawling script from https://unix.stackexchange.com/questions/494143/recursive-shell-script-to-list-files

my plan is just to concatenate strings of the output of text files in my directory

so like: content+= content + cat file.txt, I feel like what I have isn't too far off but right now the output is blank and I guess I'm misunderstanding how to separate the cat function from the string concat portion

I tried using less instead of cat but that produces the same result I'm following the link below but none of the examples seem to produce an output for me How to concatenate string variables in Bash

#! /bin/bash

echo "starting script"
content="start =>"

walk_dir () {    
    shopt -s nullglob dotglob

    for pathname in "$1"/*; do
        if [ -d "$pathname" ]; then
            walk_dir "$pathname"
        else
            case "$pathname" in
                *.txt|*.doc)
                    #printf '%s\n' "$pathname"
                    content+=(cat "$pathname") #add file contents to the previous string
                
            esac
        fi
    done
    echo "$content"

}
DIR=/c/Users/xxxx/Desktop/xxxx

walk_dir "$DIR"

pretty new to bash scripting but

monsterpiece
  • 739
  • 2
  • 12
  • 34

2 Answers2

1

Had a quick look - I think you are missing the $ sign in concat.

content+=$(cat "$pathname")

Alternatively, you can use backquote instead of parentheses without $.

Krish
  • 79
  • 3
0

You will need to say content+="$(cat "$pathname")" to append the output of cat to the scalar variable content.
Or you can just say:

shopt -s extglob globstar
content="$(cat **/*@(.txt|.doc))"
echo "$content"

to concatenate the contents of the text/document files recursively.

tshiono
  • 21,248
  • 2
  • 14
  • 22