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"
Unni Kris
  • 3,081
  • 4
  • 35
  • 57
DiamonW
  • 259
  • 1
  • 6
  • 12
  • It is working to me on Ubuntu 12.0. What about your `echo`, do they show proper data when uncommented? – fedorqui May 02 '13 at 13:14
  • It worked in Fuduntu 2013.2. Do you have `sed` installed? What Linux OS are you using? – Chris Forrence May 02 '13 at 13:14
  • Have you tried sh chExt1.sh cpp test.CPP? Instead of ./chExt1.sh... – nullByteMe May 02 '13 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. – DiamonW May 02 '13 at 13:18
  • When i try the method "sh chExt1.sh cpp test.CPP" I get a bunch of not found errors on the script and I have the permissions set correctly we were told to just run chmod 777 for this assignment. – DiamonW May 02 '13 at 13:20
  • 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. – Mike Gardner May 02 '13 at 13:21
  • What if you try a basic `echo "hello" | sed 's/hello/bye/'`? Does it work? (to check if sed is ok) – fedorqui May 02 '13 at 13:21
  • Did you try this on the command line first? You are also missing a semi-colon. Meaning execute this on the command-line: `echo oldFileName | sed 's/\(.*\)\..*/\1/'` –  May 02 '13 at 13:28
  • @DiamonW can you post the output of `/bin/sh -c "echo $PATH"` – Alex May 02 '13 at 13:29
  • If I created this using a text editor on windows and transferred it over to linux via ftp and then just redirected the contents of the file into .sh files would that cause issues? – DiamonW May 02 '13 at 13:31
  • @Alex, I was going to suggest the same thing, it seems like something is wrong with the $PATH since he is getting command not found errors and he is executing without sh. – nullByteMe May 02 '13 at 13:32
  • @DiamonW: Open the file in `vi` and look for `^M` at the end of the lines. –  May 02 '13 at 13:32
  • /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games – DiamonW May 02 '13 at 13:32
  • @DiamonW I think this will help you: http://stackoverflow.com/questions/8779951/how-do-i-run-a-shell-script-without-using-sh-or-bash-commands – nullByteMe May 02 '13 at 13:33
  • It showed the that ^M commands weren't being found back when I added -x to the output script execution. – DiamonW May 02 '13 at 13:34

2 Answers2

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.

DiamonW
  • 259
  • 1
  • 6
  • 12
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

  • 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. – DiamonW May 02 '13 at 13:39
  • 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!` –  May 02 '13 at 13:40
  • see also this SO question for emacs: http://stackoverflow.com/questions/1171609/in-emacs-how-to-strip-cr-m-and-leave-lf-j-characters –  May 02 '13 at 13:41
  • or this http://stuff.mit.edu/afs/athena/project/consultdev/answers/dialup/other_clean_ctrl_m.html –  May 02 '13 at 13:42
  • It's saying that pattern ^M isn't found. – DiamonW May 02 '13 at 13:43