Results 1 to 4 of 4

Thread: How to create a batch file?

  1. #1
    Join Date
    Dec 2013
    Beans
    37

    How to create a batch file?

    Hi.
    I currently have 5 c source files I've been working on, and every time I'm making a change in one (or more) of them, I have to recompile and run again.
    This is very exhausting.
    Is there a way to create some kind of batch file with shell commands in it, that can be executed one after the other?

    To be more clear about what I'm trying to achieve here:
    I have these 5 files:
    Code:
    backup.c
    ex3.c
    ex3_input.c
    ex3_update.c
    test.c
    test2.c
    sitting in the same folder.
    I'm frequently making changes in them, and every time I have to recompile:
    Code:
    gcc ex3.c
    gcc -o input.out ex3_input.c
    gcc -o update.out ex3_update.c
    gcc -o test.out test.c
    gcc -o test2.out test2.c
    (not all of them every time, sometime I only make changes in some of them, and therefore compiling some of them)
    and run:
    Code:
    ./a.out
    What I'm trying to achive is putting all the above commands in one file that can be run with one command.
    Can it be done?

    Regards,
    goghvv


    BTW, does anyone know why Ubuntu creates file copies with ~?
    for example, every time I'm making some change in the 'backup.c' file, running 'ls -l' shows this:

    Code:
    -rw-rw-r-- 1 **** backup.c
    -rw-rw-r-- 1 **** backup.c~
    -rw-rw-r-- 1 **** ex3.c
    -rw-rw-r-- 1 **** ex3_input.c
    -rw-rw-r-- 1 **** ex3_update.c
    -rw-rw-r-- 1 **** test2.c
    -rw-rw-r-- 1 **** test.c
    What is this backup.c~ file, why is it creating it, and how can this be prevented?
    Last edited by goghvv; May 8th, 2014 at 11:35 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Magic City of the Plains
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: How to create a batch file?

    On Linux they're called shell scripts.

    Some text editors automatically create a backup file of whatever you're editing, which is the backup.c~ file you see. Check the preferences of the program you're using if you want to disable this; personally I find it useful.

  3. #3
    Join Date
    Dec 2013
    Beans
    37

    Re: How to create a batch file?

    Wonderful.
    That was very useful.
    Thanks a lot.

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How to create a batch file?

    Although a simple shell script might be enough in this case, if you're getting into C programming you should consider getting to grips with the `make` program (and makefiles) - they have lots of advantages especially as the project gets more complicated. In your case you could just type

    Code:
    make all
    and it would automatically track which source files have changed and just recompile those. I'm not too good at them myself tbh but something like this should work

    Code:
    CC = gcc
    
    all: a.out input.out update.out test.out test2.out
    
    a.out: ex3.c
        $(CC) -o $@ $<
    
    input.out: ex3_input.c
        $(CC) -o $@ $<
    
    update.out: ex3_update.c
        $(CC) -o $@ $<
    
    test.out: test.c
        $(CC) -o $@ $<
    
    test2.out: test2.c
        $(CC) -o $@ $<
    NB the indents in the above MUST be tabs not spaces

    IIRC member dwhitney67 has posted some nice makefile examples on this forum in the past - a good start would be to check those out.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •