The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Simple script in linux:

MrOlettaMrOletta Registered User regular
edited May 2007 in Help / Advice Forum
NEW PROBLEM:

I'm new to programming a shell script. In fact I just started reading on it, heh! I have a huge list of files, and I'm looking to write a shell script that'll execute one line for each file in the directory.

So...let's say I have a folder with file names such as 101010.ctl, 101011.ctl, 101012.ctl and I want to run the command
mpb 101010.ctl>&101010.out

How do i get the shell script to step through the files in the directory executing that command with the particular filename.






SECOND PROBLEM SOLVED:
string itos(int i)	// convert int to string
	{
		stringstream s;
		s << i;
		return s.str();
	}

Hey everyone,
I'm working on a program to write files that I need to run calculations on.

Basically the user will enter values called
center
x
y

I'm going to run some code, but basically I want it to output a file with the name

center + x + y.ctl

So say our center was 10, our x was 20, and our y was 30-32:

102030.ctl
102031.ctl
102032.ctl

Usually if it was just a string, I could use something like
outData.open(output.c_str() );

where the user specified output.

Anyone point me in the right direction? Or what command I should be looking at? It's been about 3 years since i've had C++.

So basically i'm looking for something like
outData.open(centerxy.ctl);

MrOletta on

Posts

  • nethneth Registered User regular
    edited May 2007
    just concatenate the string and then open it.

    string filename = center +x + y+".ctl";
    outstream.open(filename);

    neth on
  • SmasherSmasher Starting to get dizzy Registered User regular
    edited May 2007
    This should work:

    int center = 10878, x = 27897, y = 34;
    char d[50];
    sprintf(d, "%d%d%d.ctl", center, x, y);
    ofstream fout(d);
    fout << "hello world" << std::endl;

    sprintf can also be used for floats and a bunch of other types by changing the %d.

    Smasher on
  • Jimmy KingJimmy King Registered User regular
    edited May 2007
    MrOletta wrote: »
    NEW PROBLEM:

    I'm new to programming a shell script. In fact I just started reading on it, heh! I have a huge list of files, and I'm looking to write a shell script that'll execute one line for each file in the directory.

    So...let's say I have a folder with file names such as 101010.ctl, 101011.ctl, 101012.ctl and I want to run the command
    mpb 101010.ctl>&101010.out
    

    How do i get the shell script to step through the files in the directory executing that command with the particular filename.


    Well, you said second problem solved, so I'm not sure if you meant this one or the old one which was now the second in the post.

    Anyway, like so.
    #!/bin/bash
    
    for i in `find . -name "*.ctl" -print 2>/dev/null`
    do
        echo Processing file: $i
        mpb $i.ctl>&$i.out
    done
    echo END: `date`
    

    Well, that'll end you up with, say 101010.ctl.out, but you can run it through the cut command to grab just the name without the extension if you want.

    Jimmy King on
  • JaninJanin Registered User regular
    edited May 2007
    Use "basename" to chop off the file extension.

    basename somefile.ext .ext # returns "somefile"

    Janin on
    [SIGPIC][/SIGPIC]
  • MrOlettaMrOletta Registered User regular
    edited May 2007
    That's perfect. Thanks everyone for the help. Now to let the computers chug out the computational analysis for a good 36 hours.

    MrOletta on
  • UndefinedMonkeyUndefinedMonkey Registered User regular
    edited May 2007
    MrOletta wrote: »
    NEW PROBLEM:

    I'm new to programming a shell script. In fact I just started reading on it, heh! I have a huge list of files, and I'm looking to write a shell script that'll execute one line for each file in the directory.

    So...let's say I have a folder with file names such as 101010.ctl, 101011.ctl, 101012.ctl and I want to run the command
    mpb 101010.ctl>&101010.out
    

    How do i get the shell script to step through the files in the directory executing that command with the particular filename.

    If you really want to be insane:

    1. Get a newline-separated list of all the files in the directory that you want. Use ls and pipe it into a file (might have to set --color=no depending on how you have your terminal set up.)
    2. Process this list with sed. Do this: cat filenamelist | sed -e 's/\(^[a-zA-Z0-9_]*\)\.ctl/mpb \1.ctl >& \1.out' > exelist
    3. cat exelist... you should have a list of commands in the form "mpb 101010.ctl >& 101010.out", separated by newlines.
    4. If so, chmod exelist to 777 and run it.

    UndefinedMonkey on
    This space intentionally left blank.
Sign In or Register to comment.