0

I am trying to figure out a way to automatically change version number and for that I have a script which tries to read a file and then change it.

\\ students.txt
this is a text, text is not so bad, and more text

I have students.txt and changetext.sh files in the same directory.

\\ changetext.sh
sed -i='' "s/, /- /g" students.txt

When I run this command on Cygwin on Windows64 bit system with Windows 10, it works well on the console. But when I try to run this command from shell script as shown above, it gives following error.

$ ./changetext.sh
: No such file or directoryt

I am not sure what is the issue? Can someone please guide me on this?

Piyush Patel
  • 1,646
  • 1
  • 14
  • 26
  • Try changing the script so it says "./students.txt" instead of "students.txt" and see if that makes a difference. – Dark Matter Apr 26 '18 at 19:41
  • I tried that as well but it still throws the same error. I believe there is something related to first single quoted string or there is some issue related to cygwin on 64 bit system. Not sure. I m new to cygwin – Piyush Patel Apr 26 '18 at 19:42
  • When it says "No such file or directory", what else does it say? I.e. what is the full error line? It will probably also say either "bash: ./changetxt.sh" or "sed: can't read students.txt" – Dark Matter Apr 26 '18 at 19:45
  • oh this is all it says. This is actually cygwin console output that I copied. I will reformat it properly. – Piyush Patel Apr 26 '18 at 19:47
  • A blank "No such file" probably means it thinks it's looking for an invisible character and/or the line is mal-formatted. I copied all of your script into my terminal and ran them and it worked just fine so the syntax is fine here. You might copy these here into in a different directory and see if it also works for you. Did you copy them directly into this post with the clipboard or just retype them by hand? – Dark Matter Apr 26 '18 at 19:51
  • Yes, I think it should work but I m using Windows. Which system are you using? I believe this has to do with the OS – Piyush Patel Apr 26 '18 at 19:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169895/discussion-between-dark-matter-and-piyush-p). – Dark Matter Apr 26 '18 at 20:01

2 Answers2

0

I get different behavior on my copy of Cygwin (about 6 months old under Win10), but your use of -i is incorrect.
You want "-i[SPACE]" That's not [SPACE] but a literal space of course. It's trying to make a = suffix with your use. It's probably interpreting the '' on your system differently than mine as well.

Ahh....and if I'm in a Windows directory I get errors. I believe whatever filename it's trying to generate as your backup file is legal for Cygwin, but not for Windows.

If you want a backup of the file use -i~ or -i.bak or something along those lines.

Another potential problem with your script is the lack of a shebang to specify which shell should be used to interpret it. The weirdness of -i='' is very likely to be interpreted differently by different shells as well. The bash shebang is typical:

What is the preferred Bash shebang?

zzxyz
  • 2,953
  • 1
  • 16
  • 31
  • 1
    Thanks zzxyz for your response and more information. Yes, I found that but it works similarly even if I add space after `-i `. I just pasted that code because I got it from internet and wanted to modify and during my search I also found about backup, but I didn't bother about changing the suffix. – Piyush Patel Apr 27 '18 at 02:56
0

Ok, I found the answer. I think this is Cygwin issue because I tried the same script on ubuntu OS and it works perfectly fine.

I am not sure about this issue but someone can correct me if I am wrong. I tried on Ubuntu 16.04 and it worked, but it didn't work on Windows 10 with Cygwin.

Piyush Patel
  • 1,646
  • 1
  • 14
  • 26
  • I think you're wrong about it being a Cygwin issue. I just tried it under Cygwin and it works as expected. "As expected" means that the first ',' got replaced with a '-' character and a backup of file was made under the name `students.txt=`. Note that the latter is what you asked for: `-i=''` is equivalent to `'-i=`, i.e., make a backup of the original with a suffix '='. And, yes, you definitely *should* add a shebang; in this case `#!/bin/sh` is appropropriate. – varro Apr 27 '18 at 19:46
  • Sorry, that should have been: `-i=''` is equivalent to `-i=`. – varro Apr 27 '18 at 19:53