DiamonW
DiamonW

Reputation: 259

Command not found error while shell scripting

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"

Upvotes: 4

Views: 3894

Answers (2)

DiamonW
DiamonW

Reputation: 259

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.

Upvotes: 2

user195488
user195488

Reputation:

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

Upvotes: 1

Related Questions