• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

UNIX shell script question

Special K

Diamond Member
I am trying to figure out how to use UNIX shell scripts (sch, to be exact), and am having problems with the foreach statement. The purpose of my first script is to do the following:

1. go into a directory and delete any output files that may be present from a previous run
2. go into a different directory and compile + simulate several different files, and copy their outputs to the directory that was cleaned out in step 1.

The problem I am having is that if the directory in step 1 is empty, my script just stops executing, and doesn't even try step 2. It just finishes and the command line says "foreach: No match."

Why won't it continue on and run the rest of my script?

Here is what I have:


 
Originally posted by: Childs
You need a test before you do your loop. Can be as simple as:

OK, thanks, so far that is working, but I have a couple questions:

1. In the following line:

`ls -1 $directory/*.out | wc -l`

Why did you use single quotes slanting right instead of double quotes, or single quotes slanting left?

2. When I run ls -l | wc -l in a folder with 18 files ending with *.out, the return value of that expression is 19. Why is it 19 when there are only 18 items present? Also if I do the following:

ls -l *.out | wc -l

It correctly returns 18. Why does the first case return 19, and the second return 18, when all of the files in the directory end with *.out? It seems like both commands should return the same thing.

Finally, how can I extract part of a filename from the complete path? Basically I just want the filename without the extension.
 
Originally posted by: Special K
Originally posted by: Childs
You need a test before you do your loop. Can be as simple as:

OK, thanks, so far that is working, but I have a couple questions:

1. In the following line:

`ls -1 $directory/*.out | wc -l`

Why did you use single quotes slanting right instead of double quotes, or single quotes slanting left?

Those look like back-tics (they share the tilde key). They tell the shell to run the part inside of the tics first, then the rest.

2. When I run ls -l | wc -l in a folder with 18 files ending with *.out, the return value of that expression is 19. Why is it 19 when there are only 18 items present? Also if I do the following:

ls -l *.out | wc -l

It correctly returns 18. Why does the first case return 19, and the second return 18, when all of the files in the directory end with *.out? It seems like both commands should return the same thing.

The two commands are different. The "ls -l" looks at everything and returns a slightly different format than "ls -l *.out":
:; ls -l
total 16
-rw-r--r-- 1 n0cmonkey n0cmonkey 0 Oct 12 12:05 1.out
-rw-r--r-- 1 n0cmonkey n0cmonkey 0 Oct 12 12:05 2.out
-rw-r--r-- 1 n0cmonkey n0cmonkey 0 Oct 12 12:05 3.out
-rw-r--r-- 1 n0cmonkey n0cmonkey 0 Oct 12 12:05 4.out

Finally, how can I extract part of a filename from the complete path? Basically I just want the filename without the extension.

Look at sed and awk. They're handy for stuff like this.

I have a question for you: Why csh?
 
2. When I run ls -l | wc -l in a folder with 18 files ending with *.out, the return value of that expression is 19. Why is it 19 when there are only 18 items present? Also if I do the following:

Look at the output, at least here with GNU ls if I just do 'ls -l' I get a line at the top saying "total XXXX" but if I do 'ls *.something' I don't get that line. I'm not even sure what that's the total of...

Finally, how can I extract part of a filename from the complete path? Basically I just want the filename without the extension.

Look at the basename command.
 
Originally posted by: Nothinman
Look at the output, at least here with GNU ls if I just do 'ls -l' I get a line at the top saying "total XXXX" but if I do 'ls *.something' I don't get that line. I'm not even sure what that's the total of...

Just a note, it appears to be the same way on Solaris 9 and OpenBSD too. 🙂
 
It wasnt ls -l, its is ls -1. -1 returns the output one line at a time and nothing else, which was needed for the pipe to wc -l. Anyways, that was just a 10 second example, as I dont use csh for scripting that much.

EDIT:

To answer your last question, use basename. DOH! Already answered.
 
Back
Top