Search This Blog

Thursday, June 04, 2009

running external commands

  1. To run external commands while editing a file in vim, use the '!' in the normal mode. For example
    :!ls -al
    will list the files
    :!date
    will display the current date.

  2. To read the output of external commands into the current file, do
    :r !date
    All the commands are run in normal mode. Press ESC key to enter the normal mode in vim.

    Further reading:- :help :!

  3. To run external commands in octave, use the system command. Sample octave session looks as
    $octave -q
    octave:1> system("date")
    Thu Jun 4 23:49:23 EDT 2009
    ans = 0
    octave:2> [ret_code output] = system("date");
    octave:3> ret_code
    ret_code = 0
    octave:4> output
    output = Thu Jun 4 23:49:40 EDT 2009

    octave:5> exit
    Further reading :- "doc system" shows the relevant help pages in octave.

  4. To run external commands in Fortran 90 programs, use the system command. Sample code looks as below
    $cat system.f90
    program callsystem
    implicit none
    !to examine the behaviour of the system command
    character (len=100)::cmd
    cmd="echo Wake up Neo"
    !if u are using ifc compiler use -Vaxlib during compilation
    call system(cmd//achar(0))
    call system(cmd)
    call system("date")
    ! The next line also works.
    ! call system("ls")
    end program callsystem

    $gfortran system.f90

    $./a.out
    Wake up Neo
    Wake up Neo
    Thu Jun 4 23:55:07 EDT 2009
  5. To run the external commands in C, use the system command available in stdlib.h. Sample code will be
    $cat system.c
    #include stdio.h
    #include stdlib.h

    int main() {
    /* Fixme :- <, > in the header files are not showing up on blogspot */
    int ret_code;
    ret_code = system("date");

    printf("%d\n", ret_code);
    return 0;
    }

    $gcc -Wall system.c

    $./a.out
    Fri Jun 5 00:04:14 EDT 2009
    0

    Further reading :- man system

    All the above are tested in Debian Lenny using vim 7.1, octave 3.0.1, gfortran 4.3.1, gcc 4.3.1

2 comments:

Anonymous said...

You have a great blog here, lots of useful information presented in an easy to read format. Well done - your feed has a home in my reader from now on.

Kamaraju Kusumanchi said...

Thanks for the kind words. That just made my day!

Followers