running C program to output in multiple locations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting running C program to output in multiple locations
# 1  
Old 10-08-2011
running C program to output in multiple locations

Hi Guys,

What I am looking at doing is to run a C program in my home directory, but output files in multiple directories BUT not at the same instance.

For e.g.

1st instance:
Run program.c and output results in path /aaa/bbb/ccc/

2nd instance:
Run program.c again and output results this time in path /ddd/eee/fff/

Thanks for your help!
# 2  
Old 10-08-2011
You'll have to save a file so you can tell if it's been run before and, if not, how many times.

Code:
int runcount(void)
{
        int count=0;
        FILE *fp;
        char path[512];
        const char *homedir=getenv("HOME");

        if(homedir == NULL) return(-1); // If HOME isn't set, we can't find the file!

        // set path to /home/username/.myprogram
        sprintf(path, "%s/.myprogram");
        fp=fopen(path, "w+"); // open .myprogram for reading and writing
        if(fp == NULL) // return a nonsense value if something goes wrong
                return(-1);

        // read a number.  If the file's empty, count will stay zero.
        fscanf(fp, "%d", &count);
        // seek back to the start of the file.
        fseek(fp, 0L, SEEK_SET);
        // overwrite it with count+1.  count+1 will always be a longer
        // string than count so this is safe.
        fprintf(fp, "%d", count+1);
        fclose(fp);
        return(count);
}

# 3  
Old 10-08-2011
Thanks for your reply.

The fact of the matter is that I do not care if the program has been run before and how many times.

TBH, I was more looking to seek an answer in terms of running a simple shell script that will help me run the same program but output results to various path locations.

So basically run program.c in my home directory but output results to path aa/bbb/c

Next, run program.c again and output results to path aa/ddddd/ee

Thanks.
# 4  
Old 10-08-2011
You want in different directories?

Following code will print the output in different log files each time you run the script.
Log files will have the name prog1.log, prog2.log etc
Code:
#!/bin/bash
log=$HOME/prog$(ls -rt $HOME/prog*.log 2>/dev/null | tail -1 | awk '{gsub(/[a-z.\/]/,"");print $0+1}').log
program.out > $log

--ahamed

---------- Post updated at 03:11 PM ---------- Previous update was at 03:06 PM ----------

Follogin will create different log directories with the naming convention of logdir1, logdir2 etc and put the log prog.log.

Code:
#!/bin/bash
log=$HOME/logdir$( ls -trd $HOME/logdir* 2>/dev/null | tail -1 | awk '{gsub(/[a-z.\/]/,"");print $0+1}' )
mkdir $log
program.out > $log/prog.log

--ahamed

Last edited by ahamed101; 10-08-2011 at 07:25 PM..
# 5  
Old 10-08-2011
Thanks but the output directories will be in another path, not in the home directory.

You know what guys, think what I want is a bit too complicated or iam being a bit ignorant and it is not actually possible.

Last option - plz help: I want to be able to use a shell script instead of a symlink - is this possible. LEt me take you through an e.g. :

If I wanted to run a program "myprogram.c" in path /aa/bb/cc, I could do as follows:

Code:
% ln -s /aa/bb/cc sl01
% sl01/myprogram

BUT, I have constantly been hearing that creating too many symlinks is not a good idea, so instead of doing this using the symlink, I want to be able to do the above procedure using a shell script.

I have already tried the following shell script:
Code:
#!/bin/bash
cd /aa/bb/cc

To run:
Code:
% . ./myscript.sh

There are a few options to use for running:
% ./myscript.sh ----> but this chanhes the directory back to home directory after run

Apparently the following preserves the changed directory:
% . ./myscript.sh ---> this seems to work for some but does not work for me. It gives me an error mesg "logout: Too many argument"


Longwinded I know but was required. Thanks for any help at all.
# 6  
Old 10-08-2011
I am confused!
Do you already have a directory structure? Or do you want to create a directory structure while executing the program?

--ahamed
# 7  
Old 10-08-2011
Quote:
Originally Posted by Jatsui
You know what guys, think what I want is a bit too complicated...
It's probably not complicated, but, to be frank, you have not done a good job of clearly explaining what you're trying to accomplish.

Even now, we don't know where these different directories that you mention come from. Are they a precompiled list? If so, are they in a file? If not, are they passed to the script as command line parameters? If not, will they be hardcoded into the script? If not a precompiled list, will the code need to walk a hierarchy at runtime to determine what directories currently exist?

You mention 'output results to various path locations'. What does that mean? Are you referring to a stream of data being redirected to a file? Are you referring to using a directory as a location for newly-created files? Are you referring to something else entirely?

What you are trying to do is probably not at all complicated, but the devil is in the details and you've given precious few.

---------- Post updated at 08:42 PM ---------- Previous update was at 08:23 PM ----------

Quote:
Originally Posted by Jatsui
If I wanted to run a program "myprogram.c" in path /aa/bb/cc, I could do as follows:

Code:
% ln -s /aa/bb/cc sl01
% sl01/myprogram

Why not the more direct /aa/bb/cc/myprogram?


Quote:
Originally Posted by Jatsui
Apparently the following preserves the changed directory:
% . ./myscript.sh ---> this seems to work for some but does not work for me. It gives me an error mesg "logout: Too many argument"
Yes. When you source a script (the dot command), the script's commands are run by the current shell, so any changes to the execution environment persist. As for the source of the error message, we'd need to see the code.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a script before and after reboot automatically and send output to two locations.

Hello Team . I am working a health check script ( bash) to run on linux server ( RedHat) and requirements are 1. The o/p of script need to be send to two diff files . I am testing with tee command . But I am not successful yet , any recommendations if that is the right approach ? 2. The same... (2 Replies)
Discussion started by: Varja
2 Replies

2. Shell Programming and Scripting

Running program and output files in specific directories

I have been running a program mseed2sac using the following command cd IV find . -type f -exec /swadmin/mseed2sac '{}' \; The problem is that I end up with a lot of files in directory IV. Instead I would like to select the designator HHZ, create a directory IV.SAC and all the files output... (11 Replies)
Discussion started by: kristinu
11 Replies

3. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

4. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

5. Shell Programming and Scripting

Passing multiple C program output into a shell

Hi I have the following C code # cat test.c #include <stdio.h> main() { printf ("The output is : Power\n"); printf ("The output is : No Power\n"); } The output of this C code is # ./test The output is : Power The output is : No Power Now i need to pass this outputs into a shell... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

6. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

7. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

8. UNIX for Dummies Questions & Answers

running script in multiple locations

Hey guys I have written a bash script that compares two directories and displays the files that are different in the directories. Now my problem is the script only runs in my home directory. What do I have to do so it will run in other directories. Thanks if anyone can help. Duplicate post.... (0 Replies)
Discussion started by: Joey12
0 Replies

9. Shell Programming and Scripting

Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??

Can someone please help?I have a file - fixed.txt----------------------------AABBBBCCCCCCDDDEEFFFFGGGGGGHHHIIJJJJKKKKKKLLL----------------------------To insert delimiters at fixed lengths of 2, 4, 6, 3, I created a file text1.txt as-------------------2463----------------------and trying to execute... (10 Replies)
Discussion started by: jd_mca
10 Replies

10. UNIX for Dummies Questions & Answers

Running a program

Hi.Iam new to Linux.i got linux 7.0 pro and dont know how to run programs. I want a perl interputer and i know i installed one but how do i run it ??? Also how do i run a C or C++ editor ?and how do i run cron ? (3 Replies)
Discussion started by: perleo
3 Replies
Login or Register to Ask a Question