0

I have shell script code running in GitHub Actions that gets changes and eventually what I want to happen is get an echo for each file with the file type and file name in the output.


echo "Git Add --all"
git add --all

echo "Add to Files:"
files= git diff --name-only --cached

echo "*************************************************************"
echo "GIT diff:"
echo ""
git diff --name-only --cached
echo ""
echo "*************************************************************"

echo "Files:"
echo $files

for file in $files; do
  if [ "$file" == "*.cls" ]       #  this is the snag
              then
                echo "$file Class Updated"
              fi
  if [ "$file" == "*.flow" ]       #  this is the snag
              then
                echo "$file Flow Updated"
  else
    echo "$file not found"
  fi 
done

When running I get an output

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   src/classes/xxxx.cls

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    src/flows/

no changes added to commit (use "git add" and/or "git commit -a")
Git Add --all
Add to Files:
src/classes/xxx.cls
src/flows/xxxx.flow
*************************************************************
GIT diff:

src/classes/xxx.cls
src/flows/xxxx.flow

*************************************************************
Files:


The files variable is not getting assigned the output from the git diff and does it look like once this happens the for loop will work?

Im an extreme novice when it comes to this and I'm very much in need of any assistance. Many thanks to all of those in the community

  • 2
    Does this answer your question? [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/a/4651495/7366100) – markp-fuso Nov 01 '21 at 14:10
  • No it doesnt @markp-fuso – Matt Holdgate Nov 01 '21 at 16:53
  • Note that in sh / bash syntax, `a=b cmd arg1` means *run `cmd` with argument `arg1` but first set `a=b` in its environment*. This is what you did here, though your string `b` is empty, i.e., you set `files=` in the environment for your `git diff` command. You wanted `a=$(cmd arg1)` or similar as shown in the duplicate. Since you're writing shell programs, you should get yourself a book or tutorial on writing shell programs. – torek Nov 01 '21 at 22:24
  • Thanks Torek. files=$(git diff --name-only --cached) This worked now – Matt Holdgate Nov 23 '21 at 11:14

0 Answers0