1

I did search before posting this but cant find any matching issues. It looks like my find and replace is adding a return character to the end that I cant remove. The call is sh csv2postgres.sh import-tree-references where csv2postgres.sh is:

cname=$(printf $1 | sed -e "s/-/_/g")
echo $cname
echo "${cname}.csv"
cols=`head -n 1 "${cname}.csv"`

The results are:

import_tree_references
.csvrt_tree_references
head: cannot open 'import_tree_references'$'\r''.csv'

I tried echo/printf, I tried every combinations of " and {} I could think of, I tried adding the pipe | tr -d "\r". The head command works fine if I just use $1 instead of cname and change the command to sh csv2postgres.sh import_tree_references, but the app I am working in will still be passing kebab case.

I am on ubuntu 18.10.

Any suggestions?

blindguy
  • 946
  • 10
  • 16

1 Answers1

1

The presence of \r indicates that MS Windows was involved somehow.

Here are three options:

  1. One approach is to change:

    cname=$(printf $1 | sed -e "s/-/_/g")
    

    to:

    cname=$(printf "$1" | sed -e "s/\r//g; s/-/_/g")
    

    This removes the offending \r.

    (Note that, unless you explicitly want shell expansions, all references to shell variables, such as $1 above, should be in double-quotes.)

  2. If you were using bash, not sh, you could eliminate the need to have a pipeline and a call to an external utility (sed) with:

    cname="${1//-/_}"
    cname="${cname%%$'\r'}"
    
  3. A third approach would be to find whatever file you have that has Windows line-endings and remove those line-endings with a utility like dos2unix.

Note: sh is not bash

One many systems, sh is a symlink to bash but that does not mean that sh behaves like bash. To be compatible with prior programs called sh, when invoked under the name sh, bash's advanced features are turned off and bash tries to emulate earlier less-featureful shells.

From man bash:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Id prefer the last way but for some reason ubuntu sh doesnt like it. This is weird to me because bash is just a symlic to sh. – blindguy May 03 '19 at 18:22
  • 1
    Regardless of what you claim, the symptoms are precisely consistent with what you would see if your script file had DOS line endings, and extremely hard to explain otherwise. Please double-check this. – tripleee May 03 '19 at 18:33
  • @tripleee you got it. The dos ending was inside the script file. Someone on windows must have opened it in and pushed the new line endings. – blindguy May 03 '19 at 18:40
  • 1
    Then please (perhaps upvote and accept this answer first and only then) accept the duplicate nomination. – tripleee May 03 '19 at 18:52
  • 1
    @blindguy `bash` and `sh` are _different_ even when `sh` is a symlink to `bash`. I added a section on this to the end of the answer with more details. – John1024 May 03 '19 at 19:33
  • @John1024 thanks for that, I had my symlink direction backwards – blindguy May 03 '19 at 22:10