Recent

Author Topic: [SOLVED] Linux: how to access the original commandline?  (Read 2835 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
[SOLVED] Linux: how to access the original commandline?
« on: September 11, 2018, 11:07:15 am »
When I have a program like this:

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}
  4.  
  5. var i: integer;
  6.  
  7. begin
  8. writeln('CmdLine=', system.CmdLine);
  9. writeln('paramCount=', paramCount);
  10. for i:=1 to paramCount do
  11.    writeln('paramStr(', i, ')=', paramStr(i));
  12. end.  
  13.  

and start it on Windows with 'a * b' as command line parameters, then my program shows:

Code: [Select]
D:\FPC\work\>test.exe a * b
CmdLine=test.exe  a * b
paramCount=3
paramStr(1)=a
paramStr(2)=*
paramStr(3)=b

But when I start this program and command line parameters on Linux, then my program inserts all  folders and files of the current folder:

Code: [Select]
hartmut@i3300:~$ test a * b
CmdLine=test a core Downloads examples.desktop Inst snap tmp b
paramCount=8
paramStr(1)=a
paramStr(2)=core
paramStr(3)=Downloads
paramStr(4)=examples.desktop
paramStr(5)=Inst
paramStr(6)=snap
paramStr(7)=tmp
paramStr(8)=b

I want to convert a couple of older console programs from Windows to Linux. Therefore I need access to the original commandline, where all characters and combinations are unchanged.
Is this possible and how? And is the asterisk the only character, which is expandend automatically, or are there more?

I'm an absolute beginner on Linux. I have FPC 3.0.4 on Ubuntu 18.04. Thanks in advance.
« Last Edit: September 11, 2018, 04:06:02 pm by Hartmut »

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Linux: how to access the original commandline?
« Reply #1 on: September 11, 2018, 11:40:01 am »
You are on Linux so there is no way to pass * without 'escaping' it.

Call test and escape the * sign.
Like this
Code: [Select]
hartmut@i3300:~$ test a \* b
$ is also a sign you should be aware of (if it's used in front of text it's seen as variable).

Also read http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html.

Is there any reason you want to do this with multiple parameters. If you quote all the parameters you get 1 in your program and you parse it yourself.
Code: [Select]
hartmut@i3300:~$ test 'a * b'In that case there is no need for escaping. (Do note that you can't use ' in your string in that case or you need to escape that)
« Last Edit: September 11, 2018, 11:47:42 am by rvk »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Linux: how to access the original commandline?
« Reply #2 on: September 11, 2018, 11:51:59 am »
As far as I know these characters should be avoided:

~ # & * ( \ | ; " ' < > ?

But both " and ' can be used in a pair, for example:
test 'a * b'
test "a * b"
« Last Edit: September 11, 2018, 11:53:39 am by Handoko »

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to access the original commandline?
« Reply #3 on: September 11, 2018, 04:05:36 pm »
Thank you both for your helpful answers. I learned that the user must escape a couple of characters, e.g. 'a \* b' or 'a "*" b'.

 

TinyPortal © 2005-2018