Computing.Net > Forums > Unix > C shell unix script file

C shell unix script file

Reply to Message Icon

Original Message
Name: Qiaozhen Mu
Date: January 7, 2003 at 09:38:23 Pacific
Subject: C shell unix script file
OS: linux
CPU/Ram: --
Comment:

Hi All,

I am quite new at unix script. I have two questions to ask for your help.

1) I will do quite a lot of experiments. The output file names of different experiments are different. In the post-processing script, I need to use the file names and creat directories using a part of the file name. E.g: the output filename of one experiment is rhminl85rhminh88tau5836alfa13.cam2.h1.0001-01-01-00000.nc, another one is rhminl89rhminh92tau17736alfa24.cam2.h1.0001-01-01-00000.nc. I not only need to know the names, but also want to creat directories named rhminl85rhminh88tau5836alfa13 and rhminl89rhminh92tau17736alfa24 using the same script.

2) In the script, I use qsub to submit a job. The job will run in the background. I want to execute the remaing commands in the script after the job is finished. I don't know how to succeed this.

Someone knows how to deal with these? Please do help me. Thanks for any help in advance.

Qiaozhen


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: January 7, 2003 at 15:55:02 Pacific
Reply: (edit)

The c shell has many limitations and funny quirks - well documented. My first suggestion is that you use the korn shell, best for scripting. Even if you prefer csh for your interactive shell, the first line of your scripts can specify ksh with:

#!/bin/ksh

Your first problem is not clear to me. Do you need to process some files, but you do not know the file names? Are these files in a certain directory? If you want to process each regular file in a directory, you can have a loop like this:

for fname in `ls directory1`
do
if [ -f $fname ] ; then
print "Now processing $fname ..."
dname=${fname%%.*}
print "derived directory name is $dname"
fi

The code above also demonstrates how to pull the first part of a file name (up to the first dot) into the dname variable, and this requires korn shell. You could then create that directory, if it does not already exist, with:

if [ ! -d "$dname" ] ; then
mkdir $dname
fi

To make your script wait for the launched job to finish, I cannot help because I am not familiar with qsub. If your script were to simply run a second script, by default the second script would complete before the main script continued:

print "Now running second script ..."
./mysecondscript
print "Main continues after second script ..."

If you want the second script to run in parallel, launch with an ampersand:

print "Launching second script in parallel ..."
./mysecondscript &
print "mysecondscript now running under PID $!"


Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: January 7, 2003 at 15:57:02 Pacific
Reply: (edit)

I'll take the easy one!!

For #1, it looks like the part of the filename that you want for the directory name is everything before the first period. The following command will put that part of the filename before the period into the variable DIRNAME (I'm assuming that the filename is in the variable FILENAME):

DIRNAME=`echo $FILENAME | cut -f1 -d'.'`

Note that the outermost quotes are backquotes, not normal quotes.

Use 'man cut' to look up the meaning of the parameters to 'cut'.

So, something like:

DIRNAME=`echo $FILENAME | cut -f1 -d'.'`
mkdir $DIRNAME

Would create a directory with the name extracted from the variable FILENAME



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C shell unix script file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge