1

Here's the deal. I've got cygwin installed in Win7 envionment. This is the sequence of things I would do in a command line and everything works,

File mpc.exe is a 64-bit executable created by Intel Fortran Compiler

cp ./dir1/dir2/mpc.exe ./mpc.exe
./mpc.exe arg1 arg2

everything is fine

Want to create a script for that. The reason is I will want to execute the code for various values of arg2. file "script_mpc.sh" contains following,

#!/bin/sh
cp ./dir1/dir2/mpc.exe ./mpc.exe
./mpc.exe arg1 arg2
wait
return_val=$?
[ $retval -eq 0 ] && echo "successfully executed "

Now back at the command line,

$>chmod +x script_mpc.sh
$>./script_mpc.sh

error:

./script_mpc.sh: line 2: ./mpc.exe: No such file or directory

A very fresh beginner. Learning shell commands and scripting on the go. Please help.

11
  • If you really expect this to be a bash script, you need #!/bin/bash at the top, not #!/bin/sh But I don't know if that's your problem, as your files are named with .sh so maybe you don't mean bash in the question. Commented Jul 20, 2012 at 19:22
  • Did script successfuly copy mpc.exe fo current directory? Try to specify the full path to source mpc.exe in cp command. Commented Jul 20, 2012 at 19:30
  • @Almo: Thanks for replying. I experimented with bash instead of sh at the header. Doesn't make much of a difference there. Same errors. Commented Jul 20, 2012 at 19:37
  • @rush: I have every reasons to believe the executable was properly copied. The files at destination and source have the same size (i.e. 7053 KB to be precises). However, there's a weird thing going on for sure. Once the copy process is complete, the windows folder does show the following marker for the file: " mpc.exe * ". In detail, the executable name is followed by a white space and a what looks like a floating 'black dot'. I don't know how to not have these characters at the end. They're there regardless of what extension I use, including if I do not specify any extension at all. Commented Jul 20, 2012 at 19:43
  • However, those extraneous characters at the end, 'white space+floating black dot' doesn't show if the same command, 'cp ./dir1/dir2/mpc.exe ./mpc.exe' is typed at the command line. Commented Jul 20, 2012 at 19:44

1 Answer 1

1

You're on Cygwin.

I'll bet that this line:

cp ./dir1/dir2/mpc.exe ./mpc.exe

has a Windows-style CR-LF line ending. The shell (either sh or bash) interprets the CR as part of the filename, so it copies the file to "./mpc.exe\r".

Filter the script through dos2unix. Be sure to read the man page first; unlike most text filters, it normally overwrites the input file.

Background:

Unix uses a single ASCII LF character to mark the end of a line in a text file. Windows uses a CR-LF pair. Cygwin is a Unix-like emulation layer on top of Windows, so it tends to be a rich source of problems with conflicting end-of-line representations.

Unix shells, in particular, typically don't recognize the CR as part of an end-of-line indicator; instead, they treat it as just another character -- one that tends to be invisible, depending on how you look at the file.

You probably have a mixture of LF and CR-LF line endings. If it used CR-LF endings consistently, then the #!/bin/sh or #!/bin/bash line wouldn't be recognized.

If possible, use only Unix-style editors (vim, emacs, nano, or whatever you prefer) to edit shell scripts. If you create a script using, say, Notepad or Wordpad, you're likely to run into this kind of problem.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you hit the bull's eye in there. I did use a windows based text editor to create the script. Being a starter, I thought I'd take the easy way out for now until I've mastered some of these subtle formats for linux systems. I think that should work. Thanks again.
@user1538324: I see you're new here; welcome! A couple of things to be aware of: If you like or dislike an answer or question, you can upvote or downvote it by clicking one of the arrows next to the title; this applies to any post. If an answer actually answers your question, you can accept it as the correct answer by clicking the checkmark; you can do this only for questions you asked yourself. There may be some restrictions until you get a few more reputation points.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.