Script to do a function on all files in one run


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to do a function on all files in one run
# 1  
Old 09-26-2003
Script to do a function on all files in one run

Hello everybody
I am trying to write a script in Cshell solaris 8 to read a number of files and do some commands on them (for example extracting some information) and send the results to one single file. I want to do that for all the files in one run. Could any body advise me on it.
Thanks and Regards
Reza
# 2  
Old 09-29-2003
Can you be more specific about the commands you are going to perform.

for file in *.txt
do
something on $file >>output.file
done

The output file is appended with all the results of the commands.
# 3  
Old 09-30-2003
Before you get into the loop you might also want to initialize the file first:

cat /dev/null > output.file

or you may have the results of the previous run in your output file too!
# 4  
Old 10-01-2003
Thanks for your helps
Do loop is OK but I don't know if I can introduce an array as a series of variables trough a file or by keyboard. As a simple example I want to cut few lines of each file in a directory and put it in a seperate file in the same directory.
1-Is it possible to introduce the file names as a variable to the program and
2- How can I write a script which will ask me the input value as a variable. So I can type the variable name and it will run the command on the variable (like a file name)?

Best Regards
Reza
# 5  
Old 10-01-2003
Reza.

How much do you know about shell scripting? Those are basic questions. You may do well to do some reading on Bourne, Korn and C shells in Unix. A simple search on the Internet should turn up some useful pages. Use keywords like "Bourne" "Korn" "C Shell" "Unix" "Scripting" or "Tutorial".

1. In Bourne and Korn shell you can define a variable at any time by assigning a value to it:

MYVAR=some_value

Note there are no spaces around the '=' sign and the variable name is upper case by convention.

You may return the value of a variable by preceding the name with a dollar sign:

echo $MYVAR

A script may also receive parameters from the command line in variables $1, $2 ... $9.

e.g

echo $5

would return the 5th command line argument.

So:

MY_FILE=/tmp/output.txt

cat /dev/null > $MY_FILE

or ...

cp $3 $MYFILE.old

Ok. Theres a start with shell script variables. Find out what more you can do with them.



2. To obtain a value from the user use the 'read' command. This command is built into the shell and should always be availalbe to you:

read MY_FILE

The user must enter their value followed by a carriage return, before the value is committed to the variable.

You may then access the value in the variable as before:

echo $MY_FILE



I have given you a very brief and limited overview of Bourne/Korn shell variables, but I really do recommend that you do some futher reading on this topic.

Hope this helps.

MBB
# 6  
Old 10-01-2003
Thanks Gents
I will practice much more on it. And thanks for your useful advises.
Best regards
Reza
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

2. UNIX for Dummies Questions & Answers

Run script on multiple files

Hi Guys, I've been having a look around to try and understand how i can do the below however havent come across anything that will work. Basically I have a parser script that I need to run across all files in a certain directory, I can do this one my by one on comand line however I... (1 Reply)
Discussion started by: mutley2202
1 Replies

3. Shell Programming and Scripting

Run script on multiple files

I have a script that I need to run on one file at a time. Unfortunately using for i in F* or cat F* is not possible. When I run the script using that, it jumbles the files and they are out of order. Here is the script: gawk '{count++; keyword = $1} END { for (k in count) {if (count == 2)... (18 Replies)
Discussion started by: newbie2010
18 Replies

4. Shell Programming and Scripting

Read files in shell script code and run a C program on those files

HI, I am trying to implement a simple shell script program that does not make use of ls or find commands as they are quite expensive on very large sets of files. So, I am trying to generate the file list myself. What I am trying to do is this: 1. Generate a file name using shell script, for... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

5. Shell Programming and Scripting

Script to run files with an app

hello, I need help in Unix scripting I have a whole list of file name in a Input file. I need to run the command iteratively and output the result into a text file e.g While read < Input file ; do QUERY filenane done > output.txt Appreciate your help Thank you Suhas;) (8 Replies)
Discussion started by: Suhas Kurse
8 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. Shell Programming and Scripting

howto run remotely call function from within script

Hi I have the following script : #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` } ####################################################################### # # print... (2 Replies)
Discussion started by: presul
2 Replies

8. Shell Programming and Scripting

Run function from script over ssh (BASH)

Hi, Is there any cleaver way to run function from the bash scrip over ssh? For example: #!/bin/bash #Function 1 FN1 () { ls -l } #Main run ssh user@host FN1 exit 0 Yeah, I know it will not work, but I'm asking how to make it to work :) I'm suspecting that it would be... (1 Reply)
Discussion started by: columb
1 Replies

9. Shell Programming and Scripting

shell script to run .sql files

hi Friends, Please help me in writing shell script to run list of sql files. database is Oracle 9i, unix os is solaris Requirement is 1. sql file must take two inputs a)feed id and b)business date 2.shell script must out put .xls or .csvfile as out put without trimming any column name and... (1 Reply)
Discussion started by: balireddy_77
1 Replies

10. UNIX for Dummies Questions & Answers

help to make list of files and run script..

Hey, I have finally made a command that works and now has to run it on 200+ files to run it on. How do I do that? Just fyi and if it complicates anything my commandline is: awk '{if ($1 ~ /1/) print $2}' file (yup, is should print $2 if $1 is a certain value) It doesn't work when I: ... (2 Replies)
Discussion started by: lost
2 Replies
Login or Register to Ask a Question