4

I'm trying to execute the program as followed.

./chExt1.sh cpp test.CPP  

This should rename test.CPP to test.cpp but I don't even think this script is executing at all.
I am consistently getting this "command not found error".
The script is below :

#!/bin/sh
newExtension=$1;
oldFile=$2;

        firstPart=`echo $oldFile | sed  's/\(.*\)\..*/\1/'`
    newName="$firstPart.$newExtension";

#echo $oldFile
#echo $newName
mv "$oldFile" "$newName"
#echo "$oldFile"
#echo "$firstPart"
#echo "$newName"
15
  • It is working to me on Ubuntu 12.0. What about your echo, do they show proper data when uncommented? Commented May 2, 2013 at 13:14
  • It worked in Fuduntu 2013.2. Do you have sed installed? What Linux OS are you using? Commented May 2, 2013 at 13:14
  • Have you tried sh chExt1.sh cpp test.CPP? Instead of ./chExt1.sh... Commented May 2, 2013 at 13:17
  • I'mn using Ubuntu 12.04.1 LTS I haven't tried running with them uncommented but I'll check that out. Commented May 2, 2013 at 13:18
  • 4
    add -x to your #! line to output script execution to stdout. #!/bin/sh -x It really helps with troubleshooting..you can see what is going on while it executes. Commented May 2, 2013 at 13:21

2 Answers 2

2

I finally fixed the issue. Something went horribly wrong when I FTP'd the text file which contained the script and then just transferred it inside of a .sh in linux. I wrote in from scratch in emacs and that cleared everything up.

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

Comments

1

Based on your comment, do this in vi to remove the extra control characters. I have had this problem before when editing files in gedit or when editing in Windows and then using on a Unix/Linux machine.

To remove the ^M characters at the end of all lines in vi, use:

:%s/^V^M//g

The ^v is a CtrlV character and ^m is a CtrlM. When you type this, it will look like this:

:%s/^M//g

In UNIX, you can escape a control character by preceeding it with a CtrlV. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).

Source

5 Comments

Is there an emacs equivalent of this? I haven't had to learn vi at all this semester so I have no idea how to use it.
If you don't know vi, then type vi filename at the command line, then type the command just as shown (colon and everything). Then to save, type SHIFT+Z+Z (do not press the + sign). Or you can type :wq!
see also this SO question for emacs: stackoverflow.com/questions/1171609/…
It's saying that pattern ^M isn't found.

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.