0

I am trying to run the following script on a OpenSuse machine:

OUR_REPO=$(git branch | grep "*" | awk '{print $2}')
THE_REPO_THAT_OURS_IS_BASED_ON="origin/$OUR_REPO"
GIT_COMMIT=$(git merge-base $OUR_REPO $THE_REPO_THAT_OURS_IS_BASED_ON)
echo "$OUR_REPO"
echo "$THE_REPO_THAT_OURS_IS_BASED_ON"
echo "$GIT_COMMIT"

and I get the following error:

fatal: Not a valid object name release/VERSION?
release/VERSION
origin/release/VERSION

The interesting part is that, if I am running each of those commands directly into the terminal everything works properly.

Also, if I am trying to run this script via my local machine with Fedora, I don't have any kind of issue.

The command that I use to run the script: bash my_script.sh

On the OpenSuse machine, if has any importance, I am using zsh.

Does any of you know what I am doing wrong?

Mircea
  • 121
  • 1
    Do you really get the error message with the script shown in the question? The script has 6 lines while the error refers to line 8. The \r might mean that the script has DOS line endings which should be converted to UNIX line endings. Did you create the script with a Windows text editor? – Bodo May 13 '22 at 09:58
  • You also forgot to quote some of the parameter expansions. See When is double-quoting necessary? (in short, it's needed in most Bourne-like shells, less so in zsh). – Stéphane Chazelas May 13 '22 at 09:58
  • @Bodo I've updated the error, the \r is was because of an empty line. – Mircea May 13 '22 at 10:01
  • @Mircea it's still the same error: the ? in VERSION? is the \r. Are you opening/editing the file in a Windows machine? That's what is causing the problem. – terdon May 13 '22 at 10:02
  • 1
    @Mircea, there's normally no CR characters in Unix text files. If you had one in the empty line, you likely add one on every line because the file was created on Windows. Run dos2unix on the file to fix it. – Stéphane Chazelas May 13 '22 at 10:02
  • You can paste your script on https://www.shellcheck.net/ to check for errors. Do you want to search for a literal * with grep "*"? Then you should use fgrep instead. – Bodo May 13 '22 at 10:04
  • @Bodo, I did and I fix all of those errors, but I still get the same issue. – Mircea May 13 '22 at 10:08
  • @Bodo, grep "*" works OK here. In BREs, * at the start of the regexp or following \( matches itself. That's different with EREs though. – Stéphane Chazelas May 13 '22 at 10:08
  • Sorry, cannot comment, so writing in answer. I would add set -x to the beginning of a script, it would show which command it executes when it is getting an error.
    #!/bin/bash
    set -x
    OUR_REPO=$(git branch | grep "*" | awk '{print $2}')
    THE_REPO_THAT_OURS_IS_BASED_ON="origin/$OUR_REPO"
    GIT_COMMIT=$(git merge-base $OUR_REPO $THE_REPO_THAT_OURS_IS_BASED_ON)
    echo "$OUR_REPO"
    echo "$THE_REPO_THAT_OURS_IS_BASED_ON"
    echo "$GIT_COMMIT"
    
    – BiG_NoBoDy May 13 '22 at 10:02
  • If I do that, I will get this error about my already existing error message: – Mircea May 13 '22 at 10:03
  • : invalid option set: - – Mircea May 13 '22 at 10:03
  • Better to run bash -x ./the-script. Here, if editing with vim for instance, vim would honour the MSDOS line ending so you'd end up adding a set -x<CR> line and you'd get some obscure : invalid optiont: - error message by bash (CR moves the cursor back to the left of the screen when sent to a terminal) – Stéphane Chazelas May 13 '22 at 10:04
  • I did not notice, you use zsh, then that will not work. I am not familiar to zsh. – BiG_NoBoDy May 13 '22 at 10:05
  • set -x works with zsh as well (or any Bourne-like shell), but here the OP is running bash my_script.sh anyway and the shell you run that from is not relevant. – Stéphane Chazelas May 13 '22 at 10:06
  • this is the output if I will do bash -x my_script.sh OUTPUT ++ git branch `++ grep '*' ++ awk '{print $2}'
    • OUR_REPO=$'release/VERSION\r'
    • THE_REPO_THAT_OURS_IS_BASED_ON=$'origin/release/VERSION\r\r'

    ++ git merge-base $'release/VERSION\r' $'origin/release/VERSION\r\r' fatal: Not a valid object name release/VERSION?

    • GIT_COMMIT=$'\r'
    • echo $'release/VERSION\r\r'

    release/VERSION

    • echo $'origin/release/VERSION\r\r\r'

    origin/release/VERSION

    • echo $'\r'`
    – Mircea May 13 '22 at 10:07
  • @BiG_NoBoDy Please don't post answers unless you are actually answering the question. I realize you cannot comment, but that doesn't mean you should post an answer to circumvent the limitations. – terdon May 13 '22 at 10:08
  • @Mircea as we've been telling you, you have \r (CR) in your script which means you probably edited it on a Windows machine at some point. You need to remove them, which you can do with dos2unix or tr -d '\r' < file or sed 's/\r//g' file. – terdon May 13 '22 at 10:10
  • 1
    @Mircea Please add all requested information to the question and format it properly instead of using comments for this purpose. If you fixed the errors, show the fixed script in your question. – Bodo May 13 '22 at 10:13
  • I am marking this as a duplicate. Please let me know if the answers in the duplicate do not solve your issue. – terdon May 13 '22 at 10:15

0 Answers0