INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Passing arguments from a file

Passing arguments from a file

(OP)
Hello everyone!
I have a little problem and I would very much appreciate any help. I am using clisp on a linux machine. I need to execute a program from the command line using arguments that are saved in a text file. I tried this:

clisp test.lsp > input

where test.lsp is my program and input is the text file containing the arguments. I use this way of passing arguments in other languages like C, C++ and Java but it doesn't seem to work with lisp.

Any saggestions?
Thanks in advance!
 

RE: Passing arguments from a file

What you tried was not redirecting stdin from the file, but redirecting stdout to the file.

Instead of

CODE

clisp test.lsp > input
try this

CODE

clisp test.lsp < input

RE: Passing arguments from a file

(OP)
It was a typo :(

I use the correct command.

RE: Passing arguments from a file

Then probably either your Lisp program don't read from standard input as you expected or your input file don't contain the data you expected.
It's difficult to guess, what's happened. To see what happens, try to debug your code.
If it's not too long you can post it here, or if it's very long try to extract the essential parts which deals with input and post it here.   

RE: Passing arguments from a file

(OP)
My program only contains this:

(print *args*)

and the input file contains just the two arguments:

a b


I did that only to test the functionality of the command in order to implement it in more complex programs. But even this simple program wont work.
When the command runs I should get:
(a b)

instead I get blank. In a more thorogh look I saw that I get two "enters":
\n
\n

I have no idea what this means :(

RE: Passing arguments from a file

Hi Hlektra,

Command line arguments and standard input are 2 different things.

You can design your program ambiguous in that way, that it will try to read command line arguments first and when nothing found it waits for user input from keyboard. In this case the redirecting from file will work.

Example: input.lsp

CODE

;;; *** Functions ***

;Returns a list containing items in 'string' split from
;occurrences of 'split-string'."
(defun string-split (split-string string)
  (loop with l = (length split-string)
   for n = 0 then (+ pos l)
   for pos = (search split-string string :start2 n)
   if pos collect (subseq string n pos)
   else collect (subseq string n)
   while pos))

; new line function
(defun new_line ()
  (format t "~%"))


;;; *** Main program ***

; 1. try to read comman line arguments
(format t "Reading command line arguments...")
(setq argument-list *args*)

; if command line arguments not found, try to read from standard input
(cond
  ((null argument-list)
   (format t "No command line arguments found !")
   (new_line)
   (format t "Now reading from standard input:")
   (new_line)
   (setq line (read-line))
   (format t "~%line = ~A" line)
   ; construnct argument-list from line-string
   (setq argument-list (string-split " " line))))

; print argument list
(format t "~%argument-list = ~A" argument-list)

; process your argument list...

If I now call the program with 2 command line arguments it collects them into a list and prints this list as showed here

CODE

C:\Users\Roman\Work>clisp input.lsp foo bar
Reading command line arguments...
argument-list = (foo bar)

If I call the same program without command line arguments it waits for user input, i.e. I need to input something and then press Enter

CODE

C:\Users\Roman\Work>clisp input.lsp
Reading command line arguments...No command line arguments found !
Now reading from standard input:
arg1 arg2 arg3
Here I pressed <Enter>...
line = arg1 arg2 arg3
argument-list = (arg1 arg2 arg3)
  
The program reads first an input line as a string and then splits this string using the temporary function string-split which I found here on the web:
http://coding.derkeiler.com/Archive/Lisp/comp.lang.lisp/2006-03/msg01976.html
/Oh yes I copied it rather as writing my own, because I'm using Lisp very rarely and writing such a function would be not so easy task for me smile/

Now if I had one line text-file named input.txt with this content

CODE

spam eggs
and now I try to redirect the standard input from this  file - look it works:

CODE

C:\Users\Roman\Work>clisp input.lsp < input.txt
Reading command line arguments...No command line arguments found !
Now reading from standard input:

line = spam eggs
argument-list = (spam eggs)
   

RE: Passing arguments from a file

(OP)
It worked!!
You saved me thank you mikrom :)

RE: Passing arguments from a file

Hi Hlektra,
It's ok and don't forget the STAR!
smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close