Recent

Author Topic: Running Linux shell commands having characters like asterisk in them  (Read 3289 times)

itmitica

  • Jr. Member
  • **
  • Posts: 85
The following shell command fails:
 
Code: Pascal  [Select][+][-]
  1. ls file*
no matter the implementation: RunCommand, ComandLine or TProcess Execute, whereas the following command works:
Code: Pascal  [Select][+][-]
  1. ls

To note the
Code: Pascal  [Select][+][-]
  1. ls file*

works in terminal: it shows only the files starting with the "file" prefix.

Like stated, I tried all the above implementations.
« Last Edit: February 11, 2016, 04:06:03 pm by itmitica »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Running Linux shell commands having characters like asterisk in them
« Reply #1 on: February 11, 2016, 03:06:45 pm »
The problem is that "file*" is not passed to "ls". It is the shell that expands file* and passes a list to "ls".

So the solution is to execute with shell, e.g. using baseunix.fpsystem() or manually
using

Code: Pascal  [Select][+][-]
  1. executeprocess('/bin/sh',['-c','ls file*']);

Note that the whole commandline is passed in the second argument.

itmitica

  • Jr. Member
  • **
  • Posts: 85
Re: Running Linux shell commands having characters like asterisk in them
« Reply #2 on: February 11, 2016, 03:12:29 pm »
Thanks @markov;

I'm working on finding attached usb devices. The shell command I came up with is this:

Code: [Select]
ls -lQ /dev/disk/by-id    \
  | sed -n '/usb/p'       \
  | sed '/sd.[0-9]/d'     \
  | sed 's/[^"]* //'      \
  | sed 's/\usb-//g'      \
  | sed 's/\..\/..\///g'  \
  | sed 's/_[0-9].*:0//g' \
  | sed 's/_/ /g'

I'm now in the process of finding a way to run this and read the output back. Any ideas?

http://postimg.org/image/c3t0jebfx/

So far, I could probably use a simpler version I got working and finish the string processing with Lazarus:

Code: [Select]
var
  AProcess : TProcess;
  AStringList: TStringList;
  output: String;
begin
  AProcess := TProcess.Create(nil);
  AStringList := TStringList.Create;
  AProcess.CommandLine := '/bin/sh -c  "ls -lQ /dev/disk/by-id/usb*"';
  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];

  AProcess.Execute;

  AStringList.LoadFromStream(AProcess.Output);
  output := AStringList.Text;
  ShowMessage(output);

  AStringList.Free;
  AProcess.Free;   
« Last Edit: February 11, 2016, 03:29:36 pm by itmitica »

itmitica

  • Jr. Member
  • **
  • Posts: 85
Re: Running Linux shell commands having characters like asterisk in them
« Reply #3 on: February 11, 2016, 06:41:54 pm »
If anyone is interested in the project I'm using this in, Laddiso, here is the github: https://github.com/itmitica/laddiso

This is the code with the incorporated shell command.

I had to escape double quotes in bash with a backslash and I had to nullify single quotes and double quotes inside the command string in Lazarus by doubling them.

Feedback is welcome.

Code: [Select]
var
  command: String;
  AProcess : TProcess;
  AStringList: TStringList;
  output: String;
begin
  command :=
    Trim('/bin/sh -c "                  ') +
    Trim('ls -lQ /dev/disk/by-id        ') +
    Trim('  |sed -n ''/usb/p''          ') +
    Trim('  |sed ''/sd.[0-9]/d''        ') +
    Trim('  |sed ''s/[^\""]* //''       ') +
    Trim('  |sed ''s/\usb-//g''         ') +
    Trim('  |sed ''s/\..\/..\///g''     ') +
    Trim('  |sed ''s/_[0-9].*:0//g''    ') +
    Trim('  |sed ''s/_/ /g''            ') +
    Trim('"                             ');

  AProcess := TProcess.Create(nil);
  AStringList := TStringList.Create;
  AProcess.CommandLine := command;
  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];

  AProcess.Execute;

  AStringList.LoadFromStream(AProcess.Output);
  output := AStringList.Text;
  ShowMessage(output);

  AStringList.Free;
  AProcess.Free;
end;   
« Last Edit: February 11, 2016, 07:04:24 pm by itmitica »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Running Linux shell commands having characters like asterisk in them
« Reply #4 on: February 11, 2016, 06:58:35 pm »
You shouldn't have to double double quotes. They have no meaning in pascal.

itmitica

  • Jr. Member
  • **
  • Posts: 85
Re: Running Linux shell commands having characters like asterisk in them
« Reply #5 on: February 11, 2016, 07:02:42 pm »
At first, I didn't. It didn't run with proper results.

 

TinyPortal © 2005-2018