Recent

Author Topic: Control console app by shell script  (Read 1759 times)

whiffee

  • New Member
  • *
  • Posts: 18
Control console app by shell script
« on: December 11, 2018, 06:14:38 am »
hi All,
I have a console app that I want to run from a shell script. Up to five variables and command to execute have to come from the shell script, without user intervention. Can this be done? Could someone show me a toy or point me to an example that I can look at? To demonstrate my level of ignorance, the following does not work:

=====================================
#! /bin/sh

./Appl start
input=/home/gary/hoseltopout.ply
output=/home/gary/testout.vtk
execute
quit
=====================================

(The above does presently work from the keyboard.)

thanks,
whiffee

(Mint 19, Lazarus 1.8.4)

CCRDude

  • Hero Member
  • *****
  • Posts: 600
Re: Control console app by shell script
« Reply #1 on: December 11, 2018, 09:40:40 am »
If I understood you correctly:

Code: [Select]
#!/bin/sh
./Apple param1 param2 param3

And in Appl.lpr, just printing them as an example:
Code: Pascal  [Select][+][-]
  1. WriteLn(ParamStr(1));
  2. WriteLn(ParamStr(2));
  3. WriteLn(ParamStr(3));
Make sure to use ParamCount to check if they're all specified.

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Control console app by shell script
« Reply #2 on: December 11, 2018, 10:05:22 am »
It seems like IDE menu "Project-> New project -> Console application" is what you're looking for.
It shows a convenient usage of HasOption, CheckOption, GetOptionValue etc.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Control console app by shell script
« Reply #3 on: December 11, 2018, 02:34:06 pm »
Try this:

File: Myscript.sh
Code: Bash  [Select][+][-]
  1. #! /bin/sh
  2.  
  3. ./Appl start < theInput
  4.  

File: theInput
Code: [Select]
input=/home/gary/hoseltopout.ply
output=/home/gary/testout.vtk
execute
quit

Bash (or shell) scripts are executed line by line: the shell will execute your app and wait for it to terminate before even looking at the next line.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

whiffee

  • New Member
  • *
  • Posts: 18
Re: Control console app by shell script
« Reply #4 on: December 11, 2018, 02:58:28 pm »
@ CCRDude,

Something is not working. My shell script:
Code: Pascal  [Select][+][-]
  1. #! /bin/sh
  2.  
  3. ./testapp  first second third
  4.  

My .lpr:
Code: Pascal  [Select][+][-]
  1. { add your program here }
  2.      WriteLn('this is the test app');
  3.  
  4.      WriteLn(ParamStr(1));
  5.      WriteLn(ParamStr(2));
  6.      WriteLn(ParamStr(3));
  7.      WriteLn(InttoStr(ParamCount));  

Only the first WriteLn prints. My uses are: Classes, SysUtils, CustApp, FileUtil

@lucamar,
My revised shell script:
Code: Pascal  [Select][+][-]
  1. #! /bin/sh
  2.  
  3. ./testapp start < first second third
  4.  

The error msg (and only output):
./console3.sh: 3: ./console3.sh: cannot open first: No such file


lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Control console app by shell script
« Reply #5 on: December 11, 2018, 03:30:24 pm »
@lucamar,
My revised shell script:
Code: Pascal  [Select][+][-]
  1. #! /bin/sh
  2. ./testapp start < first second third
  3.  
The error msg (and only output):
./console3.sh: 3: ./console3.sh: cannot open first: No such file

What that line does is to redirect the output of "first second third" (i.e. program "first") with parameters "second third") to the input of "testapp". You would have to use:
Code: Bash  [Select][+][-]
  1. ./testapp start < echo first second third

i.e. redirect the output of echo
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

whiffee

  • New Member
  • *
  • Posts: 18
Re: Control console app by shell script
« Reply #6 on: December 11, 2018, 04:30:18 pm »
Joy!!

Experimenting with lucamar's first post, I made a file called 'data':
Code: Pascal  [Select][+][-]
  1. input=/home/gary/hoseltopout.ply
  2. output=/home/gary/testout.vtk
  3. execute
  4. quit

then I revised the script:
Code: Pascal  [Select][+][-]
  1. #! /bin/sh
  2. ./Appl start < data

and then ran it. Success. Thanks for help. :)

 

TinyPortal © 2005-2018