You still have multiple problems in your script.
First off, a text editor is the right way to create a script 99% of the time. Building it manually line by line with echo commands is tedious and offers no advantage at all. (The other 1% covers cases where you're on a system that doesn't have an editor you're familiar with, or where there's a good reason to use a program to generate a script automatically, neither of which should apply in your case.)
The first line should be
#!/bin/tcsh -f, not
-f tells it not to source your startup files (
.login), so your script will be self-contained. The lack of
-f probably won't cause any problems, but having it is good practice. (Note: this does not apply to
bash scripts; for those shells,
-f means something else entirely.)
sed "s/_/-/g" $sql
reads each file, changing all underscores to hyphens -- but the output is written to stdout, not back to the input file. The files themselves are not changed. Since you later do this:
echo You changed $n files
I presume that doesn't fit in with your plans.
Modifying files in place is actually moderately tricky. I'd do something like this:
sed "s/_/-/g" $sql > $sql.tmp && mv $sql.tmp $sql
This modifies the file
$sql, writing the modified version to a temporary file. If that succeeds, it then renames the temporary file to
There's also a
-i option to
sed that will do something similar; another approach is:
sed -i.bak "s/_/-/g" $sql
Indentation is your friend. Everything between the
end lines should be indented to show the structure of your code. (I use 4 spaces myself; some people prefer a different number of spaces, or use tabs.)
chmod 777 test should not be in the
test script itself. You need to execute that command from the command line, to make the
test script executable. And
777 is a bad idea; it makes your script readable, executable, and writable by everyone on the system. Use
chmod +x test instead.
Be sure you execute the script by typing
./test, not just
test. (Your question says you're already doing that.) Otherwise, you might execute the built-in
test command rather than your script. There's nothing wrong with naming your script
test, as long as you (a) consistently use the
./test syntax to invoke it, and (b) you don't have the
. directory in your
If you're going to be writing a lot of scripts, you should consider using
bash rather than
tcsh. This essay explains why.
Finally (and this might be the most important lesson here), you haven't actually told us what you're trying to do. I think you want to modify the contents of certain files, replacing underscores with hyphens. Or do you want to change their names rather than their contents? It's impossible to be sure what you're trying to do, because all you've shown us is a chunk of code that doesn't work. There's nothing wrong with showing us non-working code, but we can't advise you how to fix it unless you tell us, in English, what you're trying to accomplish.